将多个查询结果合并为一个结果

时间:2017-04-07 05:47:48

标签: sql-server database-design union

我正在使用SQL Server。我有5个表(MTable,C1Table,C2Table,C3Table,C4Table)。

在MTable中我有4个查询,每个查询都给出了正确的结果:

查询1:

SELECT 
    MTable.cID, MTable.COL1, MTable.COL2, MTable.COL4, MTable.COL5, 
    MTable.COL6, MTable.Col7, MTable.Col8, MTable.Col9, MTable.Col10, 
    MTable.Col11, 
    C1Table.Col12, C1Table.Col13, C1Table.Col14, C1Table.Col15 
FROM 
    MTable 
LEFT OUTER JOIN 
    C1Table ON MTable.COL2 = C1Table.COL2 
WHERE 
    (MTable.COL6 = 0) 
    AND (MTable.COL5 = 0) 
    AND (MTable.COL4 = 0) 
    AND (MTable.COL2 > 0)

查询2:

SELECT  
    MTable.cID, MTable.COL1, MTable.COL2, MTable.COL4, MTable.COL5, 
    MTable.COL6, MTable.Col7, MTable.Col8, MTable.Col9, MTable.Col10, 
    MTable.Col11, 
    C2Table.Col12, C2Table.Col13, C2Table.Col14, C2Table.Col15 
FROM 
    MTable 
LEFT OUTER JOIN 
    C2Table ON MTable.COL4 = C2Table.COL4 
WHERE 
    (MTable.COL6 = 0) 
    AND (MTable.COL5 = 0) 
    AND (MTable.COL4 > 0)

查询3:

SELECT 
    MTable.cID, MTable.COL1, MTable.COL2, MTable.COL4, MTable.COL5, 
    MTable.COL6, MTable.Col7, MTable.Col8, MTable.Col9, MTable.Col10, 
    MTable.Col11, 
    C3Table.Col12, C3Table.Col13, C3Table.Col14, C3Table.Col15 
FROM 
    MTable 
LEFT OUTER JOIN 
    C3Table ON MTable.COL5 = C3Table.COL5 
WHERE 
    (MTable.COL6 = 0) 
    AND (MTable.COL5 > 0)

查询4:

SELECT 
    MTable.cID, MTable.COL1, MTable.COL2, MTable.COL4, MTable.COL5, 
    MTable.COL6, MTable.Col7, MTable.Col8, MTable.Col9, MTable.Col10, 
    MTable.Col11, 
    C4Table.Col12, C4Table.Col13, C4Table.Col14, C4Table.Col15 
FROM 
    MTable 
LEFT OUTER JOIN 
    C4Table ON MTable.COL6 = C4Table.COL6 
WHERE 
    (MTable.COL6 > 0)

现在我应该能够创建一个将这些结果合并为一个的查询。这样可以得到正确的结果:

SELECT 
    SUM(ThisCount) AS Expr1 
FROM 
    (SELECT COUNT(*) AS ThisCount 
     FROM MTable 
     LEFT OUTER JOIN C1Table ON MTable.COL1 = C1Table.COL1 
     WHERE (MTable.COL4 = 0) 
       AND (MTable.COL3 = 0) 
       AND (MTable.COL2 = 0) 
       AND (MTable.COL1 > 0) 

     UNION ALL 

     SELECT COUNT(*) AS ThisCount 
     FROM MTable AS MTable_3 
     LEFT OUTER JOIN C2Table ON MTable_3.COL2 = C2Table.COL2 
     WHERE (MTable_3.COL4 = 0) 
       AND (MTable_3.COL3 = 0) 
       AND (MTable_3.COL2 > 0) 

     UNION ALL 

     SELECT COUNT(*) AS ThisCount 
     FROM MTable AS MTable_2 
     LEFT OUTER JOIN C3Table ON MTable_2.COL3 = C3Table.COL3 
     WHERE (MTable_2.COL4 = 0) 
       AND (MTable_2.COL3 > 0) 

     UNION ALL 

     SELECT COUNT(*) AS ThisCount 
     FROM MTable AS MTable_1 
     LEFT OUTER JOIN C4Table ON MTable_1.COL4 = C4Table.COL4 
     WHERE (MTable_1.COL4 > 0) ) AS x

但是我需要有一个每个结果的列表(现在我应该有大约3 000行的结果)。

我已经尝试过" UNION ALL",但我收到了错误。我做错了什么?

由于

1 个答案:

答案 0 :(得分:0)

为每个查询创建临时表,然后根据需求

将所有表组合在一起
SELECT MTable.cID
    ,MTable.COL1
    ,MTable.COL2
    ,MTable.COL4
    ,MTable.COL5
    ,MTable.COL6
    ,MTable.Col7
    ,MTable.Col8
    ,MTable.Col9
    ,MTable.Col10
    ,MTable.Col11
    ,C1Table.Col12
    ,C1Table.Col13
    ,C1Table.Col14
    ,C1Table.Col15
    INTO #A
FROM MTable
LEFT OUTER JOIN C1Table ON MTable.COL2 = C1Table.COL2
WHERE (MTable.COL6 = 0)
    AND (MTable.COL5 = 0)
    AND (MTable.COL4 = 0)
    AND (MTable.COL2 > 0)


    SELECT MTable.cID
    ,MTable.COL1
    ,MTable.COL2
    ,MTable.COL4
    ,MTable.COL5
    ,MTable.COL6
    ,MTable.Col7
    ,MTable.Col8
    ,MTable.Col9
    ,MTable.Col10
    ,MTable.Col11
    ,C2Table.Col12
    ,C2Table.Col13
    ,C2Table.Col14
    ,C2Table.Col15
        INTO #B
FROM MTable
LEFT OUTER JOIN C2Table ON MTable.COL4 = C2Table.COL4
WHERE (MTable.COL6 = 0)
    AND (MTable.COL5 = 0)
    AND (MTable.COL4 > 0)

    SELECT MTable.cID
    ,MTable.COL1
    ,MTable.COL2
    ,MTable.COL4
    ,MTable.COL5
    ,MTable.COL6
    ,MTable.Col7
    ,MTable.Col8
    ,MTable.Col9
    ,MTable.Col10
    ,MTable.Col11
    ,C3Table.Col12
    ,C3Table.Col13
    ,C3Table.Col14
    ,C3Table.Col15
            INTO #C
FROM MTable
LEFT OUTER JOIN C3Table ON MTable.COL5 = C3Table.COL5
WHERE (MTable.COL6 = 0)
    AND (MTable.COL5 > 0)
    SELECT MTable.cID
    ,MTable.COL1
    ,MTable.COL2
    ,MTable.COL4
    ,MTable.COL5
    ,MTable.COL6
    ,MTable.Col7
    ,MTable.Col8
    ,MTable.Col9
    ,MTable.Col10
    ,MTable.Col11
    ,C4Table.Col12
    ,C4Table.Col13
    ,C4Table.Col14
    ,C4Table.Col15
            INTO #D
FROM MTable
LEFT OUTER JOIN C4Table ON MTable.COL6 = C4Table.COL6
WHERE (MTable.COL6 > 0)


select sum(value) from (SELECT count(*) value FROM #A
UNION ALL
SELECT count(*) value FROM #B
UNION ALL
SELECT count(*) value FROM #C
UNION ALL
SELECT count(*) value FROM #D) c