0

可能重复:
通过唯一标识符组合查询结果的行?

我在 Sql Server 2008 中有两个表,如下所示

表格1

       DealNum            DealVresion            Value

        1000                   1                 100
        1000                   2                 200
        1000                   3                 150  
        1000                   4                 130
        1001                   2                  70
        1003                   5                 160
        1003                   0                 120                    

表 2

     DealNum            DealVersion              Name 

     1000                    1                   John
     1000                    1                   Bob
     1000                    2                   John
     1000                    2                   Merle
     1000                    3                   Bob
     1000                    3                   Bob
     1000                    5                   Bob
     1001                    2                   Smith
     1001                    2                   stone
     1002                    8                   Andrew
     1003                    5                   Smith
     1003                    0                   Martin
     1003                    0                   Narine

现在我想要基于这两个表的左连接

     (T1.Dealnum= T2.Dealnum) and (T1.Deal Version = T2.Deal Version)

我希望将 Table2 中的名称连接起来,这样它就不会在 DealNum 中显示任何重复项。

结果要求:

         DeaLNum           Deal Version          Value          Name

          1000                  1                 100           Jhon,Bob
          1000                  2                 200           John,Merle
          1000                  3                 150           Bob
          1000                  4                 130           NULL
          1001                  2                 70            Smith,Stone                  
          1003                  0                 120           Martin,Narine
          1003                  5                 160           Smith

它必须连接 DealNum 和版本的名称列。如果相同的 dealNum 和 Version 具有相同的名称,则无需连接(例如:1000 - 3)

提前谢谢哈利

4

3 回答 3

1

您可以通过创建用户定义的函数来做到这一点,不知道是否可以在单个 select 语句中完成:

    create function getNameList(@dealnum int, @dealversion int) returns nvarchar(max)
    begin
    declare @name varchar(max)

    select  @name = coalesce(@name + ', ','') + name
    from    (select distinct name from table2 where dealnum = @dealnum and dealversion = @dealversion) t1

    return @name
    end

然后:

    select  distinct
            t1.dealNum,
            t1.dealVersion,
            t1.value,
            dbo.getNameList(t1.dealNum, t1.dealversion)
    from    table1 t1 join table2 t2 on t1.dealnum = t2.dealnum and t1.dealversion = t2.dealversion
于 2012-07-17T13:55:43.277 回答
1

尝试这个:

  with cte as (
  select distinct T1.DealNum [T1_DealNum],T1.DealVresion [T1_DealVresion],
 T1.Value [T1_Value],
  T2.DealNum [T2_DealNum],T2.DealVresion [T2_DealVresion],T2.Name [T2_Name]
  from Table1 T1 left outer join Table2 T2
  on (T1.Dealnum= T2.Dealnum) and (T1.DealVresion = T2.DealVresion)
  )
  select [T1_DealNum],[T1_DealVresion],[Name]=  

  STUFF((SELECT ', ' + [T2_Name]
       FROM cte b 
       WHERE a.[T1_DealNum]= b.[T2_DealNum]
       and a.[T1_DealVresion] = b.[T2_DealVresion]
      FOR XML PATH('')), 1, 2, '')
FROM cte a
GROUP BY [T1_DealNum],[T1_DealVresion]
order by [T1_DealNum],[T1_DealVresion]
于 2012-07-17T14:11:06.477 回答
1
;WITH y AS
(
  SELECT DealNum, DealVersion, Name = STUFF((SELECT ',' + y2.Name
    FROM dbo.Table2 AS y2 
    WHERE y2.DealNum = y.DealNum AND y2.DealVersion = y.DealVersion
    GROUP BY y2.Name
    FOR XML PATH(''), 
    TYPE).value(N'./text()[1]', N'nvarchar(max)'), 1, 1, '') 
  FROM dbo.Table2 AS y
)
SELECT x.DealNum, x.DealVersion, x.Value, y.Name
FROM dbo.Table1 AS x 
LEFT OUTER JOIN y 
ON x.DealNum = y.DealNum
AND x.DealVersion = y.DealVersion
GROUP BY x.DealNum, x.DealVersion, x.Value, y.Name;
于 2012-07-17T13:56:42.920 回答