开发者

How do I combine more than one row of results based on a unique identifier

开发者 https://www.devze.com 2022-12-31 17:24 出处:网络
I have the following table of data: numberholiday 123456LDN 123456NYC 123456IRL 456789NYC 456789CHILE Basically each number can have up to 4 holidays; I need each number to only be displayed once,

I have the following table of data:

number  holiday
123456  LDN
123456  NYC
123456  IRL
456789  NYC
456789  CHILE

Basically each number can have up to 4 holidays; I need each number to only be displayed once, and then combine the holiday results into one field (rather than 3 in the example above for number 123456)

Ideally I want the tab开发者_如何学Cle to display the following:

number  holiday
123456  LDN, NYC, IRL
456789  NYC, CHILE

I can either output the results to Excel and maniplaute from there, or use sql and temp tables.


If you are using MySQL then you can use GROUP_CONCAT:

SELECT number, GROUP_CONCAT(holiday)
FROM table1
GROUP BY number

If you are using SQL Server you can emulate GROUP_CONCAT by using the FOR XML PATH hack. See this article for more details.

0

精彩评论

暂无评论...
验证码 换一张
取 消