Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of values in GROUP_BY clause?

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 
like image 649
Jagmal Avatar asked Jan 29 '09 13:01

Jagmal


People also ask

How do I group values in a column in SQL?

The SQL GROUP BY clause allows us to group individual data based on defined criteria. You can group individual data by one or more table columns. In order to do the grouping properly, you often need to apply aggregate functions to the column(s) within the SQL SELECT statement.

Can we include all the columns in GROUP BY clause?

The GROUP BY clause must contain all the columns except the one which is used inside the group function.

How do I SUM by group in SQL?

SUM() function with group by The aggregate functions summarize the table data. Once the rows are divided into groups, the aggregate functions are applied in order to return just one value per group. It is better to identify each summary row by including the GROUP BY clause in the query resulst.


2 Answers

I know that in MySQL there is GROUP_CONCAT and in Sybase I think it's LIST as stated in another answer:

SELECT id, LIST(data||', ') FROM yourtable GROUP BY id 
like image 111
lpfavreau Avatar answered Sep 19 '22 23:09

lpfavreau


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.

like image 29
puppylpg Avatar answered Sep 22 '22 23:09

puppylpg