0

I'm working on this SQL and can't figure it out. Do I need to join the tables to get what I wanted? I basically need records that are the latest for each group.

SQL I have is

SELECT product_number, id, timestamp FROM table WHERE product_number IN (123,456)

This gives me

product_number | id | timestamp |
---------------------------------
       123     | 1  | 2022-01-05|
---------------------------------
       123     | 2  | 2022-05-04|
---------------------------------
       456     | 22 | 2022-03-05|
---------------------------------
       456     | 15 | 2022-08-12|
---------------------------------

However, I need the latest record for each product_number. And if I put GROUP BY product_number at the end of the query above I get the earliest records.

I'll be appreciated with any help : )

Henry
  • 199
  • 3
  • 3
  • 11

1 Answers1

0

You can do:

select product_number, max(timestamp)
  from table
 group by product_number

You won't have the id though.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292