select - SQL GROUP BY column from related table -
i have table t1
id resolver resolvedby 1 group1 2 group1 3 group1 4 b group1 5 b group1
i have table t2
groupid group person 1 group1 2 group1 b 3 group1 c
i made sql query return how many records resolved each person
select resolver,count(*) t1 resolvedby='group1' group resolver
and returns correctly
a 3 b 2
however retrieve statistics persons belong group user c have value 0. final result should be
a 3 b 2 c 0
based on edit:
you need outer join existing select 2nd table:
select t2.person, coalesce(cnt, 0) t2 left join ( -- aggregate before join (usually more efficient) select resolver,count(*) cnt t1 resolvedby='group1' group resolver ) t1 on t1.resolver=t2.person t2."group"='group1' -- adding double quotes group keyword
Comments
Post a Comment