开发者

SQL Return Count of Answers with 0's for Choices from another table

Here is what I want I want my result to look like:

EDIT! More useful structure provided here

 Answer  Counts
 ------  -------
 0       0
 1       12
 2       4
 3       0
 4       3
 5       6

Responses are stored as 1,2,3,4,5 Where each N = A,B,C,D or something like that.

My problem is

SELECT COUNT(answer_sequence) AS 'Answer Count'
FROM tblILDSlideResponses
WHERE response_id IN (
    SELECT id
    FROM tblILDSlideResponseInfo
    WHERE (slide_id = @slide) AND (session_id = @sessionid)
)
GROUP BY answer_sequence

Returns Something like this:

 Answer  Counts
 ------  -------
 0       12
 1       4
 2       3
 3       6

So when there is a possible choice with no answers I get no count. So I know I need to grab the possible choices which are available here:

SELECT Choice AS 'Choices'
FROM tblSurveyAnswerChoices
WHERE QID IN (
    SELECT question_id
    FROM tblILDSlide
    WHERE id = @slide
)

and then somehow map them to the answers. But how would I accomplish this and still keep these in order. I am thinking I need to return a blank row for every answer that exists in the tblSurveyAnswerChoices then somehow join the other counts to that, but I can't figure it out.

EDIT!

In response to the request from @Christopherous5000:

dbo.tblSLideResponseInfo
开发者_C百科id (PK,int,not null)
user_id (nvarchar(50), not null) --will be FK eventualy
slide_id (FK,int, not null)
response_date (datetime, not null)
session_id (FK,int,not null)

dbo.tblSLideResponses --can store multiple answer for same response
response_id (FK,int,not null)
answer_sequence(int, null)
pther (nvarchar(50), null)


Your post already includes a good answer: "I am thinking I need to return a blank row for every answer that exists in the tblSurveyAnswerChoices then somehow join the other counts to that."

Here is one way to do so:

select choice, ifnull(countanswervalue,0) 
from tblsurveyanswerchoice
left join 
    (select answer_sequence, count(answer_sequence) as countanswervalue
    from tblslideresponses
    group by answer_sequence) 
    as countanswer on countanswer.answer_sequence=tblsurveyanswerchoice.sequence
/* the where clause is what you already posted */
where qid in (select question_id from tblidslide where id = @slide)

The use of left join ensures that any choice in the first table will be listed. By default, left join would return NULL for items not existing in the second table, but ifnull lets you replace NULL with 0.

NOTE: ifnull in MySQL should be replaced by isnull in TSQL


My problem is

SELECT COUNT(answer_sequence) AS 'Answer Count'
FROM tblILDSlideResponses
WHERE response_id IN (
      SELECT id FROM tblILDSlideResponseInfo
      WHERE (slide_id = @slide) AND (session_id = @sessionid)
      )
GROUP BY answer_sequence

(...) So when there is a possible choice with no answers I get no count. So I know I need to grab the possible choices which are available here.

Or, you could try using a left join:

 SELECT answer_sequence, COUNT(answer_sequence) AS 'Answer Count'
 FROM tblILDSlideResponses
 LEFT JOIN tblILDSlideResponseInfo
 ON (slide_id = @slide) AND (session_id = @sessionid)
 AND tblILDSlideResponseInfo.id = tblILDSlideResponses.response_id
 GROUP BY answer_sequence
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜