一张表,字段A相同的,合并字段B的内容并输出,怎么办?有这样的语句吗?
比如表结构类似这样:
- SQL code
[ID] [int] IDENTITY(1,1) NOT NULL, [iNO] [nvarchar](20) NOT NULL, [NonMemo] [ntext] NULL,
其中 [iNO] 允许重复的,当我查询某一个 iNO 的时候,希望可以把 NonMemo 的内容合并(可以分段落吗?)输出。可以吗?
------解决方案--------------------
- SQL code
-- Title: 在SQL中分类合并数据行-- Author: dobear Mail(MSN): [email protected]-- Environment: Vista + SQL2005-- Date: 2008-04-22-- =============================================================================--1. 创建表,添加测试数据CREATE TABLE tb(id int, [value] varchar(10))INSERT tb SELECT 1, 'aa'UNION ALL SELECT 1, 'bb'UNION ALL SELECT 2, 'aaa'UNION ALL SELECT 2, 'bbb'UNION ALL SELECT 2, 'ccc'--SELECT * FROM tb/**//*id value----------- ----------1 aa1 bb2 aaa2 bbb2 ccc(5 row(s) affected)*/--2 在SQL2000只能用自定义函数实现----2.1 创建合并函数fn_strSum,根据id合并value值GOCREATE FUNCTION dbo.fn_strSum(@id int)RETURNS varchar(8000)ASBEGIN DECLARE @values varchar(8000) SET @values = '' SELECT @values = @values + ',' + value FROM tb WHERE [email protected] RETURN STUFF(@values, 1, 1, '')ENDGO-- 调用函数SELECT id, VALUE = dbo.fn_strSum(id) FROM tb GROUP BY idDROP FUNCTION dbo.fn_strSum----2.2 创建合并函数fn_strSum2,根据id合并value值GOCREATE FUNCTION dbo.fn_strSum2(@id int)RETURNS varchar(8000)ASBEGIN DECLARE @values varchar(8000) SELECT @values = isnull(@values + ',', '') + value FROM tb WHERE [email protected] RETURN @valuesENDGO-- 调用函数SELECT id, VALUE = dbo.fn_strSum2(id) FROM tb GROUP BY idDROP FUNCTION dbo.fn_strSum2--3 在SQL2005中的新解法----3.1 使用OUTER APPLYSELECT * 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----3.2 使用XMLSELECT id, [values]=STUFF((SELECT ','+[value] FROM tb t WHERE id=tb.id FOR XML PATH('')), 1, 1, '')FROM tbGROUP BY id--4 删除测试表tbdrop table tb/**//*id values----------- --------------------1 aa,bb2 aaa,bbb,ccc(2 row(s) affected)本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dobear_0922/archive/2008/04/22/2313839.aspx
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
1楼很多方法了。
------解决方案--------------------
- SQL code
/****************************************************************************************************************************************************** 合并分拆表数据 整理人:中国风(Roy) 日期:2008.06.06 ******************************************************************************************************************************************************/ --> --> (Roy)生成測試數據 if not object_id('Tab') is null drop table Tab Go Create table Tab([Col1] int,[Col2] nvarchar(1)) Insert Tab select 1,N'a' union all select 1,N'b' union all select 1,N'c' union all select 2,N'd' union all select 2,N'e' union all select 3,N'f' Go 合并表: SQL2000用函数: go if object_id('F_Str') is not null drop function F_Str go create function F_Str(@Col1 int) returns nvarchar(100) as begin declare @S nvarchar(100) select @S=isnull(@S+',','')+Col2 from Tab where [email protected] return @S end go Select distinct Col1,Col2=dbo.F_Str(Col1) from Tab go SQL2005用XML: 方法1: select a.Col1,Col2=stuff(b.Col2.value('/R[1]','nvarchar(max)'),1,1,'') from (select distinct COl1 from Tab) a Cross apply (select COl2=(select N','+Col2 from Tab where Col1=a.COl1 For XML PATH(''), ROOT('R'), TYPE))b 方法2: select a.Col1,COl2=replace(b.Col2.value('/Tab[1]','nvarchar(max)'),char(44)+char(32),char(44)) from (select distinct COl1 from Tab) a cross apply (select Col2=(select COl2 from Tab where COl1=a.COl1 FOR XML AUTO, TYPE) .query(' <Tab> {for $i in /Tab[position() <last()][email protected] return concat(string($i),",")} {concat("",string(/Tab[last()][email protected]))} </Tab>') )b SQL05用CTE: ;with roy as(select Col1,Col2,row=row_number()over(partition by COl1 order by COl1) from Tab) ,Roy2 as (select COl1,cast(COl2 as nvarchar(100))COl2,row from Roy where row=1 union all select a.Col1,cast(b.COl2+','+a.COl2 as nvarchar(100)),a.row from Roy a join Roy2 b on a.COl1=b.COl1 and a.row=b.row+1) select Col1,Col2 from Roy2 a where row=(select max(row) from roy where Col1=a.COl1) order by Col1 option (MAXRECURSION 0) 生成结果: /* Col1 COl2 ----------- ------------ 1 a,b,c 2 d,e 3 f (3 行受影响) */