本文介紹了如何在 T-SQL 中獲取所有月份的值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個查詢,我想顯示一年中的所有月份,無論該月份是否有銷售.我知道問題出在 Group By,但如何更改查詢以便我不需要它?
I have a query that I would like to display all months for the year regardless if they have sales for that month or not. I know the issue is with the Group By, but how do I change the query so I don't need it?
SELECT
ISNULL(MONTH(sd.SBINDT),0) AS MonthSold,
SUM(sd.SBQSHP) AS QtySold
FROM
dbo.SalesData sd
WHERE
sd.SBTYPE = 'O'
AND sd.SBITEM = @Part
AND YEAR(sd.SBINDT) = @Year
AND sd.DefaultLocation = @Location
GROUP BY MONTH(sd.SBINDT)
推薦答案
試試這個:-
SELECT M.Months AS MonthSold,D.QtySold as QtySold
FROM ( SELECT distinct(MONTH(sd.SBINDT))as Months from dbo.SalesData sd)M
left join
(
SELECT MONTH(sd.SBINDT) AS MonthSold,SUM(sd.SBQSHP) AS QtySold
FROM dbo.SalesData sd
WHERE sd.SBTYPE = 'O'
AND sd.SBITEM = @Part
AND YEAR(sd.SBINDT) = @Year
AND sd.DefaultLocation = @Location
GROUP BY MONTH(sd.SBINDT)
)D
ON M.Months = D.MonthSold
這篇關于如何在 T-SQL 中獲取所有月份的值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!