基于日期的最新数据

时间:2017-12-01 06:06:10

标签: sql sql-server tsql

我有类似下面的数据

+---------------------+-------------------------+-------------+-----------+
|      Createdon      | Purchasing_Order_Number | Item_Number | Net_Price |
+---------------------+-------------------------+-------------+-----------+
| Nov 22 2017  4:31PM | 18M-101499              |           1 | 369.00    |
| Nov 22 2017  4:31PM | 18M-101499              |           2 | 95.00     |
| Nov 22 2017  4:31PM | 18M-101499              |           3 | 175.00    |
| Nov 22 2017  4:31PM | 18M-101499              |           4 | 10.00     |
| Nov 22 2017  4:31PM | 18M-101499              |           5 | 220.00    |
| Nov 30 2017  2:45PM | 18M-101499              |           1 | 450.00    |
| Nov 30 2017  2:45PM | 18M-101499              |           2 | 95.00     |
| Nov 30 2017  2:45PM | 18M-101499              |           3 | 175.00    |
| Nov 30 2017  2:45PM | 18M-101499              |           4 | 10.00     |
| Nov 30 2017  2:45PM | 18M-101499              |           5 | 220.00    |
+---------------------+-------------------------+-------------+-----------+

只需要最新值。输出应如下所示。

|      Createdon      | Purchasing_Order_Number | Item_Number | Net_Price |
+---------------------+-------------------------+-------------+-----------+
| Nov 30 2017  2:45PM | 18M-101499              |           1 |    450.00 |
| Nov 30 2017  2:45PM | 18M-101499              |           2 |     95.00 |
| Nov 30 2017  2:45PM | 18M-101499              |           3 |    175.00 |
| Nov 30 2017  2:45PM | 18M-101499              |           4 |     10.00 |
| Nov 30 2017  2:45PM | 18M-101499              |           5 |    220.00 |
+---------------------+-------------------------+-------------+-----------+

Select Max(Createdon),Purchasing_Order_Number,Item_Number,Net_Price 
from [dbo].[TEPurchase_Item_Structure] Where IsDeleted=0 
group by Purchasing_Order_Number,Item_Number,Net_Price
Order by Purchasing_Order_Number,Item_Number

但我得到了不同的价值观。

请帮我查询。

3 个答案:

答案 0 :(得分:0)

一个选项使用ROW_NUMBER()和项目编号上的分区来标识最新记录:

SELECT
    Createdon, Purchasing_Order_Number, Item_Number, Net_Price
FROM
(
    SELECT Createdon, Purchasing_Order_Number, Item_Number, Net_Price,
        ROW_NUMBER() OVER (PARTITION BY Purchasing_Order_Number, Item_Number
                           ORDER BY Createdon DESC) rn
    FROM [dbo].[TEPurchase_Item_Structure]
) t
WHERE t.rn = 1;

对于奖励积分,或者由于某种原因您无法使用分析函数,当前方法的问题是您只有一个级别GROUP BY查询。这只能找到每个项目的最新日期,但不能找到整个记录。如果您想继续使用当前的方法,那么您可以加入到您想到的查询中,例如

SELECT t1.*
FROM [dbo].[TEPurchase_Item_Structure] t1
INNER JOIN
(
    SELECT Purchasing_Order_Number, Item_Number, MAX(Createdon) AS max_created
    FROM [dbo].[TEPurchase_Item_Structure]
    GROUP BY Purchasing_Order_Number, Item_Number
) t2
    ON t1.Purchasing_Order_Number = t2.Purchasing_Order_Number AND
       t1.Item_Number = t2.Item_Number AND
       t1.Createdon = t2.max_created;

这两种方法实际上都有一个限制,即如果最新记录存在确切的联系,对于给定的项目编号,只会识别一个记录。我给出的连接方法并没有真正的解决方法,但我的第一个查询确实如此。在这种情况下,我们可以用ROW_NUMBER替换RANK,然后返回每个项目的最新记录的所有关系。

答案 1 :(得分:0)

我想更正您的查询,如下所示

刚刚将max()汇总添加到您的Net_Price

Select Max(Createdon),Purchasing_Order_Number,Item_Number,MAX(Net_Price)
from [dbo].[TEPurchase_Item_Structure] Where IsDeleted=0 
group by Purchasing_Order_Number,Item_Number
Order by Purchasing_Order_Number,Item_Number

其他方式是使用带row_number ()函数的窗口函数

with cte as
(
    SELECT *, row_number() over(partition by Purchasing_Order_Number, Item_Number order by Createdon desc) rn 
           FROM [dbo].[TEPurchase_Item_Structure]  Where IsDeleted=0 
) 
select * from cte where rn =1

答案 2 :(得分:0)

试试这个:

select Createdon, Purchasing_Order_Number, Item_Number, Net_Price from
(select Createdon,
       Purchasing_Order_Number,
       Item_Number,
       Net_Price
       rank() over (partition by Item_Number order by Createdon) as RN
from TABLE_NAME) as a
where RN = 1