开发者

sql server stored procedure get count of people attending events

I need to display the number of people responding yes to attend events. Here is my SELECT statement so far:

SELECT TOP (100) PERCENT
    dbo.tb_EventAttendance.WillAttend,
    dbo.tb_Events.EventName,
    dbo.tb_Events.EventDate
FROM dbo.tb_EventAttendance
INNER JOIN dbo.tb_Events ON dbo.tb_EventAttendance.EventID = dbo.tb_Events.dbID
WHERE (dbo.tb_EventAttendance.WillAttend = 'Y')
ORDER BY dbo.tb_Event开发者_开发技巧s.EventDate

Do I use the COUNT function and if so how exactly in this situation?


You need to use both COUNT and a GROUP BY (because of the aggregate COUNT):

SELECT TOP (100) PERCENT dbo.tb_Events.EventName, dbo.tb_Events.EventDate,
COUNT(dbo.tb_EventAttendance.WillAttend) as NumAttendees, 
FROM  dbo.tb_EventAttendance INNER JOIN
dbo.tb_Events ON dbo.tb_EventAttendance.EventID = dbo.tb_Events.dbID
WHERE     (dbo.tb_EventAttendance.WillAttend = 'Y')
GROUP BY dbo.tb_EventName, dbo.tb_Events.EventDate
ORDER BY dbo.tb_Events.EventDate
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜