SQL Server 2008 Pivot table aggregate function issue

I have this query and I am trying to group by surveyname but im getting this error:

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.

This is the query:

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

group by surveyname

This is an example of the results im getting

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

This is an example of the data before the pivot:

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

The idea is to group by the surveyname and have only two results in the end.

Answers


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

I am not sure where the error is coming from in what you have posted (I assume it is when you try and add GROUP BY SurveyName to the end of the query you have posted), but you need to remove the redundant columns from your subquery, so you only select the 3 columns you need, surveyname, score, and 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

In the background you are also grouping your end results by q.question, subq.answer subquestion,aw.answerweight, sc.cweight because they are included in the subquery, but because the are not in the select list you are not seeing immediately the effect this is having.


Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.