How to get list of values in GROUP_BY clause?

2019-01-22 12:51发布

If I have data like this in a table

id   data
--   ----
1    1
1    2
1    3
2    4
2    5
3    6
3    4

How do I get results like this in a query (on sybase server)?

id   data
--   ----
1    1, 2, 3
2    4, 5
3    6, 4

9条回答
戒情不戒烟
2楼-- · 2019-01-22 13:23

In mysql, use

SELECT id, GROUP_CONCAT(data)
 FROM yourtable
 GROUP BY id

or use your custom separator:

SELECT id, GROUP_CONCAT(data SEPARATOR ', ')
 FROM yourtable
 GROUP BY id

see GROUP_CONCAT.

查看更多
冷血范
4楼-- · 2019-01-22 13:27

In PL/SQL you can do it by:

SELECT id, LISTAGG(data, ',') WITHIN GROUP(ORDER BY 0) "data"
  FROM yourtable
 GROUP BY id
查看更多
登录 后发表回答