我有一到十二个月的行。
我有三列:Year
,Month
和总查询次数。
我想在底部添加一行来计算总查询次数:
SELECT
YEAR(date) AS [Year],
MONTH(date) AS [Month],
COUNT(*)
from EnquiryMaster
Group By YEAR(date), MONTH(date)
答案 0 :(得分:3)
尝试使用“WITH ROLLUP”sql的工具。
SELECT YEAR(date),
MONTH(date),
count(*) as data_nums
FROM EnquiryMaster
GROUP BY YEAR(date),MONTH(date) WITH ROLLUP
希望这会对你有所帮助
答案 1 :(得分: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)
答案 2 :(得分:-1)
SELECT YEAR(date) AS [Year],
MONTH(date) AS [Month],
COUNT(*)
from EnquiryMaster
Group By YEAR(date), MONTH(date)
在此查询中,您将获得特定年份的每月12条记录,因为您正在进行GROUP BY年和月,如果您只需要一年的一条记录,则必须从查询中删除MONTH(日期)。
查询将如下: -
SELECT YEAR(date) AS [Year],
MONTH(date) AS [Month],
COUNT(*)
from EnquiryMaster
Group By YEAR