I’m basically trying to group my traffic based on the traffic source
Lets say the table has the following columns
Enrollment_ID | last_modified | referrer
1 | 2012-07-01 15:00:00 | http://www.facebook.com/l.php?xyz
2 | 2012-07-01 16:00:00 | http://www.facebook.com/l.php?abc
Now the referrer is different – so even if I group it by referrer – it wont say 2
So this is the query I used
select count(*) as 'Enrollments',date_format(e.last_modified,'%d/%m/%y') as 'Date', if(LOCATE('facebook', referrer)>0,'facebook',referrer) as 'referrer'
from enrollment e
where e.last_modified >='2012-07-01 00:00:00'
group by date_format(e.last_modified,'%d/%m/%y'),3
This works fine. But obviously I’m going to have more than one referrer.
So I thought of using case statement
select count(*) as 'Enrollments',date_format(e.last_modified,'%d/%m/%y') as 'Date',
case referrer
when LOCATE('facebook', referrer)>0 then 'facebook'
when LOCATE('google', referrer)>0 then 'google'
else referrer
end as 'referrer'
from enrollment e
where e.last_modified >='2012-07-01 00:00:00'
group by date_format(e.last_modified,'%d/%m/%y'),3
This does not work as expected.
For the first and second query – I have created a sql fiddle
First – http://sqlfiddle.com/#!2/70f6b/2
Second – http://sqlfiddle.com/#!2/ad890/3
I’ll appreciate if someone can take a quick glance at it
Thanks for your time
I never use MySQL, but I’m used to there being two types of CASE statements in other databases and you seem to be trying to mix them.
A
B
So, just delete your first reference to referrer, and your query may work:
Another thing is that it might be cleaner to try to split referrer into several fields, where one of them was the internet address.