i have table:
╔════╤════════════╗
║ id │ date ║
╠════╪════════════╣
║ 1 │ 2001-08-01 ║
╟────┼────────────╢
║ 1 │ 2001-09-02 ║
╟────┼────────────╢
║ 1 │ 2001-09-02 ║
╟────┼────────────╢
║ 1 │ 2001-09-01 ║
╟────┼────────────╢
║ 2 │ 2000-01-01 ║
╟────┼────────────╢
║ 3 │ 2000-09-01 ║
╚════╧════════════╝
and want to receive a max date using code:
SELECT tt.*
FROM data tt
inner join(
SELECT id, max(data) as lastDate
FROM data
GROUP BY id) groupedtt
ON tt.id = groupedtt.id AND tt.data = groupedtt.lastDate
but via this code i get duplicate values like
╔════╤════════════╗
║ id │ date ║
╠════╪════════════╣
║ 1 │ 2001-09-02 ║
╟────┼────────────╢
║ 1 │ 2001-09-02 ║
╟────┼────────────╢
║ 2 │ 2000-01-01 ║
╟────┼────────────╢
║ 3 │ 2000-09-01 ║
╚════╧════════════╝
what should i change to get table without dupliates like this:
╔════╤════════════╗
║ id │ date ║
╠════╪════════════╣
║ 1 │ 2001-09-02 ║
╟────┼────────────╢
║ 2 │ 2000-01-01 ║
╟────┼────────────╢
║ 3 │ 2000-09-01 ║
╚════╧════════════╝
i'm using ms sql server
SELECT DISTINCT.GROUP BY idint the main queryJOINin the first place?