0

I have a table like this.

deviceid    temperature     timestamp
1           35              222222222
1           36              333333333
2           35              222222222
2           35              333333333
2           35              444444444
3           37              222222222
3           39              333333333
3           41              444444444

Say I need:

200 rows that have deviceid 1

200 rows that have deviceid 2

200 rows that have deviceid 3.

But I need the 200 most recent rows of each, timestamp.

How do I limit the results to 200 of each distinct deviceid's most recent timestamped temperatures?

I kept finding answers that use TOP, MAX, and DISTINCT, but I can't seem to put it all together.

Taylor Evanson
  • 386
  • 4
  • 16
  • 1
    Can you try implemented the answer here and show us your attempt? https://stackoverflow.com/questions/2129693/using-limit-within-group-by-to-get-n-results-per-group – Jared C Dec 22 '18 at 21:34
  • Just change `categori_id` to `deviceid` and `RAND()` to `timestamp DESC` in the dupe. – Nick Dec 22 '18 at 22:13

0 Answers0