Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine MySQL queries

I am struggling to find away to combine these 2 queries... ive tried subquerys, joins, unions and have had no luck :(

Query A

SELECT DATE(crtdtime) AS cntldate, sd_class, COUNT(crtdtime) AS created
FROM master
WHERE DATE(crtdtime) = '2011-11-16'
GROUP BY cntldate, sd_class

Which produces

+------------+----------+---------+
| cntldate   | sd_class | created |
+------------+----------+---------+
| 2011-11-16 | CUST     |    2226 |
| 2011-11-16 | NET      |     238 |
+------------+----------+---------+

Query B

SELECT DATE(rstdtime) AS cntldate, sd_class, COUNT(rstdtime) AS restored
FROM master
WHERE DATE(rstdtime) = '2011-11-16'
GROUP BY cntldate, sd_class

Which produces

+------------+----------+----------+
| cntldate   | sd_class | restored |
+------------+----------+----------+
| 2011-11-16 | CUST     |     2315 |
| 2011-11-16 | NET      |      221 |
+------------+----------+----------+

But would like the end result to be...

+------------+----------+---------+----------+
| cntldate   | sd_class | created | restored |
+------------+----------+---------+----------+
| 2011-11-16 | CUST     |    2226 |     2315 |
| 2011-11-16 | NET      |     238 |      221 |
+------------+----------+---------+----------+

Would greatly appreciate any help.

Thank you.

like image 637
mybigman Avatar asked Sep 11 '26 08:09

mybigman


1 Answers

one possible way

SELECT DATE(crtdtime) AS cntldate, 
  sd_class, 
  sum( DATE(crtdtime) = '2011-11-16') AS created
  sum( DATE(rstdtime) = '2011-11-16') AS restored
FROM master
WHERE DATE(crtdtime) = '2011-11-16' or DATE(rstdtime) = '2011-11-16'
GROUP BY cntldate, sd_class
like image 133
Brian Hoover Avatar answered Sep 13 '26 22:09

Brian Hoover