0

このクエリがあり、surveynameでグループ化しようとしていますが、このエラーが発生します:

メッセージ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、最終的に2つの結果のみを持つことです。

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

内部に含まれている列が多すぎるようです。SELECT列を削除してみてください。

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

彼らはおそらく行を作っているDISTINCTので、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, 
        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 に答える