日期:2014-05-18  浏览次数:20403 次

查询某个字段的值一个月内每天出现的次数
表SSA
ID NAME UTime  
1 GL 2012-1-2
2 GL 2012-1-2
3 GL 2012-1-6

4 ml 2012-1-8

5 GL 2012-1-18

要的结果是样式如下

UNAME 1 2 3 4 5 6 7 8 .....18......31

GL 2 1 1

ml 8

------解决方案--------------------
SQL code

select NAME,
sum(case when day(UTIME)=1 then 1 else 0 end) as [1],
sum(case when day(UTIME)=2 then 1 else 0 end) as [2],
......
sum(case when day(UTIME)=31 then 1 else 0 end) as [31]
from SSA
group by NAME

------解决方案--------------------
探讨
SQL code
select NAME,
sum(case when day(UTIME)=1 then NOU else 0 end) as [1],
sum(case when day(UTIME)=2 then NOU else 0 end) as [2],
......
sum(case when day(UTIME)=31 then NOU else 0 end) as [3……