我正在尝试编写一份报告,为销售数据生成4组:每日,每月,每季度和每年。我遇到的问题是,如果没有每日活动,它将不会每月,每季度或每年生成。如何获得没有数据生成0的日子并仍然计算其他三组?这是我正在尝试使用的代码(它还没有将年份作为一个组包含在内):
WITH Summary AS
(SELECT
1 as GRP,
'DAILY' as Label,
O.SnapshotDateKey,
BilledDollars = CASE WHEN SUM(O.DollarsBilled) = 0 THEN 0 ELSE SUM(O.DollarsBilled) END,
BilledMSF = CASE WHEN SUM(O.MsfBilled) = 0 THEN 0 ELSE SUM(O.MsfBilled) END,
BookedDollarsPerMSF = CASE WHEN SUM(O.MsfBilled) = 0 THEN 0 ELSE (SUM(O.DollarsBilled)/SUM(O.MsfBilled)) END,
BilledContribution = CASE WHEN SUM(O.Contribution) = 0 THEN 0 ELSE SUM(O.Contribution) END,
BilledProfit = CASE WHEN SUM(O.Profit) = 0 THEN 0 ELSE SUM(O.Profit) END
FROM
FactInvoiceSummary O
WHERE
O.SnapshotDateKey = 20161230 and O.SalesRepKey = 167
GROUP BY
O.SnapshotDateKey
UNION
SELECT
2 as GRP,
'MTD' as Label,
O.SnapshotDateKey,
BilledDollars = CASE WHEN SUM(O.MTDDollarsBilled) = 0 THEN 0 ELSE (SUM(O.MTDDollarsBilled)) END,
BILLEDMSF = CASE WHEN SUM(O.MTDMsfBilled) = 0 THEN 0 ELSE (SUM(O.MTDMsfBilled)) END,
BookedDollarsPerMSF = CASE WHEN SUM(O.MTDMsfBilled) = 0 THEN 0 ELSE (SUM(O.MTDDollarsBilled)/SUM(O.MTDMsfBilled)) END,
SUM(O.MTDContribution) as BilledContribution,
SUM(O.MTDProfit) as BilledProfit
FROM
FactInvoiceSummary O
WHERE
O.SnapshotDateKey = 20161230 and O.SalesRepKey = 167
GROUP BY
O.SnapshotDateKey
UNION
SELECT
3 as GRP,
'QTD' as Label,
O.SnapshotDateKey,
BilledDollars = CASE WHEN SUM(O.QTDDollarsBilled) = 0 THEN 0 ELSE (SUM(O.QTDDollarsBilled)) END,
BILLEDMSF = CASE WHEN SUM(O.QTDMsfBilled) = 0 THEN 0 ELSE (SUM(O.QTDMsfBilled)) END,
BookedDollarsPerMSF = CASE WHEN SUM(O.QTDMsfBilled) = 0 THEN 0 ELSE (SUM(O.QTDDollarsBilled)/SUM(O.QTDMsfBilled)) END,
SUM(O.QTDContribution) as BilledContribution,
SUM(O.QTDProfit) as BilledProfit
FROM
FactInvoiceSummary O
WHERE
O.SnapshotDateKey = 20161230 and O.SalesRepKey = 167
GROUP BY
O.SnapshotDateKey
)
SELECT * FROM Summary
UNION ALL SELECT NULL, 0,0,0,0,0 WHERE NOT EXISTS (SELECT * FROM Summary)
谢谢!
答案 0 :(得分:0)
考虑更改WHERE
子句,因为union查询的所有部分都指向可能/可能不包含数据的精确单个日期。此外,重复所有部分中的每日聚合,因为更新的WHERE
条件将扩展行数。无需维护MTD,QTD或YTD列。此外,不需要GROUP BY
,因为所有列都可以与 GRP 和 Label 常量进行聚合。
由于SnapshotDateKey
似乎将日期捕获为数字,因此只需过滤非日常时段的适当范围即可。以下是CTE声明的更新:
WITH Summary AS
(SELECT
1 as GRP,
'DAILY' as Label,
MAX(O.SnapshotDateKey) AS MaxSnapshotDateKey,
BilledDollars = CASE WHEN SUM(O.DollarsBilled) = 0 THEN 0 ELSE SUM(O.DollarsBilled) END,
BilledMSF = CASE WHEN SUM(O.MsfBilled) = 0 THEN 0 ELSE SUM(O.MsfBilled) END,
BookedDollarsPerMSF = CASE WHEN SUM(O.MsfBilled) = 0 THEN 0 ELSE (SUM(O.DollarsBilled)/SUM(O.MsfBilled)) END,
BilledContribution = CASE WHEN SUM(O.Contribution) = 0 THEN 0 ELSE SUM(O.Contribution) END,
BilledProfit = CASE WHEN SUM(O.Profit) = 0 THEN 0 ELSE SUM(O.Profit) END
FROM
FactInvoiceSummary O
WHERE
O.SnapshotDateKey = 20161230 and O.SalesRepKey = 167
UNION
SELECT
2 as GRP,
'MTD' as Label,
MAX(O.SnapshotDateKey) AS MaxSnapshotDateKey,
BilledDollars = CASE WHEN SUM(O.DollarsBilled) = 0 THEN 0 ELSE SUM(O.DollarsBilled) END,
BilledMSF = CASE WHEN SUM(O.MsfBilled) = 0 THEN 0 ELSE SUM(O.MsfBilled) END,
BookedDollarsPerMSF = CASE WHEN SUM(O.MsfBilled) = 0 THEN 0 ELSE (SUM(O.DollarsBilled)/SUM(O.MsfBilled)) END,
BilledContribution = CASE WHEN SUM(O.Contribution) = 0 THEN 0 ELSE SUM(O.Contribution) END,
BilledProfit = CASE WHEN SUM(O.Profit) = 0 THEN 0 ELSE SUM(O.Profit) END
FROM
FactInvoiceSummary O
WHERE
O.SnapshotDateKey BETWEEN 20161201 AND 20161230 and O.SalesRepKey = 167
UNION
SELECT
3 as GRP,
'QTD' as Label,
MAX(O.SnapshotDateKey) AS MaxSnapshotDateKey,
BilledDollars = CASE WHEN SUM(O.DollarsBilled) = 0 THEN 0 ELSE SUM(O.DollarsBilled) END,
BilledMSF = CASE WHEN SUM(O.MsfBilled) = 0 THEN 0 ELSE SUM(O.MsfBilled) END,
BookedDollarsPerMSF = CASE WHEN SUM(O.MsfBilled) = 0 THEN 0 ELSE (SUM(O.DollarsBilled)/SUM(O.MsfBilled)) END,
BilledContribution = CASE WHEN SUM(O.Contribution) = 0 THEN 0 ELSE SUM(O.Contribution) END,
BilledProfit = CASE WHEN SUM(O.Profit) = 0 THEN 0 ELSE SUM(O.Profit) END
FROM
FactInvoiceSummary O
WHERE
O.SnapshotDateKey BETWEEN 20161001 AND 20161230 and O.SalesRepKey = 167
)
除此之外 - 理想情况下,您使用日期时间字段来维护日期/时间部分以提取日/周/月/季/年指标。同样如上所示,仅汇总一个的DollarsBilled,MsfBilled,Contribution,Profit列,而不计算MTD,QTD,YTD的计算集。