【问题标题】:Tricky T-SQL Query. Concatenate multiple values from a column between master/child tables棘手的 T-SQL 查询。连接主/子表之间的列中的多个值
【发布时间】:2011-10-03 22:31:52
【问题描述】:

我有如下三张表:

主表

+----------+-------------+
| MasterId | MasterName  |
+----------+-------------+
| 1        | Master 1    |
| 2        | Master 2    |
| 3        | Master 3    |
| 4        | Master 4    |
+----------+-------------+

子表

+----------+-------------+
| ChildId | ChildName    |
+----------+-------------+
| 1        | Child 1     |
| 2        | Child 2     |
| 3        | Child 3     |
| 4        | Child 4     |
+----------+-------------+

链接表

+----------+-----------------------+
| Id       | MasterId    | ChldId  |
+----------+-----------------------+
| 1        |  1          | 1       | 
| 2        |  2          | 1       | 
| 3        |  3          | 2       | 
| 4        |  4          | 3       | 
+----------+-----------------------+

一个孩子可以与多个主人链接,LinkTable 包含此详细信息。我想要一个查询来选择以下内容:

1, 'Child 1', 'Master 1, Master 2', '1,2'
2, 'Child 2', 'Master 2', '2'
3, 'Child 3', 'Master 3', '3'

是否可以不使用循环或调用附加函数,使用COALESCESTUFF、递归CTE 等?

【问题讨论】:

  • 为什么您的LinkTable 表包含两个重复的MasterId 列?
  • @S.Serpooshan 可能意味着该表的 ID。现在连这个都不记得了!

标签: sql tsql concatenation xquery sql-server-group-concat


【解决方案1】:

虽然@polishchuk 的解决方案有效,但我在下面有自己的版本:

SELECT  ChildId, ChildName, ISNULL(mastDetail.MasterIds,'')MasterIds, 
        ISNULL(mastDetail.MasterNames, '') MasterNames
FROM    ChildrenTable sub
OUTER APPLY
(
    SELECT 
    STUFF( 
            (SELECT  ',' + mast.MasterName
             FROM    MasterTable mast
             INNER JOIN LinkTable link ON (mast.MasterId = link.MasterId AND 
                   link.ChildId = child.ChildId)
             FOR XML PATH('')
            ), 1,1,''
        ) AS MasterNames,
    STUFF( 
            (SELECT  ',' + CAST(mast.MasterId AS VARCHAR)
             FROM    MasterTable mast
             INNER JOIN LinkTable link ON (mast.MasterId = link.MasterId AND 
             link.ChildId = child.ChildId)
             FOR XML PATH('')
            ), 1,1,''
        ) AS MasterIds
) AS mastDetail

【讨论】:

    【解决方案2】:

    要连接字符串,您可以使用此方法:How to concatenate all strings from a certain column for each group

    测试数据:

    declare @masterTable table(MasterId int identity, MasterName varchar(max))
    insert @masterTable (MasterName) values('m1'), ('m2'), ('m3'), ('m4')
    
    declare @childrenTable table(ChildId int identity, ChildName varchar(max))
    insert @childrenTable (ChildName) values('c1'), ('c2'), ('c3'), ('c4')
    
    declare @LinkTable table(MasterId1 int, MasterId2 int, ChildId int)
    insert @LinkTable values(1,1,1), (2,2,1), (3,3,2), (4,4,3)
    

    查询:

    select t.*
    from
    (
        select c.ChildId, c.ChildName
    
            , STUFF((
                select ', ' + m.MasterName
                from
                (
                    select l.MasterId1
                    from @LinkTable l
                    where l.ChildId = c.ChildId
    
                    union
    
                    select l.MasterId2
                    from @LinkTable l
                    where l.ChildId = c.ChildId
                )t
                join @masterTable m on m.MasterId = t.MasterId1
                for xml path(''), type
            ).value('.', 'varchar(max)'), 1, 2, '') [names]
    
            , STUFF((
                select ', ' + cast(t.MasterId1 as varchar(max))
                from
                (
                    select l.MasterId1
                    from @LinkTable l
                    where l.ChildId = c.ChildId
    
                    union
    
                    select l.MasterId2
                    from @LinkTable l
                    where l.ChildId = c.ChildId
                )t
                for xml path(''), type
            ).value('.', 'varchar(max)'), 1, 2, '') [ids]
        from @childrenTable c
    )t
    where t.ids is not null
    

    输出:

    ----------- --- -------- ------
    1           c1  m1, m2   1, 2
    2           c2  m3       3
    3           c3  m4       4
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      相关资源
      最近更新 更多