Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql server复杂联接查询_Sql_Sql Server_Tsql_Unpivot - Fatal编程技术网

sql server复杂联接查询

sql server复杂联接查询,sql,sql-server,tsql,unpivot,Sql,Sql Server,Tsql,Unpivot,我有下面的查询,它正在工作,但我得到了4列(3列以相同的方式命名),因为我只需要得到两列数据,“Entidad”和“Servicio”,但找不到方法 SELECT Entidades.nombre as entidad, Servicios.nombre as servicio, sp.nombre as servicio, sc.nombre as servicio FROM Entidades LEFT JOIN Bank

我有下面的查询,它正在工作,但我得到了4列(3列以相同的方式命名),因为我只需要得到两列数据,“Entidad”和“Servicio”,但找不到方法

SELECT
    Entidades.nombre as entidad,
    Servicios.nombre as servicio,
    sp.nombre        as servicio,
    sc.nombre        as servicio
FROM
    Entidades

LEFT JOIN Banksphere 

    INNER JOIN Servicios
    ON Banksphere.servicio_id = Servicios.id

ON Entidades.id = Banksphere.entidad_id

LEFT JOIN PAS 

     INNER JOIN Servicios sp
     ON sp.id = 3

ON Entidades.id = PAS.entidad_id

LEFT JOIN CAM 

    INNER JOIN Servicios sc
    ON sc.id = 0

ON Entidades.id = CAM.entidad_id
GROUP BY
    Entidades.id, Entidades.nombre, Servicios.nombre, sp.nombre, sc.nombre
ORDER BY
    Entidades.id
我得到的结构是这样的

Entidad     | Servicio | Servicio | Servicio
Corporativo | Abacon   | NULL     | CAM
Corporativo | MCI      | NULL     | CAM
Santander   | Sales    | PAS      | NULL
但是我想要这个

Entidad     | Servicio
Corporativo | Abacon 
Corporativo | MCI    
Corporativo | CAM
Santander   | Sales   
Santander   | PAS  

您应该能够使用
UNPIVOT
功能:

select DISTINCT entidad, value   -- use distinct if you want to remove duplicates
from
(
    SELECT
        Entidades.nombre as entidad,
        Servicios.nombre as servicio1,
        sp.nombre as servicio2,
        sc.nombre as servicio3
    FROM
        Entidades
    LEFT JOIN
        (Banksphere INNER JOIN Servicios
    ON (Banksphere.servicio_id = Servicios.id))
    ON Entidades.id = Banksphere.entidad_id
    LEFT JOIN
        (PAS INNER JOIN Servicios sp
    ON (sp.id = 3))
    ON Entidades.id = PAS.entidad_id
    LEFT JOIN
        (CAM INNER JOIN Servicios sc
    ON (sc.id = 0))
    ON Entidades.id = CAM.entidad_id
    GROUP BY
        Entidades.id, Entidades.nombre, Servicios.nombre, sp.nombre, sc.nombre
) src
unpivot
(
    value
    for col in (servicio1, servicio2, servicio3)
) unpiv

请参见

这是有效的,但如果“Entidades”有“Servicios”的重复记录,我会得到它。。。例如,Santander | PAS Santander | Movilidad Santander | Superlinea Santander | PAS我只需要为每个“实体”获取一个“服务”,如果它有,您可以使用外部选择上的
DISTINCT
关键字来删除重复项。看我的编辑。还有一件事。。。我需要对“Banksphere、PAS和CAM”表中的“Reconocido”列求和。。。然后像这样展示。。。Santander | PAS | X(其中X是“服务”的总和)你能帮我吗?@user1381537我的建议是发布一个新问题,因为你包含的内容没有任何可以这样总结的内容。与论坛网站不同,我们不使用“谢谢”或“感谢任何帮助”或签名。见”。