sql server - Select SQL with conditions to count different values -
i'm trying query on sql i'm stuck!
i have 2 tables:
table 1: question
table 2: answer
for each question can have 1 or more answers different users each user can comment 1 time.
when user answers question, must choose 1 status answer:
1) agree, 2) disagree or 3) discuss
so, "question" table has questions this:
id question 1 q1 2 q2 3 q3
..and "answer" table has answers users, plus fk "question" table , column status chosen user.
id answer idquestion status 1 a1 1 1 2 a2 1 3 3 a3 2 2 4 a4 2 2 5 a5 3 1
what need: need select questions , need count questions has different aswer's status.
example:
question 1 has 2 answers , 2 answers has different status. need count or put number know question has answers diffent status.
question 2 has 2 answers answers has same status. don't need count that.. or maybe put other number differentiate questions has answers diffent status.
and questions has 1 answer select normally.
try this:
select q.id, question, case when min(status) <> max(status) 1 else 0 end hasdifferentstatuses questions q inner join answers on(q.id = a.idquestion) group q.id, question
Comments
Post a Comment