How can I get the highlighted rows from the table below in SQL? (Distinct rows based on User name with the highest Version are highlighted)
In case you need plain text table:
+----+-----------+---+ | 1 | John | 1 | +----+-----------+---+ | 2 | Brad | 1 | +----+-----------+---+ | 3 | Brad | 3 | +----+-----------+---+ | 4 | Brad | 2 | +----+-----------+---+ | 5 | Jenny | 1 | +----+-----------+---+ | 6 | Jenny | 2 | +----+-----------+---+ | 7 | Nick | 4 | +----+-----------+---+ | 8 | Nick | 1 | +----+-----------+---+ | 9 | Nick | 3 | +----+-----------+---+ | 10 | Nick | 2 | +----+-----------+---+ | 11 | Chris | 1 | +----+-----------+---+ | 12 | Nicole | 2 | +----+-----------+---+ | 13 | Nicole | 1 | +----+-----------+---+ | 14 | James | 1 | +----+-----------+---+ | 15 | Christine | 1 | +----+-----------+---+
What I have so far is (works for one user)
SELECT USER, VERSION FROM TABLE WHERE USER = 'Brad' AND VERSION = (SELECT MAX(VERSION ) FROM TABLE WHERE USER= 'Brad')
Advertisement
Answer
this might help you :
select id, user, version from ( select id, user, version, row_number() over (partition by user order by version desc) rownum from yourtable ) as t where t.rownum = 1