所以我有两个程序
CREATE PROCEDURE GetAnswersByPartner
@pid UNIQUEIDENTIFIER, -- partner id
@sid INT -- survey id
AS
BEGIN
SELECT Sections.title AS section_title,
Subsections.title AS subsection_title,
Questions.id AS question_id,
Questions.qtext AS question_text,
Answers.val AS answer_val
FROM Partners
INNER JOIN Questions ON 1 = 1
INNER JOIN Subsections ON Subsections.id = Questions.subsection_id
INNER JOIN Sections ON Sections.id = Subsections.section_id
INNER JOIN Surveys ON Sections.survey_id = Surveys.id
LEFT JOIN Answers ON Answers.partner_id = Partners.id
AND Answers.question_id = Questions.id
WHERE Partners.id = @pid AND Surveys.id=@sid
ORDER BY Sections.id ASC
END
和
CREATE PROCEDURE GetSectionAverages
@partner_id UNIQUEIDENTIFIER,
@survey_id INT
AS
BEGIN
SELECT S.title, AVG(A.val)
FROM
Sections AS S INNER JOIN Subsections AS SS ON S.Id=SS.section_id
INNER JOIN Questions AS Q ON SS.section_id=Q.subsection_id
INNER JOIN Answers AS A ON A.question_id=Q.id
INNER JOIN Partners AS P ON A.partner_id=P.id
WHERE S.survey_id=@survey_id AND P.id=@partner_id
GROUP BY S.title
END
我无法弄清楚平均值计算错误的原因。
例如,您可以在下面的屏幕截图中看到EXEC GetAnswersByPartner @pid='cd2c53e2-3bd4-451c-8451-6953b9cc7c8b', @sid=1;
"Partner Growth"
的结果
当我运行
时,这是计算到100
的平均值
EXEC GetSectionAverages @partner_id='cd2c53e2-3bd4-451c-8451-6953b9cc7c8b', @survey_id=1;
知道这里发生了什么吗?
答案 0 :(得分:2)
您的第一个查询对Answers
表有LEFT JOIN,而您的第二个查询有INNER JOIN。我怀疑存在差异。换句话说,您的第二个查询可能会排除未回答的问题。