Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combing two queries while using group by

Tags:

sql

mysql

Having some trouble figuring out the logic to this. See the two queries below:

Query 1:

SELECT cId, crId, COUNT(EventType)
FROM Data
WHERE EventType='0' OR EventType='0p' OR EventType='n' OR EventType = 'np'
GROUP BY crId;

Query 2:

SELECT cId, crId, COUNT(EventType) AS Clicks
FROM Data
WHERE EventType='c'
GROUP BY crId;

Was just wondering if there was a way to make the column that I would get at the end of query 2 appear in query 1. Since the where statements are different, not really sure where to go, and any subquery that I've wrote just hasn't worked.

Thanks in advance

like image 203
Jon Hagelin Avatar asked Jan 14 '23 13:01

Jon Hagelin


1 Answers

SELECT cId, crId, 
SUM(CASE WHEN EventType='0' OR EventType='0p' OR EventType='n' OR EventType = 'np' THEN 1 ELSE 0 END) AS Count_1,
SUM(CASE WHEN EventType='c' THEN 1 ELSE 0 END) AS Count_2
FROM Data
WHERE EventType IN ('0','0p','n','np','c')
GROUP BY crId;
like image 185
fancyPants Avatar answered Jan 17 '23 03:01

fancyPants