我正在尝试按县分组,并在下面的查询中按县排序,它不起作用。我怎样才能按县分组并按县命令?
select COUNT(sr.Response) as 'Number Of Responses',
County =
(select qpa.PossibleAnswerText from CaresPlusParticipantSurvey.QuestionPossibleAnswer as qpa
join CaresPlusParticipantSurvey.SurveyResponse as sr1 on qpa.Id = sr1.QuestionPossibleAnswerId
where (sr1.QuestionId = 1 and sr.SurveyId = sr1.SurveyId))
FROM CaresPlusParticipantSurvey.SurveyResponse as sr
where (sr.Response is not null and CAST(sr.Response as int) < 6) and (sr.QuestionId = 8)
Group BY sr.Response, sr.SurveyId
Order by County
以下结果是当前查询的当前结果。我希望它按县分组4阿拉米达1 Colusa 8 Contra Costa
1 Alameda
1 Alameda
1 Alameda
1 Alameda
1 Colusa
1 Contra Costa
1 Contra Costa
1 Contra Costa
1 Contra Costa
1 Contra Costa
1 Contra Costa
1 Contra Costa
1 Contra Costa
答案 0 :(得分:1)
您应该能够总结回复数量&#34;和GROUP BY县:
select sum(COUNT(sr.Response) as 'Number Of Responses')
和
Group BY sr.Response, sr.SurveyId
Order by County
答案 1 :(得分:0)
这应该可以满足您的需求,但如果没有样本数据,我无法确定:
SELECT a.County, COUNT(*) 'Number of Responses'
FROM
(
SELECT
sr.Response,
County =
(
SELECT
qpa.PossibleAnswerText
FROM CaresPlusParticipantSurvey.QuestionPossibleAnswer AS qpa
JOIN CaresPlusParticipantSurvey.SurveyResponse AS sr1 ON qpa.Id = sr1.QuestionPossibleAnswerId
WHERE (sr1.QuestionId = 1 AND sr.SurveyId = sr1.SurveyId)
)
FROM CaresPlusParticipantSurvey.SurveyResponse AS sr
WHERE (sr.Response IS NOT NULL
AND CAST(sr.Response AS INT) < 6)
AND (sr.QuestionId = 8)
) a
GROUP BY a.County