我的问题是存在欺骗行为是因为人们在我们的运输订单中出错并且创建了另一个跟踪#来修复订单。所以我总是需要获得订单的最后一个副本,所以我得到正确的跟踪#。
我试过这个:
SELECT OrderNum, TrackingNumber, CreatedDate, CreatedBy, Carrier
FROM FPDWDB1.Ship.dbo.ShippingLog
GROUP BY OrderNum
我收到以下错误:
列'FPDWDB1.Ship.dbo.ShippingLog.TrackingNumber'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
答案 0 :(得分:0)
Select OrderNum, TrackingNumber, CreatedDate, CreatedBy, Carrier
FROM (
SELECT OrderNum, TrackingNumber, CreatedDate, CreatedBy, Carrier
,ROW_NUMBER() OVER (PARTITION BY OrderNum ORDER BY CreatedDate DESC) rn
FROM FPDWDB1.Ship.dbo.ShippingLog
) A Where rn = 1
OR
WITH A AS (
SELECT OrderNum, TrackingNumber, CreatedDate, CreatedBy, Carrier
,ROW_NUMBER() OVER (PARTITION BY OrderNum ORDER BY CreatedDate DESC) rn
FROM FPDWDB1.Ship.dbo.ShippingLog
)
Select OrderNum, TrackingNumber, CreatedDate, CreatedBy, Carrier
FROM A
Where rn = 1