您的查詢可以更改爲包含月份的名稱。綁定到圖表時,可以使用名稱而不是數字,以便軸可以顯示月份名稱而不是月份編號。
這裏是如何得到該月的名稱的例子:
SELECT Year = DATEPART(YEAR, date1),
date1 = DATEPART(MONTH, date1),
SUM(qty) as TotalQTY,
CASE -- Case statement translates the integer of the month into the name of the month
WHEN DATEPART(MONTH, date1) = 1 THEN 'January'
WHEN DATEPART(MONTH, date1) = 2 THEN 'February'
WHEN DATEPART(MONTH, date1) = 3 THEN 'March'
WHEN DATEPART(MONTH, date1) = 4 THEN 'April'
WHEN DATEPART(MONTH, date1) = 5 THEN 'May'
WHEN DATEPART(MONTH, date1) = 6 THEN 'June'
WHEN DATEPART(MONTH, date1) = 7 THEN 'July'
WHEN DATEPART(MONTH, date1) = 8 THEN 'August'
WHEN DATEPART(MONTH, date1) = 9 THEN 'September'
WHEN DATEPART(MONTH, date1) = 10 THEN 'October'
WHEN DATEPART(MONTH, date1) = 11 THEN 'November'
WHEN DATEPART(MONTH, date1) = 12 THEN 'December'
ELSE ''
END AS 'name_of_month'
FROM TableMain
WHERE Tablemain.date1 between @startdate and @enddate and Tablemain.fillerparam in(@fillerparam) and Tablemain.filler2 in (@filler2)
GROUP BY DATEPART(MONTH, date1), DATEPART(YEAR, date1),
CASE -- Add the case statement to the GroupBy so that it still groups as expected
WHEN DATEPART(MONTH, date1) = 1 THEN 'January'
WHEN DATEPART(MONTH, date1) = 2 THEN 'February'
WHEN DATEPART(MONTH, date1) = 3 THEN 'March'
WHEN DATEPART(MONTH, date1) = 4 THEN 'April'
WHEN DATEPART(MONTH, date1) = 5 THEN 'May'
WHEN DATEPART(MONTH, date1) = 6 THEN 'June'
WHEN DATEPART(MONTH, date1) = 7 THEN 'July'
WHEN DATEPART(MONTH, date1) = 8 THEN 'August'
WHEN DATEPART(MONTH, date1) = 9 THEN 'September'
WHEN DATEPART(MONTH, date1) = 10 THEN 'October'
WHEN DATEPART(MONTH, date1) = 11 THEN 'November'
WHEN DATEPART(MONTH, date1) = 12 THEN 'December'
ELSE ''
END
ORDER BY date1 -- Added order by to make sure that records are sorted in the report by date and not by alphabetic name of month
您也可以看看這個帖子的方式來獲取月份名稱,而不使用case語句:Convert Month Number to Month Name Function in SQL
謝謝!我沒有想過用案例。唯一出現問題的部分是第二個'CASE'之後的'AS'name_of_month'',但除此之外,它完美無瑕。 – PrincessTrevor 2014-10-08 18:45:10
該示例已更新,以從第二個「CASE」中刪除「AS'name_of_month'''。 – 2014-10-08 19:43:15