获得一个表情符号的主要(主要),次要和三次函数

问题描述 投票:0回答:1

我有以下查询来获取每位员工的主要(主要)职能和次要职能:

with

    employeeScopeFunctions as (

        select      e.employeeId, 
                    es.FunctionId,
                    ef.Label,
                    c.CompanyName,


                    realOrder = row_number() over(
                        partition by e.employeeId
                        order by isnull(es.sortOrder, 9999)
                    )

        from        employee e 
        LEFT JOIN       employee_scope es on es.employeeId = e.employeeId
        LEFT JOIN         employee_function ef on es.FunctionId = ef.FunctionId
        LEFT JOIN         Company c ON es.CompanyId = c.ID
        WHERE e.EmployeeId=54

)

select      *,
            primacy = iif(realOrder = 1, 'main', 'secondary')
from        employeeScopeFunctions

对于EmployeeId = 54,结果如下:

EmployeeId FunctionId Label                                  CompanyName realOrder Primacy
54         273        Group Chief Executive Officer          C1          1         primary
54         273        Group Chief Executive Officer          C2          2         secondary
54         273        Group Chief Executive Officer          X5          3         secondary
54         897        Group Regional Chief Executive Officer X6          4         secondary
54         897        Group Regional Chief Executive Officer F6          5         secondary
54         39         Director                               VY          6         secondary
54         39         Director                               G7          7         secondary

我想得到的是将所有公司重组为一个特定的职能,并获得三个主要级别:

EmployeeId FunctionId Label                                  CompanyName  Primacy
54         273        Group Chief Executive Officer          C1,C2,X5     primary
54         897        Group Regional Chief Executive Officer X6,F6        secondary
54         39         Director                               VY,G7        tertiary
sql sql-server
1个回答
0
投票

如果我正确地遵循了您的说明,则可以保留现有的CTE并在主查询中启用汇总功能。 ROW_NUMBER()可用于通过增加realOrder来对记录进行排名。

这应该在SQL Server中起作用:

WITH employeeScopeFunctions as (
    SELECT
        e.employeeId, 
        es.FunctionId,
        ef.Label,
        c.CompanyName,
        realOrder = row_number() over(partition by e.employeeId order by isnull(es.sortOrder, 9999))
    FROM        
        employee e 
        LEFT JOIN employee_scope es ON es.employeeId = e.employeeId
        LEFT JOIN employee_function ef ON es.FunctionId = ef.FunctionId
        LEFT JOIN company c ON es.CompanyId = c.ID
    WHERE e.EmployeeId=54
)
SELECT
    employeeId, 
    FunctionId,
    Label,
    CompanyName = STRING_AGG(CompanyName, ',') WITHIN GROUP (ORDER BY realOrder),
    Primacy = CASE ROW_NUMBER() OVER(ORDER BY MIN(realOrder))
        WHEN 1 THEN 'primary'
        WHEN 2 THEN 'secondary'
        WHEN 3 THEN 'tertiary'
    END 
FROM employeeScopeFunctions
GROUP BY
    employeeId, 
    FunctionId,
    Label

具有附加嵌套级别以避免嵌套窗口功能和聚合的替代解决方案:

WITH employeeScopeFunctions as (
    SELECT
        e.employeeId, 
        es.FunctionId,
        ef.Label,
        c.CompanyName,
        realOrder = row_number() over(partition by e.employeeId order by isnull(es.sortOrder, 9999))
    FROM        
        employee e 
        LEFT JOIN employee_scope es ON es.employeeId = e.employeeId
        LEFT JOIN employee_function ef ON es.FunctionId = ef.FunctionId
        LEFT JOIN company c ON es.CompanyId = c.ID
    WHERE e.EmployeeId=54
)
SELECT
    employeeId, 
    FunctionId,
    Label,
    CompanyName,
    Primacy = CASE ROW_NUMBER() OVER(ORDER BY minRealOrder))
        WHEN 1 THEN 'primary'
        WHEN 2 THEN 'secondary'
        WHEN 3 THEN 'tertiary'
    END 
FROM (
    SELECT
        employeeId, 
        FunctionId,
        Label,
        CompanyName = STRING_AGG(CompanyName, ',') WITHIN GROUP (ORDER BY realOrder),
        minRealOrder = MIN(realOrder)
    FROM employeeScopeFunctions
    GROUP BY
        employeeId, 
        FunctionId,
        Label
) x
© www.soinside.com 2019 - 2024. All rights reserved.