我有一到十二个月的时间。
我有三列:Year
和Month
查询总数。
我想在底部添加一行来计算查询的总数:
SELECT
YEAR(date) AS [Year],
MONTH(date) AS [Month],
COUNT(*)
from EnquiryMaster
Group By YEAR(date), MONTH(date)
我有一到十二个月的时间。
我有三列:Year
和Month
查询总数。
我想在底部添加一行来计算查询的总数:
SELECT
YEAR(date) AS [Year],
MONTH(date) AS [Month],
COUNT(*)
from EnquiryMaster
Group By YEAR(date), MONTH(date)
试试 sql 的“WITH ROLLUP”功能。
SELECT YEAR(date),
MONTH(date),
count(*) as data_nums
FROM EnquiryMaster
GROUP BY YEAR(date),MONTH(date) WITH ROLLUP
希望这对你有帮助
您可能宁愿在 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)
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