CTE 无法对多列中的空白列求和
CTE not able to sum blank columns out of multiple columns
我想为每一行计算 total_revenue 列,但 cte 没有这样做。我得到的 pr 列在几个月内都是空的,并且对其中的一些有价值。所以我得到一个 total_revenue ,其中 pr 列不为空。我做错了什么?
ormonth cc_o cc_r po pr po_o po_r total_orders total_revenue
2018-06 1367 152903.89 0 44 6424.66 1411
2018-05 10069 1086544.02 0 242 27926.02 10311
2018-04 11208 1151114.03 0 221 26004.69 11429
2018-01 13416 1379387.01 0 152 17023.29 13568
2018-03 12025 1303165.8 0 197 19957.87 12222
2018-02 12647 1252867.33 0 171 16907.52 12818
2018-01 145 1100.43 6 121 170 1600 321 2822
这是我使用的查询:
with cte_Revenue as (
SELECT
CONVERT(VARCHAR(7),Orders.OrderDate,126) as ormonth,
COUNT(CASE WHEN Orders.paymentmethodid = 5 OR Orders.paymentmethodid = 6 THEN Orders.PaymentAmount END) as 'CC_O',
SUM(CASE WHEN Orders.paymentmethodid = 5 OR Orders.paymentmethodid = 6 THEN Orders.PaymentAmount END) as 'CC_R',
COUNT(CASE WHEN Orders.paymentmethodid = 25 THEN Orders.paymentmethodid END) as 'PO',
SUM(CASE WHEN Orders.paymentmethodid = 25 THEN Orders.PaymentAmount END ) as 'PR',
COUNT(CASE WHEN Orders.paymentmethodid = 1 THEN Orders.paymentmethodid END) as 'PO_O',
SUM(CASE WHEN Orders.paymentmethodid = 1 THEN Orders.PaymentAmount END ) as 'PO_R'
FROM
Orders
WHERE
Orders.OrderDate BETWEEN '01/01/2018' AND getdate() -0 AND
Orders.OrderStatus <> 'Cancelled'
GROUP BY
CONVERT(VARCHAR(7),Orders.OrderDate,126))
Select
ormonth,
CC_O,
CC_R,
PO,
PR,
PO_O,
PO_R,
(CC_O+PO+PO_O) AS Total_Orders,
(CC_R+PR+PO_R) AS Total_Revenue
FROM cte_Revenue
看起来 PR 列可能 returning NULL,这将导致无法执行计算。正如我在评论中提到的,也许 SELECT 中的 CASE 语句可以解决这个问题。
SELECT ormonth, CC_O, CC_R, PO, PR, PO_O, PO_R,
(CC_O + PO + PO_O) AS Total_Orders,
(CC_R + CASE WHEN PR IS NULL THEN 0 ELSE PR END + PO_R) AS Total_Revenue
FROM cte_Revenue
另一种选择是确保在 CTE 中,PR 列始终具有值。在 CTE 中放入 return 0 的 ELSE 语句也可能有所帮助。
例如:
COUNT(CASE WHEN Orders.paymentmethodid = 1 THEN Orders.paymentmethodid
ELSE 0 END) as 'PO_O',
SUM(CASE WHEN Orders.paymentmethodid = 1 THEN Orders.PaymentAmount
ELSE 0 END ) as 'PO_R'
我想为每一行计算 total_revenue 列,但 cte 没有这样做。我得到的 pr 列在几个月内都是空的,并且对其中的一些有价值。所以我得到一个 total_revenue ,其中 pr 列不为空。我做错了什么?
ormonth cc_o cc_r po pr po_o po_r total_orders total_revenue
2018-06 1367 152903.89 0 44 6424.66 1411
2018-05 10069 1086544.02 0 242 27926.02 10311
2018-04 11208 1151114.03 0 221 26004.69 11429
2018-01 13416 1379387.01 0 152 17023.29 13568
2018-03 12025 1303165.8 0 197 19957.87 12222
2018-02 12647 1252867.33 0 171 16907.52 12818
2018-01 145 1100.43 6 121 170 1600 321 2822
这是我使用的查询:
with cte_Revenue as (
SELECT
CONVERT(VARCHAR(7),Orders.OrderDate,126) as ormonth,
COUNT(CASE WHEN Orders.paymentmethodid = 5 OR Orders.paymentmethodid = 6 THEN Orders.PaymentAmount END) as 'CC_O',
SUM(CASE WHEN Orders.paymentmethodid = 5 OR Orders.paymentmethodid = 6 THEN Orders.PaymentAmount END) as 'CC_R',
COUNT(CASE WHEN Orders.paymentmethodid = 25 THEN Orders.paymentmethodid END) as 'PO',
SUM(CASE WHEN Orders.paymentmethodid = 25 THEN Orders.PaymentAmount END ) as 'PR',
COUNT(CASE WHEN Orders.paymentmethodid = 1 THEN Orders.paymentmethodid END) as 'PO_O',
SUM(CASE WHEN Orders.paymentmethodid = 1 THEN Orders.PaymentAmount END ) as 'PO_R'
FROM
Orders
WHERE
Orders.OrderDate BETWEEN '01/01/2018' AND getdate() -0 AND
Orders.OrderStatus <> 'Cancelled'
GROUP BY
CONVERT(VARCHAR(7),Orders.OrderDate,126))
Select
ormonth,
CC_O,
CC_R,
PO,
PR,
PO_O,
PO_R,
(CC_O+PO+PO_O) AS Total_Orders,
(CC_R+PR+PO_R) AS Total_Revenue
FROM cte_Revenue
看起来 PR 列可能 returning NULL,这将导致无法执行计算。正如我在评论中提到的,也许 SELECT 中的 CASE 语句可以解决这个问题。
SELECT ormonth, CC_O, CC_R, PO, PR, PO_O, PO_R,
(CC_O + PO + PO_O) AS Total_Orders,
(CC_R + CASE WHEN PR IS NULL THEN 0 ELSE PR END + PO_R) AS Total_Revenue
FROM cte_Revenue
另一种选择是确保在 CTE 中,PR 列始终具有值。在 CTE 中放入 return 0 的 ELSE 语句也可能有所帮助。
例如:
COUNT(CASE WHEN Orders.paymentmethodid = 1 THEN Orders.paymentmethodid
ELSE 0 END) as 'PO_O',
SUM(CASE WHEN Orders.paymentmethodid = 1 THEN Orders.PaymentAmount
ELSE 0 END ) as 'PO_R'