通过连接来自不同列的逗号分隔值来制作字符串

Make String by concatenating comma separate values from different columns

我有一个 table,其中所有列的值都是从另一个 SQL 插入的。 Table 结构如

CREATE TABLE temp_t
(
     catalog_item_code VARCHAR(1000),
     line_trans_type VARCHAR(1000),
     ref_trans_no VARCHAR(1000)
)

以及像

这样的值
INSERT INTO temp_t 
VALUES (' DR-1002-0001, DR-1010-0001, DR-20180926-05, RO-M-2059, HU-3154-2040,  JRCEKB-SS-1550-0001',' SALES, SALES, RETURN, RETURN, SALES, SPO',' 201681, 201681, 201666, 201660, 201681, 201648')

当前,当我们 运行 SELECT 以上 table 时,我们得到如下输出:

我想制作一个格式正确的字符串(line_trans_type +' - '+ref_trans_no +char(9)+ catalog_item_code) 每个逗号分隔值列(如果包含值)如

 SALES - 201681     DR-1002-0001
 SALES - 201681     DR-1010-0001
 RETURN - 201666    DR-20180926-05
 RETURN - 201660    RO-M-2059
 SALES - 201681     HU-3154-2040
 SPO - 201648       JRCEKB-SS-1550-0001

我尝试了以下查询,但它总是对每一行重复所有 catalog_item_code。

SELECT
    REPLACE(line_trans_type , ',', '-' + 
        REPLACE(ref_trans_no, ',', char(9) + REPLACE(catalog_item_code, ',', char(13))))
FROM
    temp_t

提前致谢。

你可以试试这个:

SELECT A.Data +' - '+B.Data+  char(9)+C.Data
FROM
(
    SELECT ROW_NUMBER() OVER(ORDER BY line_trans_type)ROWNO,  Data
    FROM
    (
        SELECT A.line_trans_type,  
        Split.a.value('.', 'VARCHAR(100)') AS Data  
        FROM  
        (
        SELECT line_trans_type,  
            CAST ('<M>' + REPLACE(line_trans_type, ',', '</M><M>') + '</M>' AS XML) AS Data  
        FROM  temp_t
        ) AS A CROSS APPLY Data.nodes ('/M') AS Split(a)
    )A 
)A 
JOIN
(
    SELECT ROW_NUMBER() OVER(ORDER BY ref_trans_no)ROWNO,  Data
    FROM
    (
        SELECT A.ref_trans_no,  
        Split.a.value('.', 'VARCHAR(100)') AS Data  
        FROM  
        (
        SELECT ref_trans_no,  
            CAST ('<M>' + REPLACE(ref_trans_no, ',', '</M><M>') + '</M>' AS XML) AS Data  
        FROM  temp_t
        ) AS A CROSS APPLY Data.nodes ('/M') AS Split(a)
    )B
)B ON A.ROWNO=B.ROWNO
JOIN
(
    SELECT ROW_NUMBER() OVER(ORDER BY catalog_item_code)ROWNO,  Data
    FROM
    (
        SELECT A.catalog_item_code,  
        Split.a.value('.', 'VARCHAR(100)') AS Data  
        FROM  
        (
        SELECT catalog_item_code,  
            CAST ('<M>' + REPLACE(catalog_item_code, ',', '</M><M>') + '</M>' AS XML) AS Data  
        FROM  temp_t
        ) AS A CROSS APPLY Data.nodes ('/M') AS Split(a)
    )C
)C ON A.ROWNO=C.ROWNO

以下是您在 SQL 2012 中执行此操作的一种方法。首先创建以下函数以将逗号分隔的字符串拆分为行:

create function dbo.SplitString(@string varchar(1000))
returns table
as
return(  
  select Item = y.i.value('(./text())[1]', 'varchar(1000)'), Row_Number() over(order by (select 1/0)) rn
  from ( 
    select x = Convert(xml, '<i>' + Replace(@string, ',', '</i><i>') + '</i>').query('.')
  ) as a cross apply x.nodes('i') as y(i)
);

然后您可以使用它来 return 您的 3 个字符串值作为 3 个集合可以连接:

with c as (
    select v.item, v.rn
    from temp_t t
    cross apply dbo.splitstring(t.catalog_item_code)v
), l as (
    select v.item, v.rn
    from temp_t t
    cross apply dbo.splitstring(t.line_trans_type)v
), r as (
    select v.item, v.rn
    from temp_t t
    cross apply dbo.splitstring(t.ref_trans_no)v
)
select Concat(l.item, ' - ', r.item, Char(9), c.item)
from c 
join l on c.rn=l.rn
join r on r.rn=c.rn

Example DB<>Fiddle