2012-09-21 78 views
0

我有這個疑問,我想組由surveyname但即時得到這個錯誤:SQL Server 2008的數據透視表彙總函數問題

Msg 8120, Level 16, State 1, Line 1
Column 'pvt.Follow Up' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

這是查詢:

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

這是結果即時得到的一例

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和僅具有兩個結果到底。

+0

我增加了結果的支點前 – Geo

+1

嘗試刪除從內部選擇,你是不包括在最終'SELECT'這些領域 - 'q.question,subq.answer subquestion ,aw.answerweight,sc.cweight'。這些字段可能使行不同,所以'GROUP BY'不起作用。 – Taryn

+0

添加此爲答案,以便我可以接受它。我刪除了子查詢中的額外列,即使沒有'group by'函數 – Geo

回答

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 
1

我不知道錯誤來自您發佈的內容(我假設它是當您嘗試在您發佈的查詢的末尾添加GROUP BY SurveyName),但是您需要刪除冗餘列你的子查詢,所以你只能選擇3列,你需要,surveynamescorecategoryname

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,因爲它們包含在子查詢中還分組您的最終結果的背景,但由於不在選擇列表中,您沒有立即看到這種效果。

+0

我刪除q.question,subq.answer subquestion,aw.answerweight,sc.cweight和我的查詢工程 – Geo