0

我正在尝试在下面的查询中按县分组并按县排序,但它不起作用。我怎样才能让它按县分组和排序?

    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 Alameda 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
4

2 回答 2

1

您应该能够总结“响应数”和 GROUP BY 县: select sum(COUNT(sr.Response) as 'Number Of Responses')Group BY sr.Response, sr.SurveyId Order by County

于 2013-07-12T22:06:48.570 回答
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
于 2013-07-12T22:09:48.240 回答