我有一张发票行表,列出了库存代码,数量,变换列。 Boss希望看到过去12周内每种股票的销售量,列为股票代码,第0周(本日历周),第1周,......,第11周。
我使用以下脚本为每个股票代码表生成第0周,但不能将第1周〜第11周放入同一个表中:
set datefirst 1;
select
stockcode, sum(quantity) as TotalSaleOfWeek0
from
DR_INVLINES
where
TRANSDATE > (getdate() - DATEPART(dw,getdate()))
and QUANTITY <> 0
group by
STOCKCODE;
请帮助..
提前致谢..
答案 0 :(得分:0)
试试这个:
set datefirst 1;
select
stockcode, DATEPART(WEEK, TRANSDATE) As Week, sum(quantity) as TotalSaleOfWeek0
from
DR_INVLINES
where
TRANSDATE > DATEADD(Week, -12, getdate())
and QUANTITY <> 0
group by
STOCKCODE, DATEPART(WEEK, TRANSDATE);