按周分组统计的一条语句,我写的不好,见笑了
select 注册人数=count(1),第几周=rtrim(datepart(year,regtime))+ rtrim(datepart(wk, regtime)) from T_user group by rtrim(datepart(year,regtime))+ rtrim(datepart(wk, regtime)) order by rtrim(datepart(year,regtime)) + rtrim(datepart(wk, regtime)) desc
能给改得好点吗。我现在这个效果不好,(排序不对)。而且语句拖沓。最好能带上每一周的起始日期,多谢了!
注册人数 第几周
1113 20079
100 20078
142 20077
352 20076
499 20075
244 20074
240 20073
115 20072
400 200716
994 200715
999 200714
1319 200713
969 200712
934 200711
1022 200710
171 20071
39 200653
282 200652
240 200651
212 200650
37 200649
27 200648
34 200647
33 200646
27 200645
35 200644
41 200643
49 200642
34 200641
17 200640
5 200639
45 200638
33 200637
30 200636
1 200635
------解决方案--------------------select 注册人数=count(1),
第几周=left(regtime,4)+ rtrim(datepart(wk, regtime))
from T_user
group by left(regtime,4)+ rtrim(datepart(wk, regtime))
order by left(regtime,4)+ rtrim(datepart(wk, regtime)) 这样应该也行.哈哈
------解决方案--------------------只精简到这一步,排序更正了.
select 注册人数=count(1),第几周=rtrim(datepart(year,regtime))+ rtrim(datepart(wk, regtime)) from T_user group by datepart(year,regtime),datepart(wk, regtime) order by datepart(year,regtime),(datepart(wk, regtime) desc
求每周的第一天与最后一天加上下面的:
,DATEADD(wk,DATEDIFF(wk,0,dateym),0)
,DATEADD(wk,DATEDIFF(wk,0,dateym),6)
------解决方案--------------------我也遇到同样的问题,帮你顶一个
------解决方案--------------------select 人数=count(1),第几周=convert(varchar(4),regtime,120)+ '年第 '+right( '0 '+convert(varchar(2),datepart(wk,regtime)),2)+ '周 '
from T_user
group by convert(varchar(4),regtime,120)+ '年第 '+right( '0 '+convert(varchar(2),datepart(wk,regtime)),2)+ '周 '
order by convert(varchar(4),regtime,120)+ '年第 '+right( '0 '+convert(varchar(2),datepart(wk,regtime)),2)+ '周 ' desc