SQL查询根据交易日期加入数据以获得本月的& s&上个月的成本

时间:2015-02-18 00:03:13

标签: sql sql-server tsql

我有一个包含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

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

如果您使用的是SQL Server 2012或更高版本,则可以使用LEADLAG

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或更高版本且无法使用这些新功能,那么带有ROW_NUMBER的CTE也将为您提供以下功能:

;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美元,那么5月的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