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
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.
The GROUP BY clause must contain all the columns except the one which is used inside the group function.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With