查出的结果后``如何对nvchar字段 和NULL 空字段进行合并(几行的字段合并)?

xhunanpp 2004-05-12 11:50:23
A B C
----------------------------------------------
資訊部 軟件 小李
資訊部 編碼員 小王
行政部 秘書 小芳
資訊部 null d
行政部 s null
行政部 null e
行政部 null sd
行政部 null null
行政部 null null
行政部 sd null
資訊部 null null
資訊部 s null
資訊部 null null
-----------------------------------------------

结果:
資訊部 軟件,編碼員,秘書,s,sds 小李,小王,小芳,d,e,sd


谁能做到, 这分就是你的了,
...全文
120 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xhunanpp 2004-05-13
  • 打赏
  • 举报
回复
我用的表是#table 临时表, 不用在函数里直行啊, zjcxc(邹建)兄怎么办??
zjcxc 元老 2004-05-13
  • 打赏
  • 举报
回复
--如果不分组

--测试

--测试数据
create table 表(A varchar(10),B varchar(10),C varchar(10))
insert 表 select '資訊部','軟件' ,'小李'
union all select '資訊部','編碼員','小王'
union all select '行政部','秘書' ,'小芳'
union all select '資訊部',null ,'d'
union all select '行政部','s' ,null
union all select '行政部',null ,'e'
union all select '行政部',null ,'sd'
union all select '行政部',null ,null
union all select '行政部',null ,null
union all select '行政部','sd' ,null
union all select '資訊部',null ,null
union all select '資訊部','s' ,null
union all select '資訊部',null ,null
go

--创建处理函数
create function f_str(@b bit)
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
if @b=0
select @r=@r+','+B from 表 where b is not null
else
select @r=@r+','+C from 表 where c is not null
return(stuff(@r,1,1,''))
end
go

--调用实现查询
select A=max(A),B=dbo.f_str(0),C=dbo.f_str(1)
from 表
go

--删除测试
drop table 表
drop function f_str

/*--测试结果

A B C
---------- -------------------- --------------------
資訊部 軟件,編碼員,秘書,s,s 小李,小王,小芳,d,e,s

(所影响的行数为 1 行)

--*/
zjcxc 元老 2004-05-13
  • 打赏
  • 举报
回复
--测试

--测试数据
create table 表(A varchar(10),B varchar(10),C varchar(10))
insert 表 select '資訊部','軟件' ,'小李'
union all select '資訊部','編碼員','小王'
union all select '行政部','秘書' ,'小芳'
union all select '資訊部',null ,'d'
union all select '行政部','s' ,null
union all select '行政部',null ,'e'
union all select '行政部',null ,'sd'
union all select '行政部',null ,null
union all select '行政部',null ,null
union all select '行政部','sd' ,null
union all select '資訊部',null ,null
union all select '資訊部','s' ,null
union all select '資訊部',null ,null
go

--创建处理函数
create function f_str(@a varchar(10),@b bit)
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
if @b=0
select @r=@r+','+B from 表 where a=@a and b is not null
else
select @r=@r+','+C from 表 where a=@a and c is not null
return(stuff(@r,1,1,''))
end
go

--调用实现查询
select A,B=dbo.f_str(a,0),C=dbo.f_str(a,1)
from 表
group by A
go

--删除测试
drop table 表
drop function f_str

/*--测试结果

A B C
---------- -------------------- --------------------
行政部 秘書,s,sd 小芳,e,sd
資訊部 軟件,編碼員,s 小李,小王,d

(所影响的行数为 2 行)
--*/
zjcxc 元老 2004-05-13
  • 打赏
  • 举报
回复
--如果不分组

--测试

--测试数据
create table 表(A varchar(10),B varchar(10),C varchar(10))
insert 表 select '資訊部','軟件' ,'小李'
union all select '資訊部','編碼員','小王'
union all select '行政部','秘書' ,'小芳'
union all select '資訊部',null ,'d'
union all select '行政部','s' ,null
union all select '行政部',null ,'e'
union all select '行政部',null ,'sd'
union all select '行政部',null ,null
union all select '行政部',null ,null
union all select '行政部','sd' ,null
union all select '資訊部',null ,null
union all select '資訊部','s' ,null
union all select '資訊部',null ,null
go

--如果不分组
declare @s1 varchar(8000),@s2 varchar(8000)
select @s1='',@s2=''
select @s1=case when b is null then @s1 else @s1+','+b end
,@s2=case when c is null then @s2 else @s2+','+c end
from 表
select A=max(A),B=stuff(@s1,1,1,''),C=stuff(@s2,1,1,'')
from 表
go

--删除测试
drop table 表

/*--测试结果

A B C
---------- ----------------------- ---------------------------
資訊部 軟件,編碼員,秘書,s,sd,s 小李,小王,小芳,d,e,sd

(所影响的行数为 1 行)
--*/
zjcxc 元老 2004-05-13
  • 打赏
  • 举报
回复
--如果不分组

--测试

--测试数据
create table 表(A varchar(10),B varchar(10),C varchar(10))
insert 表 select '資訊部','軟件' ,'小李'
union all select '資訊部','編碼員','小王'
union all select '行政部','秘書' ,'小芳'
union all select '資訊部',null ,'d'
union all select '行政部','s' ,null
union all select '行政部',null ,'e'
union all select '行政部',null ,'sd'
union all select '行政部',null ,null
union all select '行政部',null ,null
union all select '行政部','sd' ,null
union all select '資訊部',null ,null
union all select '資訊部','s' ,null
union all select '資訊部',null ,null
go

--直接用临时表处理(如果要按a分组)
select a,b,c,aa=cast('' as varchar(8000)),bb=cast('' as varchar(8000))
into #t from 表 order by a
declare @a varchar(10),@r1 varchar(8000),@r2 varchar(8000)
update #t set @r1=case a when @a then
case when b is null then @r1
else case when @r1 is null then b else @r1+','+b end
end
else b end
,@r2=case a when @a then
case when b is null then @r2
else case when @r2 is null then b else @r2+','+b end
end
else b end
,aa=@r1,bb=@r2,@a=a

select a,b=max(aa),c=max(bb) from #t group by a

drop table #t
go

--删除测试
drop table 表

/*--测试结果

a b c
---------- -------------------- --------------------
行政部 s,sd,秘書 s,sd,秘書
資訊部 軟件,編碼員,s 軟件,編碼員,s

(所影响的行数为 2 行)
--*/
xhunanpp 2004-05-13
  • 打赏
  • 举报
回复
不能插入临时表啊,

下例有错,


--如果不分组

--测试

--测试数据
create table 表(A varchar(10),B varchar(10),C varchar(10))
insert 表 select '資訊部','軟件' ,'小李'
union all select '資訊部','編碼員','小王'
union all select '行政部','秘書' ,'小芳'
union all select '資訊部',null ,'d'
union all select '行政部','s' ,null
union all select '行政部',null ,'e'
union all select '行政部',null ,'sd'
union all select '行政部',null ,null
union all select '行政部',null ,null
union all select '行政部','sd' ,null
union all select '資訊部',null ,null
union all select '資訊部','s' ,null
union all select '資訊部',null ,null
go

--直接用临时表处理(如果要按a分组)
select a,b,c,aa=cast('' as varchar(8000)),bb=cast('' as varchar(8000))
into #t from 表 order by a
declare @a varchar(10),@r1 varchar(8000),@r2 varchar(8000)
update #t set @r1=case a when @a then
case when b is null then @r1
else case when @r1 is null then b else @r1+','+b end
end
else b end
,@r2=case a when @a then
case when b is null then @r2
else case when @r2 is null then b else @r2+','+b end
end
else b end
,aa=@r1,bb=@r2,@a=a

select a,b=max(aa),c=max(bb) into #tmp from #t group by a

drop table #t,#tmp
go

我要插入#tmp 临界表汇总数据``

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧