简体   繁体   English

SQL 服务器使用 FOR XML 路径按组连接多行

[英]SQL Server Concatenate Multiple Rows By Group Using FOR XML PATH

I have a table called findhighest_secondstep with data like this:我有一个名为findhighest_secondstep的表,其中包含如下数据:

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.我想返回一个包含列部门、top_employee 和最高薪水的表,而我知道在财务部门我们有 2 个人的薪水相同。 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

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

You can use STRING_AGG .您可以使用STRING_AGG Something like this:像这样的东西:

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

We may try using STRING_AGG here, since you are using SQL Server 2017:我们可以在这里尝试使用STRING_AGG ,因为您使用的是 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.这里的逻辑是,我们为给定部门中薪水最高的每个员工分配 1 级。 Then, we aggregate by department, turning out a CSV list of all employees tied for first place, along with the highest salary.然后,我们按部门汇总,得出并列第一名的所有员工的 CSV 列表,以及最高薪水。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM