[英]SQL query to format the result
I have data in following format: 我有以下格式的数据:
24 Asian Fish
24 Atlantis
24 Bakery
24 Bistro
24 Bon Appetite
24 Camo
24 Fish Bones
25 Black
38 Black
38 Burgundy
38 Dark Green
38 Navy
38 Red
Could you please suggest what query can be used to format it in following way: 您能否建议使用哪种查询以以下方式设置其格式:
24 Asian Fish|Atlantis|Bakery|Bistro|Bon Appetite|Camo|Fish Bones
25 Black
38 Black|Burgundy|Dark Green|Navy|Red
This is for SQL Server 2005. 这是用于SQL Server 2005。
Since you didn't specify what database system you're using - here's one way to do it in SQL Server (2005 and up): 由于您未指定要使用的数据库系统,因此这是在SQL Server(2005及更高版本)中使用的一种方法:
SELECT
DISTINCT ID,
STUFF((SELECT '|' + t2.Fishy
FROM dbo.YourTable t2
WHERE t2.ID = t.ID
FOR XML PATH('')), 1, 1, '') 'Fishes'
FROM dbo.YourTable t
This will produce the output: 这将产生输出:
ID Fishes
24 Asian Fish|Atlantis|Bakery|Bistro|Bon Appetite|Camo|Fish Bones
25 Black
38 Black|Burgundy|Dark Green|Navy|Red
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.