oracle 根据分组取日期最大的那条数据

SELECT
    po_line_id,
    consigned_flag,
    need_by_date 
FROM
    (
    SELECT
        t.rn,
        t.consigned_flag,
        t.need_by_date,
        t.po_line_id 
    FROM
        (
        SELECT
            ROW_NUMBER ( ) OVER ( PARTITION BY plla.po_line_id ORDER BY plla.need_by_date DESC ) rn,
            plla.po_line_id,
            plla.consigned_flag,
            plla.need_by_date 
        FROM
            po_line_locations_all plla 
        ) t 
    WHERE
        t.rn = 1 
    ) m 
WHERE
    m.po_line_id = 44150;

 

注:PARTITION BY 为分组字段

       ORDER BY 根据日期排序  

       desc 倒序,ROW_NUMBER() OVER(PARTITION BY plla.po_line_id ORDER BY plla.need_by_date DESC) rn 为序号

      rn=1取倒序排序的第一条

  

 

posted @ 2021-01-29 14:40  哎丫丫呀喂  阅读(3581)  评论(0编辑  收藏  举报