从SQL DENSE_Rank连接字符串结果

时间:2013-08-01 03:27:59

标签: sql string concatenation dense-rank

我有一种情况需要将DENSE_RANK函数的SQL结果“连接”到一个字符串中,我不知道另外一个SQL函数会这样做。

问题是我最终会从DENSE_RANK函数中获得相同排名的多行,这可以是预期的,例如:

ID    Info    Total    Rank
1     A       2        1
1     B       2        1
1     C       1        2
2     D       2        1
2     E       1        2
3     F       1        1

我想要实现的结果如下所示,因为ID 1有2个具有相同排名的Info,我想将它们“连接”成一个字符串(以逗号分隔):

ID    Info1    Info2    
1     A,B      C
2     D        E
3     F        G

我目前有以下代码给我DENSE_RANK的结果,但是我不知道如何找到我的最终结果集,我有一个“连接”字符串,我尝试了一些不同的技术并通过stackoverflow很好看,但我找不到一种方法可以使它工作。

SELECT t1.ID
  ,t2.Info1
  ,t3.Info2
  ,t4.Info3
FROM table1 t1 WITH (NOLOCK)
LEFT JOIN (SELECT t2.ID
             ,t2.Info AS Info1
             ,t2.Total
       FROM (SELECT t2.ID
                   ,t2.Info
                   ,t2.Total
                   ,DENSE_RANK() OVER (PARTITION BY t2.ID ORDER BY t2.Total DESC) AS DENSE_RANK
             FROM table2 t2 WITH (NOLOCK)) t2
             WHERE DENSE_RANK = 1) AS t2 ON t1.ID = t2.ID

LEFT JOIN (SELECT t3.ID
             ,t3.Info AS Info2
             ,t3.Total
       FROM (SELECT t3.ID
                   ,t3.Info
                   ,t3.Total
                   ,DENSE_RANK() OVER (PARTITION BY t3.ID ORDER BY t3.Total DESC) AS DENSE_RANK
             FROM table3 t3 WITH (NOLOCK)) t3
             WHERE DENSE_RANK = 2) AS t3 ON t1.ID = t3.ID

LEFT JOIN (SELECT t4.ID
             ,t4.Info AS Info3
             ,t4.Total
       FROM (SELECT t4.ID
                   ,t4.Info
                   ,t4.Total
                   ,DENSE_RANK() OVER (PARTITION BY t4.ID ORDER BY t4.Total DESC) AS DENSE_RANK
             FROM table4 t4 WITH (NOLOCK)) t4
             WHERE DENSE_RANK = 3) AS t4 ON t1.ID = t4.ID

ORDER BY t1.ID ASC

2 个答案:

答案 0 :(得分:0)

这是你要找的那个吗?

使用CTE or temporary table存储您的结果,说它是temp

SELECT SELECT MAX(t1.ID)
  ,STUFF( (SELECT ','+Info3 FROM temp t
           WHERE t.Rank=A.RankFOR XML PATH(''))  ,  1 , 1 , '' )
FROM temp A
GROUP BY A.Rank

此查询为您提供逗号分隔值Info3具有相同等级

答案 1 :(得分:0)

@Nithesh

我修改了我的代码以提升排名:

SELECT t1.ID
  ,t2.Info1
  ,t2.DENSE_RANK AS [Rank1]
  ,t3.Info2
  ,t3.DENSE_RANK AS [Rank2]
  ,t4.Info3
  ,t4.DENSE_RANK AS [Rank3]
INTO temp
FROM table1 t1 WITH (NOLOCK)
LEFT JOIN (SELECT t2.ID
             ,t2.Info AS Info1
             ,t2.Total
             ,t2.DENSE_RANK
       FROM (SELECT t2.ID
                   ,t2.Info
                   ,t2.Total
                   ,DENSE_RANK() OVER (PARTITION BY t2.ID ORDER BY t2.Total DESC) AS DENSE_RANK
             FROM table1 t2 WITH (NOLOCK)) t2
             WHERE DENSE_RANK = 1) AS t2 ON t1.ID = t2.ID

LEFT JOIN (SELECT t3.ID
             ,t3.Info AS Info2
             ,t3.Total
             ,t3.DENSE_RANK
       FROM (SELECT t3.ID
                   ,t3.Info
                   ,t3.Total
                   ,DENSE_RANK() OVER (PARTITION BY t3.ID ORDER BY t3.Total DESC) AS DENSE_RANK
             FROM table1 t3 WITH (NOLOCK)) t3
             WHERE DENSE_RANK = 2) AS t3 ON t1.ID = t3.ID

LEFT JOIN (SELECT t4.ID
             ,t4.Info AS Info3
             ,t4.Total
             ,t4.DENSE_RANK
       FROM (SELECT t4.ID
                   ,t4.Info
                   ,t4.Total
                   ,DENSE_RANK() OVER (PARTITION BY t4.ID ORDER BY t4.Total DESC) AS DENSE_RANK
             FROM table1 t4 WITH (NOLOCK)) t4
             WHERE DENSE_RANK = 3) AS t4 ON t1.ID = t4.ID

ORDER BY t1.ID ASC

然后我运行了您提供的代码的修改版本(谢谢):

SELECT MAX(A.ID) AS ID
  ,STUFF((SELECT ',' + Info1
          FROM temp t WITH (NOLOCK)
          WHERE t.[Rank1] = A.[Rank1]
                AND t.[ID] = A.[ID] FOR XML PATH(''))  ,  1 , 1 , '' ) AS Info1
FROM temp A
GROUP BY A.ID
    ,A.[Rank1]

但我的结果仍然包含重复项:

ID  Info1
1   A,A,A,B,B,B
2   D,D
3   F