I have a table liked this:
session | created_at | data
------------------------------------
12345 | 2018-10-10 08:10:10 | {}
13456 | 2018-10-11 09:12:13 | {}
12345 | 2018-10-10 08:12:13 | {}
14567 | 2018-08-09 10:11:12 | {}
14567 | 2018-08-09 03:09:11 | {}
12345 | 2018-10-10 08:24:12 | {}
There are thousands of rows. I'd like to group the rows by session but also order each group by timestamp. So if a new item is added to an existing session, that session group would be at the top.
I'd like to LIMIT this by groups of sessions. So ultimately I'd like to select groups of sessions and limit by group. This way, if I limit by 10, I'd potentially get 35 rows that comprises the 10 sessions.
Simply grouping by session, doesn't show all the rows, but 1 row per session. So this output I'd like to have like:
session | created_at | data
------------------------------------
14567 | 2018-12-09 10:11:12 | {}
14567 | 2018-08-09 03:09:11 | {}
------------------------------------
13456 | 2018-10-11 09:12:18 | {}
13456 | 2018-10-11 09:12:13 | {}
------------------------------------
12345 | 2018-10-10 08:24:12 | {}
12345 | 2018-10-10 08:12:13 | {}
12345 | 2018-10-10 08:10:10 | {}
13456 | 2018-10-11 09:12:18 | {}
comes from?select * from tablename order by session desc, created_at desc ;
...?select t.* from tablename as t join (select session from tablename group by session order by <whatever> limit 10) as s using (session) order by t.session desc, t.created_at desc ;