After doing some joins and stuff I have table in the following format
name session_id status time
abc 1 10
xyz 2 11
jack 2 10
zuck 1 10
paul 1 10
Now I want my results to be grouped like this
session_id name+status time
1 abc:10, zuck:10, paul:10
2 xyz:11, jack:11
and the result set to be sorted by time, I'm little confused how to achieve this using group by
if your db is oracle, you can try. Afer oracle 11g r2, you can also use listagg
select session_id, wm_concat(name_status), time from
(
select session_id, (name+':'+status) as name_status, time
from stuff
)
group by session_id
order by time
SELECT
session_id
, GROUP_CONCAT( CONCAT(name, ':', COALESCE(status, -1)
SEPARATOR ', '
ORDER BY `time`
) AS name_status
, MIN(`time`) AS min_time --- or MAX(`time`)
FROM
TableX
GROUP BY
session_id
ORDER BY
min_time
You could use time
instead of MIN(time)
, if all rows with same session_id
have same time
(if time
is functionally dependent on session_id
). If that's not the case and you use time
, you'll not get consistent results on that column.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.