1

我有一到十二个月的时间。

我有三列:YearMonth查询总数。

我想在底部添加一行来计算查询的总数:

SELECT 
YEAR(date) AS [Year],
MONTH(date) AS [Month],
COUNT(*) 
from EnquiryMaster 
Group By YEAR(date), MONTH(date)
4

3 回答 3

3

试试 sql 的“WITH ROLLUP”功能。

SELECT YEAR(date),
MONTH(date),
count(*) as data_nums
FROM EnquiryMaster
GROUP BY YEAR(date),MONTH(date) WITH ROLLUP

希望这对你有帮助

于 2012-12-14T07:03:38.147 回答
2

您可能宁愿在 UI 层中显示总数,但如果您真的希望从 SQL Server 执行此操作,您可以尝试类似

SELECT  YEAR(date) AS [Year],
        MONTH(date) AS [Month],
        COUNT(*) 
from    EnquiryMaster
Group By    YEAR(date), 
            MONTH(date)
UNION ALL
SELECT  YEAR(date) AS [Year],
        NULL,
        COUNT(*) 
from    EnquiryMaster
Group By    YEAR(date)

另外,也许看看Summarizing Data Using ROLLUP

于 2012-12-14T06:38:50.517 回答
-1
     SELECT YEAR(date) AS [Year],
            MONTH(date) AS [Month],
            COUNT(*) 
     from EnquiryMaster 
     Group By YEAR(date), MONTH(date)

在此查询中,您将获得特定年份每个月的 12 条记录,因为您正在执行 GROUP BY 年和月,如果您一年只想要一条记录,则必须从查询中删除 MONTH(date)。

查询将是这样的: -

     SELECT YEAR(date) AS [Year],
            MONTH(date) AS [Month],
            COUNT(*) 
     from EnquiryMaster 
     Group By YEAR  
于 2012-12-14T06:44:03.490 回答