如何把多行数据合并成一行?

wufanglu wufanglu 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
sql2k, 我想把以下数据过滤,然后合并成一行,寻求最优的方法。
declare @t table(a varchar(30), b varchar(30), c varchar(30), d varchar(30))
insert into @t select '1' , '', '', ''
insert into @t select '' , '2', '', ''
insert into @t select '1' , '', '3', '4'
insert into @t select '' , '', '', '4'
insert into @t select '' , '', '2', ''
insert into @t select 'x' , '', '2', ''
insert into @t select '' , 'y', '2', ''
insert into @t select '' , 'y', '2', 'z'

--显示的原始数据
select * from @t

--这些是我过滤出来的数据,只是里边有很多的空格
select a,b,c,d from @t
where 1=1
and (a='1' or a='')
and (b='2' or b='')
and (c='3' or c='')
and (d='4' or d='')

--想得到这个效果,即打扁成一行
select a='1', b='2', c='3', d='4'
...全文

相关推荐

评论 15
收藏 已收藏
分享
返回顶部
wufanglu
MS-SQL Server 34,876 成员 254,635 内容