1

我有一种情况,我需要将 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 个信息具有相同的等级,所以我想将它们“连接”成一个字符串(逗号分隔):

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
4

2 回答 2

0

这是你要找的那个吗?

使用 aCTE 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此查询为您提供具有相同等级的逗号分隔值

于 2013-08-01T04:26:50.550 回答
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
于 2013-08-04T23:28:24.093 回答