SELECT
MAX(id) id,
po_nbr
FROM
temp
GROUP BY
po_nbr
要获得关联的日期,您可以这样做(请注意,这意味着需要一个顺序编号):
SELECT
temp.id,
temp.po_nbr,
temp.crt_ts
FROM
temp
INNER JOIN (
SELECT MAX(id) id FROM temp GROUP BY po_nbr
) latest ON latest.id = temp.id
如果没有序列号,它将是:
SELECT
MAX(temp.id) id,
temp.po_nbr,
temp.crt_ts
FROM
temp INNER JOIN (
SELECT MAX(crt_ts) crt_ts, po_nbr
FROM temp i
GROUP BY po_nbr
) latest ON latest.crt_ts = temp.crt_ts AND latest.po_nbr = temp.po_nbr
GROUP BY
temp.po_nbr,
temp.crt_ts
GROUP BY
如果可以保证每个po_nbr
组没有两个相等的日期,则可以忽略。