1

i have a table in aws athena as shown below. enter image description here

and i want to get rows for each device id with highest timestamp.

Piotr Findeisen
  • 17,029
  • 2
  • 46
  • 75
Robin gupta
  • 166
  • 2
  • 10

1 Answers1

2

With ansi-sql you just need a sub-select with a group by and aggregation function:

    SELECT distinct last_record.*
FROM changesets AS c
INNER JOIN 
    (SELECT c2.user,
         MAX(c2.created_at) AS max_timestamp
    FROM changesets AS c2
    GROUP BY  c2.user) AS last_record
    ON c.user = last_record.user

And of course don't forget if deviceId is not the primary-key or non-unique this will return multiple rows from last_record so you will need the distinct

vfrank66
  • 969
  • 12
  • 24