Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server 2008透视表聚合函数问题_Sql_Sql Server 2008_Pivot - Fatal编程技术网

SQL Server 2008透视表聚合函数问题

SQL Server 2008透视表聚合函数问题,sql,sql-server-2008,pivot,Sql,Sql Server 2008,Pivot,我有此查询,正在尝试按surveyname分组,但我收到以下错误: Msg 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 Proced

我有此查询,正在尝试按surveyname分组,但我收到以下错误:

Msg 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分组

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

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
分组,最后只有两个结果。

我不确定您发布的内容中的错误来自何处(我假设是在您尝试将
groupby surveyname
添加到您发布的查询末尾时),但您需要从子查询中删除冗余列,因此,您只需选择所需的3列,
surveyname
score
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.answersubquestion、aw.answerweight、sc.cweight对最终结果进行分组,因为它们包含在子查询中,但是,由于不在选择列表中,您无法立即看到其效果。

似乎您在内部
选择中包含了太多的列,请尝试删除这些列:

q.question, subq.answer subquestion, aw.answerweight, sc.cweight
它们很可能使行
不同
,因此
分组依据
无法正常工作。因此,您的查询将是:

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

我在数据透视之前添加了结果。请尝试从内部选择中删除未包含在最终
select
中的字段-
q.question,subq.answer subquestion,aw.answerweight,sc.cweight
。这些字段可能使行不同,因此
分组依据
不起作用。请将此添加为答案,以便我可以接受它。我从子查询中删除了额外的列,即使没有
groupby
功能化的答案,它也可以正常工作,很高兴你找到了答案。:)我删除了q.question、subq.answer子问题、aw.answerweight、sc.cweight和我的查询工作