我有一个查询可以提供36个月的单位销售历史记录。它按原样工作,但我想知道是否有办法让它不那么冗长和繁琐。
查询有两个部分 - 标题和数据。
以下是标题的内容:
SELECT
DATEADD(m, -36, GETDATE()-day(GETDATE()-1)) AS "MM36"
, DATEADD(m, -35, GETDATE()-day(GETDATE()-1)) AS "MM35"
, DATEADD(m, -34, GETDATE()-day(GETDATE()-1)) AS "MM34"
截至今天,这是2011年9月 - 11月。它继续......
, DATEADD(m, -03, GETDATE()-day(GETDATE()-1)) AS "MM03"
, DATEADD(m, -02, GETDATE()-day(GETDATE()-1)) AS "MM02"
, DATEADD(m, -01, GETDATE()-day(GETDATE()-1)) AS "MM01"
2014年6月 - 8月.36行代码。
提取单位销售数据的陈述如下:
, SUM (CASE WHEN "SalesTable"."SalesDate" BETWEEN DATEADD(m, -36, GETDATE()-day(GETDATE()-1)) AND DATEADD(m, -35, GETDATE()-day(GETDATE()-1))-1 THEN "SalesTable"."SalesQty" - "SalesTable"."ReturnsQty" ELSE NULL END) AS "NetQty36"
, SUM (CASE WHEN "SalesTable"."SalesDate" BETWEEN DATEADD(m, -35, GETDATE()-day(GETDATE()-1)) AND DATEADD(m, -34, GETDATE()-day(GETDATE()-1))-1 THEN "SalesTable"."SalesQty" - "SalesTable"."ReturnsQty" ELSE NULL END) AS "NetQty35"
, SUM (CASE WHEN "SalesTable"."SalesDate" BETWEEN DATEADD(m, -34, GETDATE()-day(GETDATE()-1)) AND DATEADD(m, -33, GETDATE()-day(GETDATE()-1))-1 THEN "SalesTable"."SalesQty" - "SalesTable"."ReturnsQty" ELSE NULL END) AS "NetQty34"
截至今天,这是2011年9月 - 11月。它继续......
, SUM (CASE WHEN "SalesTable"."SalesDate" BETWEEN DATEADD(m, -03, GETDATE()-day(GETDATE()-1)) AND DATEADD(m, -02, GETDATE()-day(GETDATE()-1))-1 THEN "SalesTable"."SalesQty" - "SalesTable"."ReturnsQty" ELSE NULL END) AS "NetQty03"
, SUM (CASE WHEN "SalesTable"."SalesDate" BETWEEN DATEADD(m, -02, GETDATE()-day(GETDATE()-1)) AND DATEADD(m, -01, GETDATE()-day(GETDATE()-1))-1 THEN "SalesTable"."SalesQty" - "SalesTable"."ReturnsQty" ELSE NULL END) AS "NetQty02"
, SUM (CASE WHEN "SalesTable"."SalesDate" BETWEEN DATEADD(m, -01, GETDATE()-day(GETDATE()-1)) AND DATEADD(m, -00, GETDATE()-day(GETDATE()-1))-1 THEN "SalesTable"."SalesQty" - "SalesTable"."ReturnsQty" ELSE NULL END) AS "NetQty01"
2014年6月至8月。
在本报告完成之前,我还有很多要补充的内容,所以我希望尽可能保持整洁。
谢谢,
修改
下面的例子显示,通过上面的代码,我能够得到我想要的东西(没有代表发布图像所以我必须做一个链接)。它还显示,使用数据透视表我错过了我的项目编号和滚动月份。如何添加这两件事?
我尝试删除“ItemTable”。“ItemNum”字段如下,但它没有显示在我的Crystal Report中。我试图把它放在其他任何地方,我得到“无法绑定”的错误。
select
[0] MM00
, [1] MM01
, [2] MM02
, ...
from (
select
"ItemTable"."ItemNum",DateDiff(m, "SalesTable"."SalesDate", GetDate()) months_ago
, "SalesTable"."SalesQty" - "SalesTable"."ReturnsQty" NetQty
from your_table
) as source
pivot
(
sum(NetQty) For months_ago in ([0], [1], [2], [3], ...)
) as PivotTable
再次感谢。
答案 0 :(得分:2)
这种情况似乎已成熟Pivot table。
的内容
select
[0] MM00
, [1] MM01
, [2] MM02
, ...
from (
select
DateDiff(m, "SalesTable"."SalesDate", GetDate()) months_ago
, "SalesTable"."SalesQty" - "SalesTable"."ReturnsQty" NetQty
from your_table
) as source
pivot
(
sum(NetQty) For months_ago in ([0], [1], [2], [3], ...)
) as PivotTable
修改强>
完善示例:包括项目编号非常简单;只需将它包含在select语句中即可。
select
"ItemTable"."ItemNum"
, [0] MM00
, [1] MM01
, [2] MM02
, ...
from (
select
"ItemTable"."ItemNum"
, DateDiff(m, "SalesTable"."SalesDate", GetDate()) months_ago
, "SalesTable"."SalesQty" - "SalesTable"."ReturnsQty" NetQty
from your_table
) as source
pivot
(
sum(NetQty) For months_ago in ([0], [1], [2], [3], ...)
) as PivotTable
拥有枢轴列的动态名称相当困难。一般的想法是你必须将SQL构建为字符串并动态执行它。 Google搜索会产生大量结果(例如a SQL Hints blog)。一些不完整的代码可以为您提供一般性的想法。
SET @DynamicPivotQuery =
N'select
"ItemTable"."ItemNum"
, [' + @Name1 + ']
, [' + @Name2 + ']
, [' + @NameN + ']
from (
select
"ItemTable"."ItemNum"
/* search MSDN for the best date formatting algorithm */
, CONVERT_TO_STRING("SalesTable"."SalesDate") month
, "SalesTable"."SalesQty" - "SalesTable"."ReturnsQty" NetQty
from your_table
) as source
pivot
(
sum(NetQty) For month in (
[' + @Name1 + ']
, [' + @Name2 + ']
, [' + @NameN + ']
)
) as PivotTable';
--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery