日期列表中的星期日数

时间:2013-12-19 05:44:33

标签: sql sql-server-2005

我有一个俱乐部成员名单以及他们参加俱乐部的日期。我需要知道每个成员在一段时间内参加了多少个星期天。我有一张表格列出每个出席人数,由会员编号和出勤日期组成。

例如,在这个例子中,13/1和20/1是星期日

MEMBER                 ATTENDANCE

12345                  13/1/13

12345                  14/1/13

56789                  13/1/13

56789                  14/1/13

56789                  20/1/13

24680                  14/1/13

24680                  15/1/13

理想情况下,我希望看到这个回复:

MEMBER                 # OF SUNDAYS

12345                  1

56789                  2

24680                  0

1 个答案:

答案 0 :(得分:3)

select MEMBER, 
sum(case when datename(weekday,ATTENDANCE)='Sunday' then 1 else 0 end) as no_of_sundays 
from table 
group by MEMBER

编辑:要处理重复日期,请使用

select MEMBER, 
sum(case when datename(weekday,ATTENDANCE)='Sunday' then 1 else 0 end) as no_of_sundays 
from (select distinct MEMBER,ATTENDANCE from table ) as t 
group by MEMBER