join - MySQL query joining 3 tables and counting -
i've been stuck on problem far long.
have merge 3 tables , counting of distinct values.
have 3 tables
1.user_me
profileid( string )
responded( int 1 or 0)
2.profiles
profileid ( string )
idlocation ( int )
3.lookup_location
id ( int )
location (string )
i can join user_me , profiles on user_me.profileid = profiles.profileid
can join profiles , lookup_location on profiles.idlocation = lookup_location.id
under profiles need count number of distinct values idlocation user_me.profileid = profiles.profileid
need count number of profiles.idlocation have user_me.responded = 1
i have this:
select lookup.location, count(*) total user_me user join profiles on user.profileid= profiles.profileid join lookup_location lookup on profiles.idlocation = lookup.id group profiles.idlocation
but still need have column giving me count user_me.responded = 1
like:
select lookup.location, count(*) total, count(*) responded
if i'm understanding question correctly, can case
statement in count
aggregate:
select lookup.location, count(*) total, count(case when user.responded = 1 1 end) responded user_me user join profiles on user.profileid= profiles.profileid join lookup_location lookup on profiles.idlocation = lookup.id group profiles.idlocation
since you're using mysql
, can use sum(user.responded = 1)
.
Comments
Post a Comment