0

I have following table structure:

id    customer_id    version_num
1         1              1
2         1              2
3         2              1
4         2              2
5         3              1
6         3              2
7         3              3

I want results to be displayed as:

id    customer_id    version_num
2         1              2
4         2              2
7         3              3

I'm able to get the following results:

id    customer_id    version_num
1         1              1
3         2              1
5         3              1

Query that I used:

select * from TABLE group by customer_id

I don't know how to make use of latest(version_num) in my query.

kojow7
  • 10,308
  • 17
  • 80
  • 135
Shrikant Kakani
  • 1,511
  • 2
  • 17
  • 37

1 Answers1

1
SELECT Y.id, Y.customer_id, Y.version_num
FROM YourTable Y
JOIN (SELECT customer_id, MAX(version_num) max_v
      FROM YourTable
      GROUP  customer_id
     ) T
  ON Y.customer_id = T.customer_id
 AND Y.version_num = T.max_v;
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118