Skip to content
Advertisement

How do i sum two query with each count and criteria in it

I have this two query sql and i’m trying to do a sum of both 2 counts in the query together but when i try to do the UNION ALL but it kept prompt me to enter parameter for CountOfTools_Number2. All i wan is the sum of the two count and return me a value.

SELECT Count(Test.Tool_Number) AS CountOfTool_Number1
FROM Test INNER JOIN Test_Tool ON Test.Tool_Number = Test_Tool.Current_Tool
GROUP BY Test_Tool.Current_Test
HAVING (((Test_Tool.Current_Test) Like "*E1*"));

SELECT Count(Test.Tool_Number) AS CountOfTool_Number2
FROM Test_Tool, Test INNER JOIN Previous_Tool ON Test.Tool_Number = Previous_Tool.Previous_Tool
GROUP BY Test_Tool.Current_Test
HAVING (((Test_Tool.Current_Test) Like "*E1*"));

Advertisement

Answer

Presuming your two example queries work, this should do the trick:

select 
    sum(CountOfTool_Number1) as CountOfTool
from (

    SELECT Count(Test.Tool_Number) AS CountOfTool_Number1
    FROM Test INNER JOIN Test_Tool ON Test.Tool_Number = Test_Tool.Current_Tool
    GROUP BY Test_Tool.Current_Test
    HAVING (((Test_Tool.Current_Test) Like "*E1*"))

    union all

    SELECT Count(Test.Tool_Number) AS CountOfTool_Number2
    FROM Test_Tool, Test INNER JOIN Previous_Tool ON Test.Tool_Number = Previous_Tool.Previous_Tool
    GROUP BY Test_Tool.Current_Test
    HAVING (((Test_Tool.Current_Test) Like "*E1*"))

    ) as q;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement