user1202606
user1202606

Reputation: 1160

return 0 with sql query instead of nothing

How do I return a 0 with as Responses with the PossibleAnswerText if count is 0? Right now it won't return anything.

select 
    COUNT(sr.Id) AS 'Responses', qpa.PossibleAnswerText
from 
    CaresPlusParticipantSurvey.QuestionPossibleAnswer as qpa 
join  
    CaresPlusParticipantSurvey.SurveyResponse as sr on sr.QuestionPossibleAnswerId = qpa.Id
where 
    sr.QuestionPossibleAnswerId = 116 
GROUP BY 
    qpa.PossibleAnswerText

Upvotes: 0

Views: 167

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 181077

You'll need to use LEFT JOIN, and remove the condition on the "rightmost" table from the WHERE clause;

SELECT COUNT(sr.Id) AS 'Responses', qpa.PossibleAnswerText
FROM CaresPlusParticipantSurvey.QuestionPossibleAnswer AS qpa 
LEFT JOIN CaresPlusParticipantSurvey.SurveyResponse as sr 
  ON sr.QuestionPossibleAnswerId = qpa.Id
WHERE qpa.Id = 116 
GROUP BY qpa.PossibleAnswerText

Upvotes: 1

Related Questions