日期:2014-05-17 浏览次数:20870 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-11-05 14:11:48
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([列1] varchar(6),[列2] int)
insert [huang]
select '红警',372 union all
select '红警',437 union all
select '大富翁',778
--------------开始查询--------------------------
select a.[列1],
stuff((select ','+CONVERT(VARCHAR(10),[列2]) from [huang] b
where b.[列1]=a.[列1]
for xml path('')),1,1,'') '列2'
from [huang] a
group by a.[列1]
----------------结果----------------------------
/*
列1 列2
------ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
大富翁 778
红警 372,437
*/