如何将两个查询合并为一个

时间:2015-09-17 10:53:11

标签: sql sql-server

如何加入这两个SQL语句?我希望第一列和第二列的列一起显示为一个SQL查询。

SELECT  
    E.tbl1_ORG AS Organización, E.tbl1_CODE AS [Orden de Trabajo], 
    E.tbl1_OBJECT AS Equipo, O.tbl3_POSITION AS Posicion, 
    E.tbl1_JOBTYPE AS [Tipo de Trabajo], 
    E.tbl1_DESC AS [Descripcion OT], E.tbl1_WORKADDRESS AS Comentarios, 
    E.tbl1_REQM AS Error, B.tbl2_PERSON AS Trabajador, 
    B.tbl2_ENTERED AS Fecha, B.tbl2_HOURS AS Horas
FROM    
    dbo.table1 AS E 
INNER JOIN
    dbo.table2 AS B ON E.tbl1_CODE = B.tbl2_EVENT 
INNER JOIN
    dbo.table3 AS O ON O.tbl3_CODE = E.tbl1_OBJECT
WHERE   
    E.tbl1_JOBTYPE IN ('PM', 'CM', 'PMM') and
    E.tbl1_ORG = #PROMPT('Organización')# and
    B.tbl2_ENTERED between #PROMPT('Fecha_Inicio')# and #PROMPT('Fecha_Final')# and 
    (E.tbl1_REQM = #PROMPT('Error')# OR #PROMPT('Error')# = '%') and
    (E.tbl1_OBJECT = #PROMPT('Equipo')# OR #PROMPT('Equipo')# = '%') and
    (O.tbl3_POSITION = #PROMPT('Posicion')# OR #PROMPT('Posicion')# = '%')

SELECT  
    tbl2_event 'Orden de Trabajo',
    STUFF((SELECT ', ' + CAST(tbl2_person AS VARCHAR(100)) [text()]    
           FROM table2 
           WHERE tbl2_event = t.tbl2_event
           FOR XML PATH(''), TYPE).value('.','NVARCHAR(MAX)'),1,1,' ') Empleados, 
    STUFF((SELECT ', ' + CAST(tbl2_hours AS VARCHAR(100)) [text()]       
           FROM table2 
           WHERE tbl2_event = t.tbl2_event
           FOR XML PATH(''), TYPE).value('.','NVARCHAR(MAX)'), 1, 1, ' ') Horas
FROM table2 t
GROUP BY tbl2_event

两者都完全靠自己工作,但我不知道如何合并它们。

1 个答案:

答案 0 :(得分:1)

为每个查询添加ROW_NUMBER,然后FULL JOIN将它们放在一起。您需要决定每个查询中行的顺序。

WITH
CTE1
AS
(
    SELECT
        ROW_NUMBER() OVER (ORDER BY ...) AS rn,
        E.tbl1_ORG AS Organización, E.tbl1_CODE AS [Orden de Trabajo], 
        E.tbl1_OBJECT AS Equipo, O.tbl3_POSITION AS Posicion, 
        E.tbl1_JOBTYPE AS [Tipo de Trabajo], 
        E.tbl1_DESC AS [Descripcion OT], E.tbl1_WORKADDRESS AS Comentarios, 
        E.tbl1_REQM AS Error, B.tbl2_PERSON AS Trabajador, 
        B.tbl2_ENTERED AS Fecha, B.tbl2_HOURS AS Horas
    FROM
        dbo.table1 AS E 
    INNER JOIN
        dbo.table2 AS B ON E.tbl1_CODE = B.tbl2_EVENT 
    INNER JOIN
        dbo.table3 AS O ON O.tbl3_CODE = E.tbl1_OBJECT
    WHERE   
        E.tbl1_JOBTYPE IN ('PM', 'CM', 'PMM') and
        E.tbl1_ORG = #PROMPT('Organización')# and
        B.tbl2_ENTERED between #PROMPT('Fecha_Inicio')# and #PROMPT('Fecha_Final')# and 
        (E.tbl1_REQM = #PROMPT('Error')# OR #PROMPT('Error')# = '%') and
        (E.tbl1_OBJECT = #PROMPT('Equipo')# OR #PROMPT('Equipo')# = '%') and
        (O.tbl3_POSITION = #PROMPT('Posicion')# OR #PROMPT('Posicion')# = '%')
)
,CTE2
AS
(
    SELECT
        ROW_NUMBER() OVER (ORDER BY ...) AS rn,
        tbl2_event 'Orden de Trabajo',
        STUFF((SELECT ', ' + CAST(tbl2_person AS VARCHAR(100)) [text()]    
               FROM table2 
               WHERE tbl2_event = t.tbl2_event
               FOR XML PATH(''), TYPE).value('.','NVARCHAR(MAX)'),1,1,' ') Empleados, 
        STUFF((SELECT ', ' + CAST(tbl2_hours AS VARCHAR(100)) [text()]       
               FROM table2 
               WHERE tbl2_event = t.tbl2_event
               FOR XML PATH(''), TYPE).value('.','NVARCHAR(MAX)'), 1, 1, ' ') Horas
    FROM table2 t
    GROUP BY tbl2_event
)
SELECT ...
FROM
    CTE1 FULL JOIN CTE2 ON CTE1.rn = CTE2.rn
ORDER BY ...
;

如果您有超过2-3个表加入,FULL JOIN会很快变得丑陋和缓慢。看看我对替代解决方案的类似问题的答案:best way to "glue" columns together