i am using sql server 2008 and have two tables (family_table)
and(children_table)
that are related by family_id
, How do you count children that havestate 'a'
or state 'c' and children that have state 'b' and != 'e' and != 'f'
to each family via one query?
I tried query but the result of count of children_state
is wrong , and i don't know where is the problem , my query is :
select f.family_id ,
count(c.family_id) as [A children Count] ,
count(cp.family_id) as [B children Count] FROM Family_table as f left outer join
children_table as c on c.family_id = f.family_id and c.children_state = 'A' or c.children_state = 'c' left outer join children_table as cp
on cp.family_id = f.family_id and cp.children_state = 'B' and cp.children_state <> 'e' and cp.children_state <> 'f' group by f.family_id