上次装运日期的物品清单

时间:2017-07-20 12:43:32

标签: sql max getdate

我试图获取订单库存商品清单,以及供应商提供的最新确认交货时间。如果我尝试使用这样的查询,它将返回为该项目确认的每个提前期。我怎么才能让它只显示最后一个?

SELECT  

T0.DocNum AS Order,  
T1.ItemCode AS Item,  
T2.U_Internal_Code AS IntItem,  
CAST (((T2.onhand+T2.OnOrder)-T2.IsCommited) as int) AS Stock,  
T3.ShipDate

FROM 

ORDR T0

INNER JOIN RDR1 T1 ON T0.DocEntry = T1.DocEntry   
INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode  
INNER JOIN POR1 T3 ON T2.U_Bestelcode = T3.U_Bestelcode

WHERE T0.DocNum='1234' AND T3.ShipDate=(SELECT MAX (ShipDate) FROM POR1 WHERE T3.ShipDate = ShipDate)

ORDER BY T1.ItemCode

3 个答案:

答案 0 :(得分:0)

试试这个。前1只选择一行位于顶部。如果你写TOP 2,你可以从顶部获得2个数据。

对于SQL

SELECT TOP 1

T0.DocNum AS Order,  
T1.ItemCode AS Item,  
T2.U_Internal_Code AS IntItem,  
CAST (((T2.onhand+T2.OnOrder)-T2.IsCommited) as int) AS Stock,  
T3.ShipDate

FROM 

ORDR T0

INNER JOIN RDR1 T1 ON T0.DocEntry = T1.DocEntry   
INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode  
INNER JOIN POR1 T3 ON T2.U_Bestelcode = T3.U_Bestelcode

WHERE T0.DocNum='1234' AND T3.ShipDate=(SELECT MAX (ShipDate) FROM POR1 WHERE T3.ShipDate = ShipDate)

ORDER BY T1.ItemCode DESC;

对于MySQL

SELECT

    T0.DocNum AS Order,  
    T1.ItemCode AS Item,  
    T2.U_Internal_Code AS IntItem,  
    CAST (((T2.onhand+T2.OnOrder)-T2.IsCommited) as int) AS Stock,  
    T3.ShipDate

    FROM 

    ORDR T0

    INNER JOIN RDR1 T1 ON T0.DocEntry = T1.DocEntry   
    INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode  
    INNER JOIN POR1 T3 ON T2.U_Bestelcode = T3.U_Bestelcode

    WHERE T0.DocNum='1234' AND T3.ShipDate=(SELECT MAX (ShipDate) FROM POR1 WHERE T3.ShipDate = ShipDate)

    ORDER BY T1.ItemCode DESC
    LIMIT 1;

答案 1 :(得分:0)

通过对WHERE T3.ShipDate = ShipDate进行过滤,您将获得T3中每行的不同日期。

我认为您希望WHERE T3.Vendor= Vendor之类的内容能够找到供应商的最新日期

显然取决于您的数据库结构。

答案 2 :(得分:0)

更改此

WHERE T0.DocNum='1234' AND T3.ShipDate=(SELECT MAX (ShipDate) FROM POR1 WHERE T3.ShipDate = ShipDate)
ORDER BY T1.ItemCode

到这个

WHERE T0.DocNum='1234'
ORDER BY T3.ShipDate DESC, T1.ItemCode
limit 1