1

I have a Postgres table that looks like this:

id a
1 1
2 1
3 2
4 3
5 3
6 4
7 4

I want to get rows that only have the biggest id (primary key) where column a values are different but all in a list of numbers, i.e. expected output for rows where a are 1 and 3:

id a
2 1
5 3

How to write a SQL statement to fill this need?

SELECT * 
FROM table 
WHERE a IN (1, 3) ...

Any advice would be appreciated, thank you in advance!

lemon
  • 14,875
  • 6
  • 18
  • 38
Changkun
  • 1,502
  • 1
  • 14
  • 29
  • Why are only 1 and 3 values considered for the field "*a*"? – lemon Oct 19 '22 at 14:46
  • 1
    `Select MAX(id), a FROM Table WHERE a in (1,3) GROUP BY a` not sure about Postgres, but in MSSQL if you group by A first - then Max on `Id` will get Max from the group. – Dawood Awan Oct 19 '22 at 14:51

0 Answers0