1

格式化 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

谢谢你的帮助

4

2 回答 2

1

在 SQL Server 中,您可以使用窗口函数解决此问题:

SELECT
     eventStartDate
    ,eventTitle
    ,eventDesc
    ,eventTime
    ,count(eventID)over(partition by eventStartDate) as eventCount
FROM GroupEvents 
WHERE eventStartDate = '10/14/2013'
GROUP BY eventStartDate
于 2013-10-14T23:13:42.893 回答
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'
于 2013-10-14T23:27:03.620 回答