sql select row with max value if multiple exists if not select the available row
i have this sql table and i want to query the rows with max date if multiple ones exists with the same num if theres no multiple colomns just get the existing row for example on this table
Num | date |
---|---|
1 | 1990/01/01 |
1 | 2020/12/31 |
2 | 1990/01/01 |
2 | 2020/12/31 |
3 | Null |
the result should be :
Num | date |
---|---|
1 | 2020/12/31 |
2 | 2020/12/31 |
3 | Null |
Solution 1:
try the below, but it does depend on which db you are using:
select num, max(date) from XTableNameX group by num