Question

I have a situation where I need to "concatenate" SQL results from the DENSE_RANK function into a single string, I'm not aware of another SQL function that does this.

The issue is that I end up with multiple rows for the same rank from the DENSE_RANK function, which can be expected,so for instance:

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

What I want to achieve is a result that looks like the following, so because ID 1 had 2 Info's with the same rank I want to "concatenate" them into a single string (comma separated):

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

I currently have the following code which is giving me the results from the DENSE_RANK, but I am stuck trying to figure out how to get my final result set where I have a "concatenated" string, I've tried a few different techniques and had a good look through stackoverflow, but I cannot quite find a way that will make it work.

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
Was it helpful?

Solution

Is this is the one you are looking for?

Use a CTE or temporary table to store your curent result say it is 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

This query gives you the comma separated value of Info3 having same rank

OTHER TIPS

@Nithesh

I've modified my code to also pull in the rank:

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

I then run a modified version of your code provided (thanks):

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]

But my results are still containing duplicates:

ID  Info1
1   A,A,A,B,B,B
2   D,D
3   F
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top