Tuesday, 6 August 2013

Combining two count queries from same source

Combining two count queries from same source

I have two queries that return a list of service items with a count. One
is for the entire database, and the other is for a specified period. They
work great individually, but I would like to optimize them into a single
query.
The two queries are:
SELECT service_type, count(service_type) from qba_customers group by
service_type order by count(service_type) desc
SELECT service_type, count(service_type) from qba_customers WHERE
created_on BETWEEN '2013-01-01' AND '2013-06-30' group by service_type
order by count(service_type) desc
I tried a few things unsuccessfully, below is what I thought would work
initially:
SELECT service_type, COUNT(service_type) AS full_count,
(count(service_type) WHERE created_on BETWEEN '2013-01-01' AND
'2013-06-30') AS period_count FROM qba_customers GROUP BY service_type
ORDER BY service_type DESC
Thanks in advance!

No comments:

Post a Comment