UNION ALL - 防止合并列
UNION ALL - Prevent Combining of Columns
我尝试了以下操作,但最后我得到了一个单列 Amount2,其中也包含来自 Amount1 的 SUM。
SELECT
YEAR(createdDate) as Year,
MONTH(createdDate) AS Month,
Sum(GrandTotal) AS Amount1
FROM
Quotes
WHERE createdDate BETWEEN @DateFrom AND @DateTo
GROUP BY YEAR(createdDate), MONTH(createdDate)
--ORDER BY YEAR(createdDate), MONTH(createdDate)
UNION ALL
SELECT
YEAR(createdDate) as Year,
MONTH(createdDate) AS Month,
Sum(GrandTotal) AS Amount2
FROM
Quotes
WHERE createdDate BETWEEN @DateFrom AND @DateTo
AND orderDate IS NOT NULL
GROUP BY YEAR(createdDate), MONTH(createdDate)
ORDER BY YEAR(createdDate), MONTH(createdDate);
但是我想保留 Amount1 和 Amount2 columns/amounts。
实际上我想得到如下结果:
Year | Month | Amount1 | Amount2
---------------------------------
2016 4 120 70
2016 5 300 110
您无法为 Amount1 和 Amount2 获取两列。您的结果由同一列组成
列别名应该相同(并且仅在第一个 select 中)
SELECT
YEAR(createdDate) as Year,
MONTH(createdDate) AS Month,
Sum(GrandTotal) AS Amount1
FROM
Quotes
WHERE createdDate BETWEEN @DateFrom AND @DateTo
GROUP BY YEAR(createdDate), MONTH(createdDate)
--ORDER BY YEAR(createdDate), MONTH(createdDate)
UNION ALL
SELECT
YEAR(createdDate) ,
MONTH(createdDate),
Sum(GrandTotal)
FROM
Quotes
WHERE createdDate BETWEEN @DateFrom AND @DateTo
AND orderDate IS NOT NULL
GROUP BY YEAR(createdDate), MONTH(createdDate)
ORDER BY YEAR(createdDate), MONTH(createdDate);
或者如果你想列,你应该使用正确的 JOIN
SELECT
YEAR(a.createdDate) as Year,
MONTH(a.createdDate) AS Month,
Sum(a.GrandTotal) AS Amount1,
Sum(b.GrandTotal) AS Amount2
FROM Quotes as a
INNER JOIN Quotes as b on (YEAR(a.createdDate) as YEAR(b.createdDate)
AND MONTH(a.createdDate) = MONTH(b.createdDate))
WHERE a.createdDate BETWEEN @DateFrom AND @DateTo
GROUP BY YEAR(a.createdDate), MONTH(a.createdDate)
ORDER BY YEAR(createdDate), MONTH(createdDate)
最简单的方法就是使用条件聚合:
SELECT YEAR(createdDate) as Year,
MONTH(createdDate) AS Month,
Sum(GrandTotal) AS Amount1,
SUM(CASE WHEN orderDate IS NOT NULL THEN GrandTotal END) as Amount2
FROM Quotes
WHERE createdDate BETWEEN @DateFrom AND @DateTo
GROUP BY YEAR(createdDate), MONTH(createdDate)
ORDER BY YEAR(createdDate), MONTH(createdDate);
如果您希望值位于单独的行(而不是单独的列),则向 GROUP BY
:
添加一个键
SELECT YEAR(createdDate) as Year,
MONTH(createdDate) AS Month,
(CASE WHEN orderDate IS NOT NULL THEN 'valid' ELSE 'null' END) as HasOrderDate,
Sum(GrandTotal) AS Amount
FROM Quotes
WHERE createdDate BETWEEN @DateFrom AND @DateTo
GROUP BY YEAR(createdDate), MONTH(createdDate),
(CASE WHEN orderDate IS NOT NULL THEN 'valid' ELSE 'null' END)
ORDER BY YEAR(createdDate), MONTH(createdDate),
(CASE WHEN orderDate IS NOT NULL THEN 'valid' ELSE 'null' END);
我尝试了以下操作,但最后我得到了一个单列 Amount2,其中也包含来自 Amount1 的 SUM。
SELECT
YEAR(createdDate) as Year,
MONTH(createdDate) AS Month,
Sum(GrandTotal) AS Amount1
FROM
Quotes
WHERE createdDate BETWEEN @DateFrom AND @DateTo
GROUP BY YEAR(createdDate), MONTH(createdDate)
--ORDER BY YEAR(createdDate), MONTH(createdDate)
UNION ALL
SELECT
YEAR(createdDate) as Year,
MONTH(createdDate) AS Month,
Sum(GrandTotal) AS Amount2
FROM
Quotes
WHERE createdDate BETWEEN @DateFrom AND @DateTo
AND orderDate IS NOT NULL
GROUP BY YEAR(createdDate), MONTH(createdDate)
ORDER BY YEAR(createdDate), MONTH(createdDate);
但是我想保留 Amount1 和 Amount2 columns/amounts。
实际上我想得到如下结果:
Year | Month | Amount1 | Amount2
---------------------------------
2016 4 120 70
2016 5 300 110
您无法为 Amount1 和 Amount2 获取两列。您的结果由同一列组成
列别名应该相同(并且仅在第一个 select 中)
SELECT
YEAR(createdDate) as Year,
MONTH(createdDate) AS Month,
Sum(GrandTotal) AS Amount1
FROM
Quotes
WHERE createdDate BETWEEN @DateFrom AND @DateTo
GROUP BY YEAR(createdDate), MONTH(createdDate)
--ORDER BY YEAR(createdDate), MONTH(createdDate)
UNION ALL
SELECT
YEAR(createdDate) ,
MONTH(createdDate),
Sum(GrandTotal)
FROM
Quotes
WHERE createdDate BETWEEN @DateFrom AND @DateTo
AND orderDate IS NOT NULL
GROUP BY YEAR(createdDate), MONTH(createdDate)
ORDER BY YEAR(createdDate), MONTH(createdDate);
或者如果你想列,你应该使用正确的 JOIN
SELECT
YEAR(a.createdDate) as Year,
MONTH(a.createdDate) AS Month,
Sum(a.GrandTotal) AS Amount1,
Sum(b.GrandTotal) AS Amount2
FROM Quotes as a
INNER JOIN Quotes as b on (YEAR(a.createdDate) as YEAR(b.createdDate)
AND MONTH(a.createdDate) = MONTH(b.createdDate))
WHERE a.createdDate BETWEEN @DateFrom AND @DateTo
GROUP BY YEAR(a.createdDate), MONTH(a.createdDate)
ORDER BY YEAR(createdDate), MONTH(createdDate)
最简单的方法就是使用条件聚合:
SELECT YEAR(createdDate) as Year,
MONTH(createdDate) AS Month,
Sum(GrandTotal) AS Amount1,
SUM(CASE WHEN orderDate IS NOT NULL THEN GrandTotal END) as Amount2
FROM Quotes
WHERE createdDate BETWEEN @DateFrom AND @DateTo
GROUP BY YEAR(createdDate), MONTH(createdDate)
ORDER BY YEAR(createdDate), MONTH(createdDate);
如果您希望值位于单独的行(而不是单独的列),则向 GROUP BY
:
SELECT YEAR(createdDate) as Year,
MONTH(createdDate) AS Month,
(CASE WHEN orderDate IS NOT NULL THEN 'valid' ELSE 'null' END) as HasOrderDate,
Sum(GrandTotal) AS Amount
FROM Quotes
WHERE createdDate BETWEEN @DateFrom AND @DateTo
GROUP BY YEAR(createdDate), MONTH(createdDate),
(CASE WHEN orderDate IS NOT NULL THEN 'valid' ELSE 'null' END)
ORDER BY YEAR(createdDate), MONTH(createdDate),
(CASE WHEN orderDate IS NOT NULL THEN 'valid' ELSE 'null' END);