sql问题 求大手帮忙额
省 市 县区 县区名称 人员
13 1301130100市局1测试员1
13 1301130100市局1测试员2
13 1301130101第一分局测试员3
13 1302130200市局2测试员4
13 1302130200市局2测试员5
如何合并显示为
省 市 县区 县区名称 人员
13 1301130100市局测试员1
测试员2
130101第一分局测试员3
1302130200市局2测试员4
测试员5
就是一样的数据不再显示。。。
求大手帮忙额。
不知各位明白意思没有?
[解决办法]
use DBTestgoif OBJECT_ID('tabTest') is not null drop table tabTestgocreate table tabTest(Province int,City int,Area int,AreaName nvarchar(50),UserName nvarchar(50))insert into tabTestselect 13,1301,130100,'市局1','测试员1' union allselect 13,1301,130100,'市局1','测试员2' union allselect 13,1301,130101,'第一分局','测试员2' union allselect 13,1302,130200,'市局2','测试员4' union allselect 13,1302,130200,'市局2','测试员4'gocreate function GetNames(@Province int,@City int,@Area int,@AreaName nvarchar(50))returns nvarchar(800)asbegindeclare @s nvarchar(800) set @s= '' select @s=@s + ','+UserName from tabTest where Province=@Province and City=@City and Area=@Area and AreaName=@AreaNamereturn substring(@s,2,len(@s)-1)endgoSELECT Province,City,Area,AreaName,dbo.GetNames(Province,City,Area,AreaName) as NamesFROM tabTestgroup by Province,City,Area,AreaName
[解决办法]
--> 测试数据:[tbl]if object_id('[tbl]') is not null drop table [tbl]create table [tbl]([省] int,[市] int,[县区] int,[县区名称] varchar(8),[人员] varchar(7))insert [tbl]select 13,1301,130100,'市局1','测试员1' union allselect 13,1301,130100,'市局1','测试员2' union allselect 13,1301,130101,'第一分局','测试员3' union allselect 13,1302,130200,'市局2','测试员4' union allselect 13,1302,130200,'市局2','测试员5'SELECT *FROM (SELECT DISTINCT [省],[市],[县区],[县区名称] FROM tbl)AOUTER APPLY( SELECT [人员]= STUFF(REPLACE(REPLACE( ( SELECT [人员] FROM tbl N WHERE [省]= A.[省] AND [市]=A.市 AND [县区]=A.县区 AND [县区名称]=A.县区名称 FOR XML AUTO ), '<N 人员="', ' '), '"/>', ''), 1, 1, ''))N/*省 市 县区 县区名称 人员13 1301 130100 市局1 测试员1 测试员213 1301 130101 第一分局 测试员313 1302 130200 市局2 测试员4 测试员5*/
[解决办法]
create table [tb](cola varchar(8),colb varchar(8),colc varchar(8),cold varchar(8),cole varchar(7))insert [tb]select '13','1301','130100','市局1','测试员1' union allselect '13','1301','130100','市局1','测试员2' union allselect '13','1301','130101','第一分局','测试员3' union allselect '13','1302','130200','市局2','测试员4' union allselect '13','1302','130200','市局2','测试员5'goselect *,rid=identity(int,1,1) into #tbfrom tborder by cola,colb,colc,coldselect (case when (select count(*) from #tb where cola=t.cola and rid<=t.rid)=1 then cola else '' end) cola, (case when (select count(*) from #tb where cola=t.cola and colb=t.colb and rid<=t.rid)=1 then colb else '' end) colb, (case when (select count(*) from #tb where cola=t.cola and colb=t.colb and colc=t.colc and rid<=t.rid)=1 then colc else '' end) colc, (case when (select count(*) from #tb where cola=t.cola and colb=t.colb and colc=t.colc and cold=t.cold and rid<=t.rid)=1 then cold else '' end) cold, colefrom #tb tdrop table tb,#tb/*****************************************cola colb colc cold cole-------- -------- -------- -------- -------13 1301 130100 市局1 测试员1 测试员2 130101 第一分局 测试员3 1302 130200 市局2 测试员4 测试员5(5 行受影响)
[解决办法]
--省 市 县区 县区名称 人员 --13 1301 130100 市局1 测试员1 --13 1301 130100 市局1 测试员2 --13 1301 130101 第一分局 测试员3 --13 1302 130200 市局2 测试员4--13 1302 130200 市局2 测试员5if OBJECT_ID('tb')is not nulldrop table tbgocreate table tb (省 int,市 int,县区 int,县区名称 varchar(30),人员 varchar(30))insert into tb values(13, 1301, 130100 ,'市局1','测试员1' )insert into tb values(13 ,1301 ,130100 ,'市局1','测试员2' )insert into tb values(13 ,1301 ,130101 ,'第一分局','测试员3' )insert into tb values(13 ,1302 ,130200 ,'市局2','测试员4' )insert into tb values(13 ,1302 ,130200 ,'市局2','测试员5' ) --如何合并显示为--省 市 县区 县区名称 人员 --13 1301 130100 市局 测试员1 -- 测试员2 -- 130101 第一分局 测试员3-- 1302 130200 市局2 测试员4-- 测试员5 ;with ct as (select *,rn=ROW_NUMBER()over(order by 人员) from tb ) select 省 ,市 ,县区 ,县区名称 ,人员 from ct where rn=1 union allselect (select case 省 when t.省 then null else 省 end from ct where rn=t.rn+1 ) as 省 , (select case 市 when t.市 then null else 市 end from ct where rn=t.rn+1 ) as 市 , (select case 县区 when t.县区 then null else 县区 end from ct where rn=t.rn+1 ) as 县区 , (select case 县区名称 when t.县区名称 then null else 县区名称 end from ct where rn=t.rn+1 ) as 县区名称 , (select case 人员 when t.人员 then null else 人员 end from ct where rn=t.rn+1 ) as 人员 from ct t where t.rn<5 省 市 县区 县区名称 人员----------- ----------- ----------- ------------------------------ ------------------------------13 1301 130100 市局1 测试员1NULL NULL NULL NULL 测试员2NULL NULL 130101 第一分局 测试员3NULL 1302 130200 市局2 测试员4NULL NULL NULL NULL 测试员5(5 行受影响)