Home > Back-end >  Combining two very similar queries with different WHERE conditions
Combining two very similar queries with different WHERE conditions

Time:07-01

I have 2 SQL queries I'm looking to combine. The only difference between the 2 queries is there WHERE Clause.

SELECT plateNo,
       SUM(cardno) AS workingcards_1
FROM 
      (select a.plateNo,
      COUNT(DISTINCT cardno) as cardno
from(select *, use_time
     from card_reader)cr
Inner join meter m
    ON cr.use_time BETWEEN m.start and m.end
    WHERE cr.case IN (first, second, third)
GROUP BY m.plateNo)
GROUP BY plateNo;

Second

SELECT plateNo,
       SUM(cardno) AS workingcards_2
FROM 
      (select a.plateNo,
      COUNT(DISTINCT cardno) as cardno
from(select *, use_time
     from card_reader)cr
Inner join meter m
    ON cr.use_time BETWEEN m.start and m.end
    WHERE cr.case NOT IN (first, second, third)
GROUP BY m.plateNo)
GROUP BY plateNo;

The Output I am looking for is something like this

plateNo Workingcard_1 Workingcard_2
AZ1 13 1
AZ2 4 4
AZ3 20 15

CodePudding user response:

You can use "union all" between the 2 querys

CodePudding user response:

You can use UNION statemnt between the two statements if you don't have duplicate records, UNION ALL if you have duplicate ones.

  • Related