[英]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.