How to extract info based on the latest row

前端 未结 1 1756
北海茫月
北海茫月 2021-01-23 00:44

I have two tables:-

TABLE A :-

ORNO  DEL   PONO    QTY
801   123   1       80
801   123   2       60
801   123   3       70
801   151   1               


        
相关标签:
1条回答
  • 2021-01-23 01:27
    select b.*, y.QTY
    from
    (
        select a.ORNO, a.PONO, MAX(a.DEL) [max]
        from @tA a
        group by a.ORNO, a.PONO
    )x
    join @tA y on y.ORNO = x.ORNO and y.PONO = x.PONO and y.DEL = x.max
    join @tB b on b.ORNO = y.ORNO and b.PONO = y.PONO
    

    Output:

    ORNO        PONO        STATUS      ITEM       QTY
    ----------- ----------- ----------- ---------- -----------
    801         1           12          APPLE      95
    801         2           12          ORANGE     60
    801         3           12          MANGO      75
    802         1           22          PEAR       50
    802         2           22          KIWI       55
    802         3           22          MELON      30
    
    0 讨论(0)
提交回复
热议问题