join comma delimited data column

后端 未结 6 1489
日久生厌
日久生厌 2020-11-27 06:30

my table1 is :

T1

col1    col2
 C1     john
 C2     alex
 C3     piers
 C4     sara

and so table 2:

T2

         


        
6条回答
  •  旧巷少年郎
    2020-11-27 07:16

    This task cannot be solved with standard SQL. In Oracle I would write a stored function (PL/SQL) to parse the Name-ID-string (T2 col2) and resolve the names. Don't know if that's possible in Transact-SQL, but it's glorious inefficient.

    T2 is a badly designed, not normalized table. That's the problem. If you would normalize it, so that you have one line per Name-ID (col 2 in T2), you can get the list of names with a simple join of the two tables. To generate the desired output format (comma delimited) you need to write something else than SQL - maybe a stored procedure or something else that iterates over the resultset.

提交回复
热议问题