Sql select help request

I have a database table with data as shown below:

Primary key | Column1       | Column2          | col3 | col4 |col5
---------------------------------------------------------------------

1           | Chicago Bulls | Michael Jordan   | 6'6  | aaaa | cccc

2           | Chicago Bulls | Scottie Pippen   | 6'8  | zzzz | 345

3           | Utah Jazz     | Malone           | 6'9  | vvvv | xcxc

4           | Orlando Magic | Hardaway         | 6'7  | xnnn | sdsd

I want to write a query that will retrieve all the different values ​​in column 1 and add the values ​​in column2 for each value of Column1. For example: the request should return

**Chicago Bulls | Michael Jordan, Scottie Pippen**

**Utah Jazz     | Malone**

**Orlando Magic | Hardaway**

I can write a query to retrieve all the different values ​​in column1, and then iterate over each individual value to get the added column 2 after some manipulations. Is it possible to do all the work in just one request? Please help with an example query. Thank.

+5
source share
4 answers

If you are using MySQL

select Column1, group_concat(Column2)
from t
group by Column1     
+5
source

If you are using SQL Server:

SELECT Column1,
stuff((
    SELECT ', ' + Column2
    FROM tableName as t1
    where t1.Column1 = t2.Column1
    FOR XML PATH('')
    ), 1, 2, '')
FROM tableName as t2
GROUP BY Column1

, Microsoft , , SQL Server...

1 , , ;)

+2

- ARRAY_AGG(), , , . SQL. GROUP_CONCAT() - , , .

ARRAY_AGG() GROUP_CONCAT() HSQLDB 2.0.1. http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#N12312

0

SQL Anywhere list() aggregation .

If you use SQL Server, then, in addition to a complex solution that abuses XML to some extent, you can write your own aggregation function in any .net language. And Microsoft's documentation for this feature uses the string concatenation example as an example.

0
source

All Articles