TSQL Pivot with group by

时间:2018-02-22 17:50:11

标签: sql-server tsql pivot

我正在使用PIVOT子句转换此数据:

ComponenteID Descripcion                              PosicionID  Email                                                        Nivel
------------ ---------------------------------------- ----------- ------------------------------------------------------------ -----
9            Maquinaria                               19          monica.rodriguez@definity.com                           1
9            Maquinaria                               19          monica.rodriguez@definity.com                           2
9            Maquinaria                               19          monica.rodriguez@definity.com                           3
9            Maquinaria                               150         fulano.detal@definityfirst.com                               1

要:

TipoComponenteId Descripcion                              AprobadoresNivel1                                                                   AprobadoresNivel2                                                                   AprobadoresNivel3
---------------- ---------------------------------------- ----------------------------------------------------------------------------------- ----------------------------------------------------------------------------------- -----------------------------------------------------------------------------------
9                Maquinaria                               19 - monica.rodriguez@definity.com, 150 - fulano.detal@definity.com                                             19 - monica.rodriguez@definity.com                                             19 - monica.rodriguez@definity.com

但是因为MAX子句偏离了AprobadoresNivel1(第三列)中的一个元素(见下文)

TipoComponenteId Descripcion                              AprobadoresNivel1                                                                   AprobadoresNivel2                                                                   AprobadoresNivel3
---------------- ---------------------------------------- ----------------------------------------------------------------------------------- ----------------------------------------------------------------------------------- -----------------------------------------------------------------------------------
9                Maquinaria                               19 - monica.rodriguez@definityfirst.com                                             19 - monica.rodriguez@definityfirst.com                                             19 - monica.rodriguez@definityfirst.com

有没有办法在PIVOT子句中使用group by?还有其他方法可以实现这个目标吗?

1 个答案:

答案 0 :(得分:0)

在Panagiotis Kanavos的帮助下,我设法做到这一点,解决了我的问题:

SELECT 
     tc.ID TipoComponenteId  ,tc.Descripcion,
    STRING_AGG(
        CASE 
            WHEN ac.Nivel = 1 THEN (CONVERT(VARCHAR(20),ac.PosicionID) + ' - ' + e.Email)
            ELSE null
        END,',') AS AprobadoresNivel1,
    STRING_AGG(
        CASE 
            WHEN ac.Nivel = 2 THEN  (CONVERT(VARCHAR(20),ac.PosicionID) + ' - ' + e.Email)
            ELSE null
        END,',') AS AprobadoresNivel2,
    STRING_AGG(
        CASE 
            WHEN ac.Nivel = 3 THEN  (CONVERT(VARCHAR(20),ac.PosicionID) + ' - ' + e.Email)
            ELSE null
        END,',') AS AprobadoresNivel3
FROM AprobadoresComision ac
    INNER JOIN TipoComponente tc ON tc.ID = ac.ComponenteID
    INNER JOIN Posicion p ON p.PosicionID = ac.PosicionID
    INNER JOIN Empleado e ON e.EmpleadoID = p.EmpleadoID
WHERE ac.Estatus = 1 
    AND ac.ComponenteID = 9 
GROUP BY tc.ID,tc.Descripcion

由于