获取最大值或组合重复值的数据

时间:2012-02-28 03:14:19

标签: sql sql-server-2008 tsql group-concat

这与我的previous Qn

相关联
Table A: 
ID  Rank Name 
1   100  Name1 
1    45  Name2 
2    60  Name3 
2    42  Name4 
2    88 Name5 
3    50 name6
3    50 name7

Table B: 
ID FileName 
1  fn1 
2  fn2 
3  fn3

我想要的是

1 fn1 name1 
2 fn2 name5 
3 fn3  name6,name7

这是我的代码没有处理上面的duplciate排名,所以我得到两行值3。

select B.*,A.Name
LEFT JOIN ( SELECT A.Id, MAX(A.Rank)as Rank 
    FROM A 
    GROUP BY A.Id
    ) AS NewA 
            JOIN A 
                on A.Rank = NewA.Rank
                    AND A.ID = NewA.Id
    on NewA.ID = B.ID 

当Id的排名相同时,如何修改此选项以使用逗号分隔A.name?

由于

3 个答案:

答案 0 :(得分:2)

select B.ID,
       B.[FileName],
       stuff((select ',' + A1.name
              from TableA as A1
              where B.ID = A1.ID and
                    A1.[Rank] = (select max(A2.[Rank])
                                 from TableA as A2                   
                                 where A1.ID = A2.ID)
              for xml path(''), type).value('.', 'varchar(max)'), 1, 1, '') as Names
from TableB as B

试试SE-Data

答案 1 :(得分:1)

;WITH x AS
(
    SELECT ID, [Rank], Name, 
      rn = DENSE_RANK() OVER (PARTITION BY ID ORDER BY [Rank] DESC)
      FROM dbo.TableA
), y AS
(
  SELECT x.ID, x.Name, f.[Filename]
    FROM x INNER JOIN dbo.TableB AS f
    ON x.ID = f.ID WHERE x.rn = 1
)
SELECT DISTINCT ID, [Filename], Names = STUFF((SELECT ',' + name
    FROM y AS y2 WHERE y.ID = y2.ID
    FOR XML PATH(''), TYPE).value('.[1]', 'nvarchar(max)'), 1, 1, '')
FROM y
ORDER BY ID;

答案 2 :(得分:0)

您需要使用CTE来解决此问题。

这应该直接插入SQL并使用给出的数据作为示例。显然,您需要更新实际数据的查询,因为这可能不是您的真实架构

create table #tableA (ID int, [rank] int, name varchar(max))
insert into #tableA values(1,100,'Name1')
insert into #tableA values(1,45,'Name2')
insert into #tableA values(2,60,'Name3')
insert into #tableA values(2,42,'Name4')
insert into #tableA values(2,88,'Name5')
insert into #tableA values(3,50,'Name6')
insert into #tableA values(3,50,'Name7')

create table #tableB (ID int, [FileName] varchar(max))
insert into #tableB values(1,'fn1')
insert into #tableB values(2,'fn2')
insert into #tableB values(3,'fn3')

SELECT B.*,A.Name, ROW_NUMBER() OVER (PARTITION BY A.ID ORDER BY A.[rank]) AS RowNum
INTO #RankedTable
FROM #tableB as B
    LEFT JOIN ( 
        SELECT A.Id, MAX(A.Rank)as Rank 
        FROM #tableA AS A 
        GROUP BY A.Id
        ) AS NewA 
    JOIN #tableA AS A
        on A.Rank = NewA.Rank
            AND A.ID = NewA.Id
    on NewA.ID = B.ID 

;WITH ConcatenationCTE (ID, [FileName], Name, RowNum)
AS
(
  SELECT ID, [FileName], Name, RowNum 
  FROM #RankedTable
  WHERE RowNum = 1
  UNION ALL
  SELECT #RankedTable.ID, #RankedTable.[FileName],
          ConcatenationCTE.Name + ',' + #RankedTable.Name AS Name, 
          #RankedTable.RowNum 
  FROM #RankedTable
      JOIN ConcatenationCTE 
          ON ConcatenationCTE.ID = #RankedTable.ID 
              AND ConcatenationCTE.RowNum +1 = #RankedTable.RowNum
)
SELECT ConcatenationCTE.ID, [FileName], Name 
FROM ConcatenationCTE 
        JOIN 
     (SELECT ID, MAX(RowNum) AS RowNum 
      FROM ConcatenationCTE GROUP BY ID) AS FinalValues 
        ON FinalValues.ID = ConcatenationCTE.ID 
            AND FinalValues.RowNum = ConcatenationCTE.RowNum