Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

sql - How to merge two queries into one

How can I join these 2 SQL statements? I want the columns of the first and the columns of the second to appear together as one SQL query.

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')# = '%')

And:

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

Both work perfectly on their own, but I don't know how to merge them.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add ROW_NUMBER to each query and then FULL JOIN them together. You'll need to decide on the ordering of rows in each query.

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 ...
;

If you have more than 2-3 tables to join like this FULL JOIN would quickly become ugly and slow. Have a look at my answer for a similar question for alternative solution: best way to "glue" columns together


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...