Bean Huang
Bean Huang

Reputation: 159

SQL Server Concatenate Multiple Rows By Group Using FOR XML PATH

I have a table called findhighest_secondstep with data like this:

id  department  top_employee    highest_salary  rownumber1
-----------------------------------------------------------
 5  Finance         Shane           6300            1
10  Finance         Laura           6300            1
 7  HR              Vik             7200            1
 3  IT              Kate            7500            1
14  Marketing       Elice           6800            1
 6  Sales           Shed            8000            1

I want to return a table with columns department, top_employee and highest_salary while I know in Finance department we have 2 people having the same salary. So I want to show all of them.

SELECT 
    hs.department AS department,
    top_employee = STUFF((SELECT ', ' + top_employee
                          FROM findhighest_secondstep 
                          FOR XML PATH('')), 1, 1, '')                   
FROM 
    findhighest_secondstep hs
GROUP BY 
    hs.department

This is what I got:

department    top_employee
--------------------------------------------------
Finance       Shane, Laura, Vik, Kate, Elice, Shed
HR            Shane, Laura, Vik, Kate, Elice, Shed
IT            Shane, Laura, Vik, Kate, Elice, Shed
Marketing     Shane, Laura, Vik, Kate, Elice, Shed
Sales         Shane, Laura, Vik, Kate, Elice, Shed

What I want:

department  top_employee    highest_salary
---------------------------------------------
Finance     Shane, Laura    6300
HR          Vik             7200
IT          Kate            7500
Marketing   Elice           6800
Sales       Shed            8000

Upvotes: 4

Views: 7690

Answers (3)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

We may try using STRING_AGG here, since you are using SQL Server 2017:

WITH cte AS (
    SELECT *, RANK() OVER (PARTITION BY department ORDER BY highest_salary DESC) rnk
    FROM findhighest_secondstep
)

SELECT
    department,
    STRING_AGG(top_employee, ',') AS top_employee,
    MAX(highest_salary) AS highest_salaray
FROM cte
WHERE
    rnk = 1
GROUP BY
    department;

The logic here is that we assign a rank of 1 to every employee in a given department who has the highest salary. Then, we aggregate by department, turning out a CSV list of all employees tied for first place, along with the highest salary.

Upvotes: 0

gotqn
gotqn

Reputation: 43636

You can use STRING_AGG. Something like this:

SELECT department
      ,top_employee    
      ,STRING_AGG(highest_salary, ',')
FROM findhighest_secondstep 
GROUP BY department
        ,highest_salary

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269773

You need a correlated subquery:

SELECT hs.department AS department,
       STUFF( (SELECT ', ' + top_employee
               FROM findhighest_secondstep hs2
               WHERE hs2.department = hs.department
               FOR XML PATH('')
              ), 1, 2, ''
            ) as top_employees                   
FROM findhighest_secondstep hs
GROUP BY hs.department

Upvotes: 7

Related Questions