日期:2014-05-17 浏览次数:20529 次
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-03-31 00:30:59
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86)
-- Jun 17 2011 00:57:23
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([Id] int,[AllTestId] int,[Answer] varchar(4))
insert [huang]
select 137,47,'中文' union all
select 138,47,'英文'
--------------开始查询--------------------------
select MIN(a.[Id])[Id],a.[AllTestId],
stuff((select '
------解决方案--------------------
'+[Answer] from [huang] b
where b.[AllTestId]=a.[AllTestId]
for xml path('')),1,1,'') '[Answer]'
from [huang] a
group by a.[AllTestId]
----------------结果----------------------------
/*
Id AllTestId [Answer]
----------- ----------- ----------------------------------------------------------------------------------------------------------------
137 47 中文
------解决方案--------------------
英文
*/