年代统计即按(1940-1-1~1949-12-31,1950-1-1~1959-12-31,……)来分类统计,我写出的语句如下:
SELECT COUNT(strWorkNo) CountPer, YearFirst FROM (
SELECT strWorkNo,'YearFirst' = CASE WHEN dtBirthday Between CAST((YEAR(dtBirthday)/10) as varchar(4))+'0-1-1'
and CAST((YEAR(dtBirthday)/10) as varchar(4))+'9-12-31' THEN CAST((YEAR(dtBirthday)/10) as varchar(4))+'0'
ELSE '0' END
from HREmployee
) a
GROUP BY YearFirst
SELECT strWorkNo,'YearFirst' = CASE WHEN dtBirthday Between CAST((YEAR(dtBirthday)/10) as varchar(4))+'0-1-1'
and CAST((YEAR(dtBirthday)/10) as varchar(4))+'9-12-31' THEN CAST((YEAR(dtBirthday)/10) as varchar(4))+'0'
ELSE '0' END
from HREmployee
) a
GROUP BY YearFirst
周统计
select sum(销售金额), datename(week, 销售日期-1) from sales where 销售日期 between begindate and enddate group by datename(week, 销售日期-1)
注意:这里之所以要把销售日期-1是因为sql server默认的一周的第一天是星期天,而我们习惯的统计是以星期一到星期天计算的,所以减一。