0

我有这个查询,我正在尝试按调查名称分组,但我收到了这个错误:

消息 8120,级别 16,状态 1,第 1
行列“pvt.Follow Up”在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中。

这是查询:

SELECT 
   surveyname, [Follow Up] AS Follow_Up, [Ambiance] AS Ambiance, 
   [Consultation] AS Consultation, [Procedure/Service] AS Procedure_Service
FROM 
   (SELECT
       s.name surveyname, q.question, subq.answer subquestion,aw.answerweight, 
       aw.score, rc.categoryname, sc.cweight
    FROM survey.dbo.results r
    JOIN survey.dbo.questions q ON r.questionidfk = q.id
    LEFT JOIN survey.dbo.answers subq ON r.itemidfk = subq.id
    LEFT JOIN survey.dbo.answers a ON r.answeridfk = a.id
    JOIN survey.dbo.surveys s ON q.surveyidfk = s.id
    join sigweb.dbo.survey_types_main stm on s.id = stm.surveyidfk
    join survey.dbo.survey_results sr on r.owneridfk = sr.ownerid
    join sigweb.dbo.BosleySurvey bs on bs.contactid = sr.contactid and stm.clientsurveytypeid = bs.surveytype
    join sigweb.dbo.contact c on sr.contactid = c.contactid
    join sigweb.dbo.patient p on p.contactid = c.contactid
    join sigweb.dbo.doctor d on p.doctorid = d.doctorid
    join sigweb.dbo.survey_tracking st on st.contactid = c.contactID and st.surveytypeid = stm.surveytypeid
    left join survey.dbo.answerweighting aw on isnull(r.itemidfk, r.questionidfk) = aw.questionitemidfk and r.answeridfk = aw.answeridfk
    left join survey.dbo.rating_categories rc on aw.categoryidfk = rc.id
    left join survey.dbo.survey_categories sc on aw.categoryidfk = sc.categoryidfk and s.id = sc.surveyidfk
    where 
       aw.answerWeight is not null) ps
PIVOT
(
   AVG(score)
   FOR categoryname IN
    ( [Follow Up], [Ambiance], [Consultation], [Procedure/Service])
) AS pvt

按调查名称分组

这是我得到的结果的一个例子

SURVEYNAME      FOLLOW_UP   Ambiance   Consultation   Procedure_Service
Review             NULL     NULL    NULL          9.81
Review             9.54     NULL    NULL          NULL
Consultation       5        NULL    NULL          NULL
Consultation       NULL     5           NULL          NULL
Consultation       NULL     5           NULL          NULL
Consultation       NULL     5           NULL          NULL
Consultation       NULL     5           NULL          NULL
Consultation       NULL     5           NULL          NULL
Consultation       NULL     NULL    5         NULL
Consultation       5        NULL    NULL          NULL
Consultation       NULL     NULL    5         NULL

这是数据透视之前的数据示例:

Review          6   Follow Up
Review          9   Procedure/Service
Consultation    5   Ambiance
Consultation    5   Ambiance
Consultation    5   Ambiance
Consultation    5   Ambiance
Consultation    5   Ambiance
Consultation    5   Ambiance
Consultation    5   Consultation
Consultation    5   Consultation

这个想法是按 分组,最后surveyname只有两个结果。

4

2 回答 2

1

我不确定您发布的内容中的错误来自哪里(我假设是当您尝试添加GROUP BY SurveyName到您发布的查询的末尾时),但是您需要从子查询中删除冗余列,所以您只选择您需要的 3 列surveynamescore、 和categoryname

SELECT 
   surveyname, [Follow Up] AS Follow_Up, [Ambiance] AS Ambiance, 
   [Consultation] AS Consultation, [Procedure/Service] AS Procedure_Service
FROM 
   (SELECT
       s.name surveyname, aw.score, rc.categoryname
    FROM survey.dbo.results r
    JOIN survey.dbo.questions q ON r.questionidfk = q.id
    LEFT JOIN survey.dbo.answers subq ON r.itemidfk = subq.id
    LEFT JOIN survey.dbo.answers a ON r.answeridfk = a.id
    JOIN survey.dbo.surveys s ON q.surveyidfk = s.id
    join sigweb.dbo.survey_types_main stm on s.id = stm.surveyidfk
    join survey.dbo.survey_results sr on r.owneridfk = sr.ownerid
    join sigweb.dbo.BosleySurvey bs on bs.contactid = sr.contactid and stm.clientsurveytypeid = bs.surveytype
    join sigweb.dbo.contact c on sr.contactid = c.contactid
    join sigweb.dbo.patient p on p.contactid = c.contactid
    join sigweb.dbo.doctor d on p.doctorid = d.doctorid
    join sigweb.dbo.survey_tracking st on st.contactid = c.contactID and st.surveytypeid = stm.surveytypeid
    left join survey.dbo.answerweighting aw on isnull(r.itemidfk, r.questionidfk) = aw.questionitemidfk and r.answeridfk = aw.answeridfk
    left join survey.dbo.rating_categories rc on aw.categoryidfk = rc.id
    left join survey.dbo.survey_categories sc on aw.categoryidfk = sc.categoryidfk and s.id = sc.surveyidfk
    where 
       aw.answerWeight is not null) ps
PIVOT
(
   AVG(score)
   FOR categoryname IN
    ( [Follow Up], [Ambiance], [Consultation], [Procedure/Service])
) AS pvt

在后台,您还将最终结果分组,q.question, subq.answer subquestion,aw.answerweight, sc.cweight因为它们包含在子查询中,但由于不在选择列表中,您不会立即看到这样做的效果。

于 2012-09-21T18:57:17.797 回答
1

It appears that you are including too many columns in the inner SELECT, try to remove the columns:

q.question, subq.answer subquestion, aw.answerweight, sc.cweight

They are most likely making the rows DISTINCT so the GROUP BY does not work properly. So your query will be:

SELECT surveyname, 
    [Follow Up] AS Follow_Up, 
    [Ambiance] AS Ambiance, 
    [Consultation] AS Consultation, 
    [Procedure/Service] AS Procedure_Service
FROM 
(
    SELECT s.name surveyname, 
        aw.score, 
        rc.categoryname, 
    FROM survey.dbo.results r
    JOIN survey.dbo.questions q 
        ON r.questionidfk = q.id
    LEFT JOIN survey.dbo.answers subq 
        ON r.itemidfk = subq.id
    LEFT JOIN survey.dbo.answers a 
        ON r.answeridfk = a.id
    JOIN survey.dbo.surveys s 
        ON q.surveyidfk = s.id
    join sigweb.dbo.survey_types_main stm 
        on s.id = stm.surveyidfk
    join survey.dbo.survey_results sr 
        on r.owneridfk = sr.ownerid
    join sigweb.dbo.BosleySurvey bs 
        on bs.contactid = sr.contactid 
            and stm.clientsurveytypeid = bs.surveytype
    join sigweb.dbo.contact c 
        on sr.contactid = c.contactid
    join sigweb.dbo.patient p 
        on p.contactid = c.contactid
    join sigweb.dbo.doctor d on p.doctorid = d.doctorid
    join sigweb.dbo.survey_tracking st 
        on st.contactid = c.contactID 
            and st.surveytypeid = stm.surveytypeid
    left join survey.dbo.answerweighting aw 
        on isnull(r.itemidfk, r.questionidfk) = aw.questionitemidfk 
            and r.answeridfk = aw.answeridfk
    left join survey.dbo.rating_categories rc 
        on aw.categoryidfk = rc.id
    left join survey.dbo.survey_categories sc 
        on aw.categoryidfk = sc.categoryidfk and s.id = sc.surveyidfk
    where aw.answerWeight is not null
) ps
PIVOT
(
   AVG(score)
   FOR categoryname IN
    ( [Follow Up], [Ambiance], [Consultation], [Procedure/Service])
) AS pvt
于 2012-09-21T19:09:54.140 回答