行中我有一到十二個月的時間。計算所有12個月的GrandTotal
我有三欄:Year
,Month
和查詢總數。
我想在底部添加一行來計算總計查詢:
SELECT
YEAR(date) AS [Year],
MONTH(date) AS [Month],
COUNT(*)
from EnquiryMaster
Group By YEAR(date), MONTH(date)
行中我有一到十二個月的時間。計算所有12個月的GrandTotal
我有三欄:Year
,Month
和查詢總數。
我想在底部添加一行來計算總計查詢:
SELECT
YEAR(date) AS [Year],
MONTH(date) AS [Month],
COUNT(*)
from EnquiryMaster
Group By YEAR(date), MONTH(date)
你或許應該,而顯示在用戶界面層的總數,但如果你真的想從SQL做到這一點服務器,你可以嘗試像
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(日期)。
查詢會是這樣: -
SELECT YEAR(date) AS [Year],
MONTH(date) AS [Month],
COUNT(*)
from EnquiryMaster
Group By YEAR
嘗試 「WITH ROLLUP」 SQL的設施。
SELECT YEAR(date),
MONTH(date),
count(*) as data_nums
FROM EnquiryMaster
GROUP BY YEAR(date),MONTH(date) WITH ROLLUP
希望這將有助於你
+1做總計UI層 –