I have two tables and trying to join both of them based on primary and foreign key.But the problem is that in second table the foreign key has multiple duplicate rows.
Structure :-
1 Table - category
catid catname
1 AAA
2 BBB
3 CCC
2 Table - answers
ansid catid userid
1 1 9
2 1 9
3 2 9
4 2 6
The result should be
userid catid catname present in answers table
null 1 AAA no
6 2 BBB yes
null 3 CCC no
My query is
SELECT a.userid, c.catid,c.catname,
case when sum(a.catid is not null) > 0
then 'yes' else 'no' end as present_in_answers_table
from answers a left join
category c on c.catid = a.catid
where (a.userid = 6) group by c.catid
But it is not returning the results what I want.It returns only one row that is
userid catid catname present in answers table
6 2 BBB yes