我想從查詢中獲取前6個月的數據。如何在SQL中只獲取特定月份的數據
即我必須只分組前六個月。
假設當前月份是六月話,我只希望一月份的數據&也是我不希望所有其他的前一個月比
月誰能幫助我這個
SELECT
so_date
FROM
RS_Sells_Invoice_Info_Master SIIM
LEFT OUTER JOIN
RS_Sell_Order_Master AS SM ON SM.sell_order_no = SIIM.sell_order_no
LEFT OUTER JOIN
RS_Sell_Order_Mapping AS SOM ON SOM.sell_order_no = SIIM.sell_order_no AND SIIM.product_id = SOM.product_id
LEFT OUTER JOIN
RS_Inventory_Master AS IM ON IM.product_id = SIIM.product_id
where
so_date between CAST(DATEADD(month, DATEDIFF(month, 0, so_date)-5, 0)AS DATE) and CAST(DATEADD(month, DATEDIFF(month, 0, so_date)-4, 0)AS DATE)
「過去六個月」比較是不一樣的「只希望一月份的數據」。你想要什麼 - 從1月份開始的所有數據,** 1月份(僅1月份)的所有數據**,或者可能有多個解釋的「前6個月」(當前日曆月和前5個月,前6個日曆月到當前日期,今天減去6個月到其他日期)。 – alroc
重複,至少[這個問題](http://stackoverflow.com/questions/1424999/get-the-records-of-last-month-in-sql-server),雖然沒有任何現有的答案是理想的(現在添加更好的答案)。 –