积极答复者
求助(SQL相关)

问题
答案
-
ps.自己把数字周转换为汉字周吧
declare @table table ( d date, sales int ) insert into @table select '2011-01-01',10000 union all select '2011-01-02',10001 union all select '2011-01-03',10002 union all select '2011-01-04',10003 union all select '2011-01-05',10044 union all select '2011-01-11',10001 union all select '2011-01-12',10001 union all select '2011-01-13',10001 union all select '2011-01-21',10002 union all select '2011-01-23',10003 select * from ( select '' as d,case when grouping(DATEPART(ww,d))=1 then 'all' else convert(varchar(20),DATEPART(ww,d)) end w,SUM(sales) sales from @table group by DATEPART(ww,d) with cube union all select CONVERT(varchar(20),d,120) as d,convert(varchar(20),DATEPART(ww,d)) w, sales from @table ) t order by w,d desc /* d w sales -------------------- -------------------- ----------- 2011-01-01 1 10000 1 10000 2011-01-05 2 10044 2011-01-04 2 10003 2011-01-03 2 10002 2011-01-02 2 10001 2 40050 2011-01-13 3 10001 2011-01-12 3 10001 2011-01-11 3 10001 3 30003 2011-01-21 4 10002 4 10002 2011-01-23 5 10003 5 10003 all 100058 (16 行受影响) */
family as water- 已标记为答案 allen00111 2011年2月12日 2:28
全部回复
-
ps.自己把数字周转换为汉字周吧
declare @table table ( d date, sales int ) insert into @table select '2011-01-01',10000 union all select '2011-01-02',10001 union all select '2011-01-03',10002 union all select '2011-01-04',10003 union all select '2011-01-05',10044 union all select '2011-01-11',10001 union all select '2011-01-12',10001 union all select '2011-01-13',10001 union all select '2011-01-21',10002 union all select '2011-01-23',10003 select * from ( select '' as d,case when grouping(DATEPART(ww,d))=1 then 'all' else convert(varchar(20),DATEPART(ww,d)) end w,SUM(sales) sales from @table group by DATEPART(ww,d) with cube union all select CONVERT(varchar(20),d,120) as d,convert(varchar(20),DATEPART(ww,d)) w, sales from @table ) t order by w,d desc /* d w sales -------------------- -------------------- ----------- 2011-01-01 1 10000 1 10000 2011-01-05 2 10044 2011-01-04 2 10003 2011-01-03 2 10002 2011-01-02 2 10001 2 40050 2011-01-13 3 10001 2011-01-12 3 10001 2011-01-11 3 10001 3 30003 2011-01-21 4 10002 4 10002 2011-01-23 5 10003 5 10003 all 100058 (16 行受影响) */
family as water- 已标记为答案 allen00111 2011年2月12日 2:28