我有一个非常复杂的选择语句,它使用内部连接从表中返回计数统计信息(将其视为答案库——下面的复杂选择语句)。
这些答案与名为 Questions_Bank_AnswerChoices 的表相关(存储所有问题)。
我试图首先提取问题(从表 Questions_Bank_AnswerChoices 中),然后将它们与统计数据(下面的复杂语句)匹配。下面的复杂语句提取统计数据,但除非问题已得到回答,否则不会提取问题。
因此,如果没有人回答问题 1,那么问题 1 将不会显示在统计数据中,因为它不包含在“答案”表中(因为没有人回答它)。
我怎样才能做到这一点?我认为我需要外部加入?
复杂的选择语句:
WITH tbl as (
SELECT
Questions_Bank.QuestionID, Questions_Bank.QuestionName,
REPLACE(Schools_Answers_Items.AnswerValue, '? ', ', ') as AnswerValue,
COUNT(Schools_Answers_Items.SchoolsAnswersItemID) AS CountAnswer,
Schools_Answers_Items.SchoolID
FROM Questions_Bank
INNER JOIN Schools_Answers_Items
ON Questions_Bank.QuestionID = Schools_Answers_Items.QuestionID
LEFT OUTER JOIN Schools_Answers
ON Schools_Answers_Items.SchoolsAnswerID = Schools_Answers.SchoolsAnswerID
WHERE (Questions_Bank.QuestionID = 1108)
AND (Schools_Answers.SchoolID = 103)
GROUP BY
Schools_Answers_Items.SchoolID,
Schools_Answers_Items.AnswerValue,
Questions_Bank.QuestionID,
Questions_Bank.QuestionName
)
SELECT
QuestionID, QuestionName, AnswerValue, CountAnswer,
SUM(CountAnswer) OVER () AS CountAllAnswers
FROM tbl