简体   繁体   English

如何将多个列值连接成每个唯一行的单个值

[英]How to concatenate multiple columns values into a single value per unique row

I have a table with data of the duplicate result set, like the column names and the values which caused the duplicates.我有一个包含重复结果集数据的表,例如列名和导致重复的值。

ColName列名 ColValue ColValue UniqueIDpercombination UniqueIDpercombination
KitiD KitiD K89901 K89901 1 1
Kit成套工具 00900 00900 1 1
KitiD KitiD L7865 L7865 2 2
Kit成套工具 00400 00400 2 2
UPC UPC 345234 345234 3 3
UPN UPN AVF AVF 3 3

...... and so on. ...... 等等。

I would like to combine the colname and colvalues into a single row per uniqueID as shown in below table.我想将 colname 和 colvalues 组合成每个 uniqueID 的一行,如下表所示。 The column name values are not fixed.列名值不固定。

ColName列名 ColValue ColValue UniqueIDpercombination UniqueIDpercombination
KitiD - Kit KitiD - 套件 K89901 - 00900 K89901 - 00900 1 1
KitiD - Kit KitiD - 套件 L7865 - 00400 L7865 - 00400 2 2
UPC - UPN UPC-UPN 345234 AVF 345234 AVF 3 3

I tried using stuff with xml path , I did not get the desired output, below is the query which I tried and the output.我尝试使用xml pathstuff ,我没有得到所需的 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 
ColName列名 ColValue ColValue
kitid-kit-kitid-kit kitid 套件 kitid 套件 K89901-00900 -L7865-00400 K89901-00900 -L7865-00400

Any suggestions on how to fix this?.对于如何解决这个问题,有任何的建议吗?。

Use a combination of STUFF and XML PATH then GROUP BY the UniqueIDpercombination column.使用STUFFXML PATH的组合,然后GROUP BY UniqueIDpercombination列进行分组。

You were close with your query, however, you need to SELECT from your table first, then use a WHERE clause in your subqueries to essentially JOIN your outer query with your inner subqueries with a GROUP BY .您的查询已接近尾声,但是,您需要先从表中SELECT ,然后在子查询中使用WHERE子句实质上使用GROUP BY将外部查询与内部子查询JOIN That way you won't return the entire table in one single row.这样您就不会在一行中返回整个表格。

It's also worth noting, I used 2 as the length for the STUFF function to account for the space in your ' - ' delimiter.还值得注意的是,我使用2作为STUFF function的长度来说明' - '分隔符中的空间。 You can play around with the lengths and delimiters that you need by changing the arguments passed into the STUFF function.您可以通过更改传递给STUFF function 的 arguments 来使用所需的长度和分隔符。

STUFF ( character_expression , start , length , replaceWith_expression ) 

Query:询问:

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 . 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

step 1: if you want to use join table / single use第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

step 2: if you want to use loop / if the number of duplicate is not only 2 row第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 <> fiddle https://dbfiddle.uk/CQ3RSYCQ db <> 小提琴https://dbfiddle.uk/CQ3RSYCQ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM