SQL记录和计数

时间:2013-10-14 23:06:09

标签: sql count

无法格式化SQL Quert以获取记录和计数。

SELECT
     eventStartDate
    ,eventTitle
    ,eventDesc
    ,eventTime
    ,count(eventID)
FROM GroupEvents 
WHERE eventStartDate = '10/14/2013'
GROUP BY eventStartDate

我想找回记录,以便我可以这样显示... (我可以格式化 - 只显示我需要的东西)

There are 3 events for 10/14/2013   
    1. Dinner at Anthony's - 5:30pm
    2. Movie with New Members - 7:30pm
    3. Denny's - 11:00pm

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

在SQL Server中,您可以使用Window Function解决此问题:

SELECT
     eventStartDate
    ,eventTitle
    ,eventDesc
    ,eventTime
    ,count(eventID)over(partition by eventStartDate) as eventCount
FROM GroupEvents 
WHERE eventStartDate = '10/14/2013'
GROUP BY eventStartDate

答案 1 :(得分:0)

如果您使用的RBDMS不支持窗口函数,则需要分别确定每个事件日期的计数,然后加入此中间结果以获取每个事件的计数和详细信息:

SELECT
     ge.eventStartDate
    ,ge.eventTitle
    ,ge.eventDesc
    ,ge.eventTime
    ,q.eventCount
FROM GroupEvents ge
JOIN (
    SELECT
         eventStartDate
        ,count(eventID) AS eventCount
    FROM GroupEvents 
    GROUP BY eventStartDate
) q ON q.eventStartDate = ge.eventStartDate
WHERE ge.eventStartDate = '10/14/2013'