[英]SQL date scenario for getting previous month's data to-date on the 1st of month, then drop last month
[英]SQL query to join data based on transaction date to get this month's & previous month's cost
我有一個交易數據集,其中包含諸如product_id,cost_unit,date等列。產品的成本逐月變化。 我想要一個具有以下結構的表
Product | Cost_from_this_Month | Cost_from_Previous_month
每個月一個產品可以有多行,但是該月的成本是相同的。 即,費用固定為一個月。 如何構造查詢以獲得所需的結果?
我知道如何獲取最新費用:
SELECT * FROM (
SELECT Product, Cost_Unit
, Date
, zrank = row_number() OVER ( PARTITION BY Product ORDER BY Date DESC)
FROM MY_Table
) a where a.zrank = 1
誰能幫我?
如果您使用的是SQL Server 2012或更高版本,條件聚合和lag()
函數如何?
select year(date), month(date), max(cost_unit) as thismonthcost,
lag(max(cost_unit)) over (order by year(date), month(date)) as lastmonthcost
from my_table
group by year(date), month(date)
如果您使用的是SQL Server 2012或更高版本,則可以使用LEAD
或LAG
:
DECLARE @Products TABLE (
Product VARCHAR(100),
Cost_Unit MONEY,
[Date] DATETIME
)
INSERT @Products VALUES
('Widget', 10, '1/1/2014'),
('Widget', 12, '2/1/2014'),
('Widget', 15, '3/1/2014'),
('Widget', 17, '4/1/2014'),
('Gizmo', 20, '1/1/2014'),
('Gizmo', 25, '2/1/2014'),
('Gizmo', 26, '3/1/2014'),
('Gizmo', 27, '4/1/2014')
SELECT
Product,
[Date],
Products.Cost_Unit AS [Cost_This_Month],
LAG(Products.Cost_Unit) OVER (PARTITION BY Product ORDER BY [Date]) AS [Cost_Last_Month]
FROM @Products Products
ORDER BY Product, [Date] DESC
這里的輸出產生:
Product Date Cost_This_Month Cost_Last_Month
--------------- ----------------------- --------------------- ---------------------
Gizmo 2014-04-01 00:00:00.000 27.00 26.00
Gizmo 2014-03-01 00:00:00.000 26.00 25.00
Gizmo 2014-02-01 00:00:00.000 25.00 20.00
Gizmo 2014-01-01 00:00:00.000 20.00 NULL
Widget 2014-04-01 00:00:00.000 17.00 15.00
Widget 2014-03-01 00:00:00.000 15.00 12.00
Widget 2014-02-01 00:00:00.000 12.00 10.00
Widget 2014-01-01 00:00:00.000 10.00 NULL
如果您未使用SQL 2012或更高版本,並且無法使用這些新功能,則CROW ROW_NUMBER還將為您提供以下功能:
;WITH CTE AS (
SELECT
Product,
[Date],
Cost_Unit,
ROW_NUMBER() OVER (PARTITION BY Product ORDER BY [Date] DESC) AS RowNum
FROM @Products
)
SELECT
CM.Product,
CM.Product,
CM.Cost_Unit AS [Cost_This_Month],
LM.Cost_Unit AS [Cost_Last_Month]
FROM CTE CM
LEFT OUTER JOIN CTE LM
ON CM.Product = LM.Product
AND CM.RowNum = LM.RowNum - 1
更新更新以考慮一個月中的多個事務。 假設該月的最后一筆交易是您當月使用的費用(例如,如果5/1上為$ 10,5 / 5上為$ 11,則May的Cost_Unit為$ 11)。
-- LAG Solution
SELECT
Product,
[Date],
Products.Cost_Unit AS [Cost_This_Month],
LAG(Products.Cost_Unit) OVER (
PARTITION BY Product ORDER BY [Date]) AS [Cost_Last_Month]
FROM (
SELECT *, ROW_NUMBER() OVER (
PARTITION BY Product, YEAR([Date]), MONTH([Date])
ORDER BY [Date] DESC) AS RowNum
FROM @Products
) Products
WHERE RowNum = 1
ORDER BY Product, [Date] DESC
-- ROW_NUMBER() Solution
;WITH CTE AS (
SELECT
Product,
[Date],
Cost_Unit,
ROW_NUMBER() OVER (PARTITION BY Product ORDER BY [Date] DESC) AS RowNum
FROM (
SELECT *, ROW_NUMBER() OVER (
PARTITION BY Product, YEAR([Date]), MONTH([Date])
ORDER BY [Date] DESC) AS RowNum
FROM @Products
) Products
WHERE Products.RowNum = 1
)
SELECT
CM.Product,
CM.[Date],
CM.Cost_Unit AS [Cost_This_Month],
LM.Cost_Unit AS [Cost_Last_Month]
FROM CTE CM
LEFT OUTER JOIN CTE LM
ON CM.Product = LM.Product
AND CM.RowNum = LM.RowNum - 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.