我的数据如下:
Docdate Qty Amount
-----------------------------------
2014/08/01(Friday) 5 100
2014/08/03(Sunday) 5 100
2014/08/04(Monday) 8 100
2014/08/10(Sunday) 8 100
2014/08/11(Monday) 8 100
2014/08/17(Sunday) 8 100
2014/08/18(Monday) 8 100
2014/08/24(Sunday) 8 100
2014/08/25(Monday) 8 100
2014/08/31(Sunday) 8 100
我想在下面的模板中概括一个存储过程:
Item QtyWeek1(1-3) QtyWeek2(4-10) QtyWeek3(11-17) QtyWeek4(18-24) QtyWeek5(25-31)
A 10 16 16 16 16
B - - - - -
是否可以在存储过程中执行此操作?
可以在Crystal Report中执行此操作吗?如果是,应显示哪些存储过程列?
由于
答案 0 :(得分:0)
如果您列出Docdate DATETIME类型
,请尝试此解决方案SELECT item, SUM(CASE WHEN DATEPART(wk, Docdate) BETWEEN 1 AND 3 THEN Qty ELSE 0 END) AS qtyWeek1,
SUM(CASE WHEN DATEPART(wk, Docdate) BETWEEN 4 AND 10 THEN Qty ELSE 0 END) AS qtyWeek2,
SUM(CASE WHEN DATEPART(wk, Docdate) BETWEEN 11 AND 17 THEN Qty ELSE 0 END) AS qtyWeek3,
SUM(CASE WHEN DATEPART(wk, Docdate) BETWEEN 18 AND 24 THEN Qty ELSE 0 END) AS qtyWeek4,
SUM(CASE WHEN DATEPART(wk, Docdate) BETWEEN 25 AND 31 THEN Qty ELSE 0 END) AS qtyWeek5
FROM YouTable
GROUP BY item
答案 1 :(得分:0)
一点也不容易。我想你还有一个列名项,你想要总共五周,所以我不得不考虑一个额外的列YYYYMM来包含周的月份。
然后查询将是:
select ITEM, MONTH_YEAR, [1] WEEK1, [2] WEEK2, [3] WEEK3, [4] WEEK4, [5] WEEK5
from (
select
ITEM,
YEAR(DATE1)*100+MONTH(DATE1) MONTH_YEAR,
1 + WEEK - WEEK0 WEEK,
sum(Qty) Qty
from Table1 t1
cross apply (SELECT cast(Replace(LEFT(DocDate,10),'/','-') as date)) N(DATE1)
cross apply (SELECT DATEPART(WEEK, DATEADD(DAY, -1, DATE1))) T(WEEK)
cross apply (SELECT DATEPART(WEEK, DateAdd(day, -1, DateAdd(month, MONTH(DATE1) - 1, DateAdd(Year, YEAR(DATE1)-1900, 0))))) U(WEEK0)
group by WEEK, YEAR(DATE1)*100+MONTH(DATE1), WEEK0, ITEM
) A
pivot (sum(QTY) FOR WEEK in ([1],[2],[3],[4],[5])) B
词汇表:
答案 2 :(得分:0)
由于您有水晶报告 DO NOT 在数据库中执行交叉表。在水晶报告中做。
如果你看一下这方面的常规建议,那就是:在报告工具中执行支点
一般解决方案是:
在您的现有记录集中添加一个列,例如weeknumber
,
将您现有的DocDate
映射到周数(您需要解释规则,即一周开始的时间,第1周等等,然后我可以建议一些代码)
在水晶报表中创建一个交叉表报表,该报表在新列weeknumber
上转动。
这样,如果你有2或20列,crystal会处理它,而不是在数据库对象中进行硬编码。