[英]How to concatenate multiple columns values into a single value per unique row
我有一个包含重复结果集数据的表,例如列名和导致重复的值。
列名 | ColValue | UniqueIDpercombination |
---|---|---|
KitiD | K89901 | 1 |
成套工具 | 00900 | 1 |
KitiD | L7865 | 2 |
成套工具 | 00400 | 2 |
UPC | 345234 | 3 |
UPN | AVF | 3 |
...... 等等。
我想将 colname 和 colvalues 组合成每个 uniqueID 的一行,如下表所示。 列名值不固定。
列名 | ColValue | UniqueIDpercombination |
---|---|---|
KitiD - 套件 | K89901 - 00900 | 1 |
KitiD - 套件 | L7865 - 00400 | 2 |
UPC-UPN | 345234 AVF | 3 |
我尝试使用xml path
的stuff
,我没有得到所需的 output,下面是我尝试过的查询和 output。
SELECT STUFF
(
(
SELECT '-' + s.Colname +','
FROM ##resultset s
--group by uniqueidpercombination, colname
FOR XML PATH('')
),
1, 1, ''
) AS colname
,STUFF
(
(
SELECT '/' + s.colvalue
FROM ##resultsets
--group by uniqueidpercombination, colvalue
FOR XML PATH('')
),
1, 1, ''
) AS colvale
列名 | ColValue |
---|---|
kitid 套件 kitid 套件 | K89901-00900 -L7865-00400 |
对于如何解决这个问题,有任何的建议吗?。
使用STUFF
和XML PATH
的组合,然后GROUP BY
UniqueIDpercombination
列进行分组。
您的查询已接近尾声,但是,您需要先从表中SELECT
,然后在子查询中使用WHERE
子句实质上使用GROUP BY
将外部查询与内部子查询JOIN
。 这样您就不会在一行中返回整个表格。
还值得注意的是,我使用2
作为STUFF function的长度来说明' - '
分隔符中的空间。 您可以通过更改传递给STUFF
function 的 arguments 来使用所需的长度和分隔符。
STUFF ( character_expression , start , length , replaceWith_expression )
询问:
SELECT
STUFF((SELECT ' - ' + ColName
FROM sample_table b
WHERE a.UniqueIDpercombination = b.UniqueIDpercombination
FOR XML PATH('')), 1, 2, '') AS ColName,
STUFF((SELECT ' - ' + ColValue
FROM sample_table b
WHERE a.UniqueIDpercombination = b.UniqueIDpercombination
FOR XML PATH('')), 1, 2, '') AS ColValue,
a.UniqueIDpercombination
FROM sample_table a
GROUP BY UniqueIDpercombination
ORDER BY UniqueIDpercombination
db<>fiddle here 。
create table #temp (
ColName varchar (10),
ColValue varchar (10),
UniqueIDpercombination int
)
insert into #temp values ('KitiD', 'K89901', 1)
insert into #temp values ('Kit', '00900', 1 )
insert into #temp values ('KitiD', 'L7865', 2 )
insert into #temp values ('Kit', '00400', 2 )
insert into #temp values ('UPC', '345234', 3 )
insert into #temp values ('UPN', 'AVF', 3 )
select *,ROW_NUMBER() over(partition by uniqueIDpercombination order by uniqueIDpercombination) as rownum into #joinTable from #temp
第1步:如果你想使用连接表/单次使用
select distinct UniqueIDpercombination into #masterTable from #temp
select concat(b1.ColName,' - ',b2.ColName) ColName,CONCAT(b1.ColValue,' - ',b2.ColValue) ColValue,a.UniqueIDpercombination from #masterTable a
left join #joinTable b1 on a.UniqueIDpercombination = b1.UniqueIDpercombination and b1.rownum = 1
left join #joinTable b2 on a.UniqueIDpercombination = b2.UniqueIDpercombination and b2.rownum = 2
第2步:如果你想使用循环/如果重复的数量不仅是2行
select cast(NULL as varchar) ColName, cast(NULL as varchar) ColValue, UniqueIDpercombination into #masterTable1 from #temp
group by UniqueIDpercombination
declare @i int
set @i = 1
while @i <= (select max(rownum) from #joinTable)
begin
update a
set
a.ColName = case when a.ColName is null then b.ColName else (case when b.ColName is null then a.ColName else concat(a.ColName,' - ',b.ColName) end) end,
a.ColValue = case when a.ColValue is null then b.ColValue else (case when b.ColValue is null then a.ColValue else concat(a.ColValue,' - ',b.ColValue) end) end
from #masterTable1 a
left join #joinTable b on a.UniqueIDpercombination = b.UniqueIDpercombination and b.rownum = @i
set @i = @i +1
end
db <> 小提琴https://dbfiddle.uk/CQ3RSYCQ
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.