MS SQL Server - WITH ROLLUP&通过...分组

时间:2014-12-15 07:41:15

标签: sql-server group-by

QQ:

我有一个如下代码可以正常工作,但我还要添加WITH ROLLUP& WITH CUBE选项(2种不同的情况。)。知道怎么样?在评论我的想法,不幸的是不起作用......

SELECT 
    --ISNULL(EventDate, 'Total'), DOES NOT WORK
    DATEPART(YEAR, EventDate) AS EventYear,
    DATEPART(MONTH, EventDate) AS EventMonth,
    COUNT (*) AS [Number of events]
FROM 
    tblEvent
GROUP BY
    DATEPART(YEAR, EventDate),
    DATEPART(MONTH, EventDate) -- WITH ROLLUP DOESN NOT WORK
ORDER BY
    DATEPART(YEAR, EventDate)DESC, 
    DATEPART(MONTH, EventDate)DESC

1 个答案:

答案 0 :(得分:0)

尝试:

SELECT 
    --ISNULL(EventDate, 'Total'), DOES NOT WORK
    DATEPART(YEAR, EventDate) AS EventYear,
    DATEPART(MONTH, EventDate) AS EventMonth,
    COUNT (*) AS [Number of events]
FROM 
    tblEvent
GROUP BY ROLLUP(
    DATEPART(YEAR, EventDate),
    DATEPART(MONTH, EventDate))
ORDER BY
    DATEPART(YEAR, EventDate)DESC, 
    DATEPART(MONTH, EventDate)DESC