A
A
AndNovak2015-11-04 22:22:18
SQL
AndNovak, 2015-11-04 22:22:18

How to group two columns?

d5179b63156546118bccf1727f3bc4e2.png
Tell me how to write a query that displays the name of the state (state) in one column, in the second all the cities
(city) that are in this state.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
nozzy, 2015-11-04
@AndNovak

MSSQL:

select state,
( select city+',' as 'data()' from dbo.test t2 where t1.state=t2.state for xml path('') )
from dbo.test t1
group by state

MySQL:
SELECT 
state,
GROUP_CONCAT(city)
FROM your_table
GROUP BY state

D
Dmitry Entelis, 2015-11-04
@DmitriyEntelis

select state, group_concat(city separator ",") from ... group by state

dev.mysql.com/doc/refman/5.7/en/group-by-functions...

A
Aleksey Ratnikov, 2015-11-04
@mahoho

You have SQL Server, judging by the screenshot from SQL Server Management Studio. There is no group_concat(), the simplest solution can be found here: stackoverflow.com/questions/451415/simulating-grou...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question