数据表TEST,内容如下:
Int A Int B Int C
1 1 123
1 1 234
1 3 12
2 1 56
2 1 88希望得到如下查询结构,如果写SQL语句?
Int A Int B VarChar C
1 1 123,234
1 3 12
2 1 56,88
Int A Int B Int C
1 1 123
1 1 234
1 3 12
2 1 56
2 1 88希望得到如下查询结构,如果写SQL语句?
Int A Int B VarChar C
1 1 123,234
1 3 12
2 1 56,88
C=stuff((select ','+c from tb where A=t.A and B=t.B for xml path('')),1,1,'')
from tb t
C=stuff((select ','+c from tb where A=t.A and B=t.B for xml path('')),1,1,'')
from tb t group by A,B
from TEST group by IntA,IntB
create function f_str(@a int ,@b int) returns varchar(1000)
as
begin select isnull(ltrim(c)+',','') from test where a=@a and b=@b
endgoselect a,b,dbo.f_str(a,b) from test
insert @tb select 1 ,1 ,123
insert @tb select 1 ,1 ,234
insert @tb select 1 ,3 ,12
insert @tb select 2 ,1 ,56
insert @tb select 2 ,1 ,88select a,B,replace((select C as 'data()' from @tb where a.A=a and a.B=B for xml path('')),' ',',') as c from @tb a
group by a,B/*
(1 行受影响)(1 行受影响)(1 行受影响)(1 行受影响)(1 行受影响)
a B c
----------- ----------- ---------
1 1 123,234
1 3 12
2 1 56,88(3 行受影响)
*/
create function f_str(@a int ,@b int) returns varchar(1000)
as
begin
declare @sql varchar(1000)
select isnull(@sql+',','')+ltrim(c) from test where a=@a and b=@b
endgoselect a,b,dbo.f_str(a,b) from test
、 FOR XML AUTO将结果转换为XML结构,例如对于上面2中的student表,执行以下语句SELECT *FROM [student]FOR XML AUTO
select A,B,C=left(C,len(C)-1)
from
(select t2.A,t2.B,
C=(select convert(varchar,C),',' from tb t1 where t1.A=t2.A and t1.B=t2.B for xml path(''))
from tb t2
group by t2.A,t2.B) t
数据:
id col
----- ------
1 a
1 b
2 a
2 b
2 c
期望得到结果:
id col
------ -----------
1 a,b
2 a,b,c
----------------------------------------------------------------------------------
-- Author : htl258(Tony)
-- Date : 2009-03-15 10:19:22
-- Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)
-- Blog : http://blog.csdn.net/htl258
----------------------------------------------------------------------------------
--> 生成测试数据表: [tb]
IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb] ([id] [int],[col] [nvarchar](10))
INSERT INTO [tb]
SELECT '1','a' UNION ALL
SELECT '1','b' UNION ALL
SELECT '2','a' UNION ALL
SELECT '2','b' UNION ALL
SELECT '2','c'
--SELECT * FROM [tb]
-->SQL查询如下:
--SQL2000以上版本:
IF OBJECT_ID('dbo.f_str')>0
DROP FUNCTION dbo.f_str
GO
CREATE FUNCTION dbo.f_str
(
@id INT
)
RETURNS VARCHAR(1000)
AS
BEGIN
DECLARE @r VARCHAR(1000)
SELECT @r = ISNULL(@r+',', '') + col
FROM tb
WHERE id = @id
RETURN @r
END
GO
SELECT DISTINCT id,dbo.f_str(id) col FROM tb
/*
id col
----------- ---------------------
1 a,b
2 a,b,c
(2 行受影响)
*/
-->SQL2005以上版本:
SELECT DISTINCT id,
STUFF(
(
SELECT ','+col
FROM tb t
WHERE id = tb.id FOR XML PATH('')
), 1, 1, ''
) AS col
FROM tb
/*
id col
----------- ---------------------
1 a,b
2 a,b,c
(2 行受影响)
*/
--*******************************************************************************************
表结构,数据如下:
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc 需要得到结果:
id values
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加) 1. 旧的解决方法(在sql server 2000中只能用函数解决。)
--=============================================================================
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
--1. 创建处理函数
CREATE FUNCTION dbo.f_strUnite(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @str varchar(8000)
SET @str = ''
SELECT @str = @str + ',' + value FROM tb WHERE id=@id
RETURN STUFF(@str, 1, 1, '')
END
GO
-- 调用函数
SELECt id, value = dbo.f_strUnite(id) FROM tb GROUP BY id
drop table tb
drop function dbo.f_strUnite
go
/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为 2 行)
*/
--===================================================================================
2. 新的解决方法(在sql server 2005中用OUTER APPLY等解决。)
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
-- 查询处理
SELECT * FROM(SELECT DISTINCT id FROM tb)A OUTER APPLY(
SELECT [values]= STUFF(REPLACE(REPLACE(
(
SELECT value FROM tb N
WHERE id = A.id
FOR XML AUTO
), ' <N value="', ','), '"/>', ''), 1, 1, '')
)N
drop table tb /*
id values
----------- -----------
1 aa,bb
2 aaa,bbb,ccc (2 行受影响)
*/ --SQL2005中的方法2
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go select id, [values]=stuff((select ','+[value] from tb t where id=tb.id for xml path('')), 1, 1, '')
from tb
group by id /*
id values
----------- --------------------
1 aa,bb
2 aaa,bbb,ccc (2 row(s) affected) */ drop table tb
C=stuff((select ','+c from tb where A=t.A and B=t.B for xml path('')),1,1,'')
from tb t group by A,B