【问题标题】:How to get average of columns in a pivot table如何获取数据透视表中列的平均值
【发布时间】:2015-01-31 04:57:09
【问题描述】:

我刚刚学习了数据透视表的概念,有人帮我想出了这个代码来创建一个:(我添加了平均代码)

  SELECT  pvt.Fname,
            pvt.Lname,
            pvt.[English], 
            pvt.[Mathematics], 
            pvt.[Geography], 
            pvt.[Literature], 
            pvt.[French],
            TotalScore = ISNULL(pvt.[English], 0) + ISNULL(pvt.[Mathematics], 0) + ISNULL(pvt.[Geography], 0) + ISNULL(pvt.[Literature], 0) + ISNULL(pvt.[French], 0)

            AverageScore=AVG((pvt.[English]) + (pvt.[Mathematics]) + (pvt.[Geography]) +(pvt.[Literature])+(pvt.[French]))

    FROM    (   SELECT  s.SubjectName, er.Result, su.Fname, su.Lname 
                FROM    Subject AS s
                        INNER JOIN ExamResults AS er
                            ON s.SubjectID = er.SubjectID
                        INNER JOIN Exam AS e
                            ON er.ExamID = e.ExamID
                        LEFT JOIN Student AS su
                            ON er.StudentID = su.StudentID
                WHERE   e.Class = @Class
            ) AS e
            PIVOT 
            (   MAX([Result])
                FOR [SubjectName] IN ([English], [Mathematics], [Geography], [Literature], [French])
            ) AS pvt;




 Fname  |   Lname  | English | Mathematics | Geography | Literature | French | TotalScore| AverageScore
     -------+----------+---------+-------------+-----------+------------+--------+-------------
    Earnest |   Baker  |   100   |     35      |      -    |     -      |   75   |     210    
     House  | Richards |    56   |      -      |     35    |     -      |   75   |     166
     Jacob  |    Jake  |    86   |     37      |      -    |     75     |    -   |     198 

四个表及其列如下:

主题表列:SubjectID,SubjectName

学生表列:StudentID,Fname,Lname

考试表格列:ExamID,ExamDate,ExamPeriod,Class

ExamResults 列:ExamResultID,ExamID,StudentID,SubjectID,Result

问题是,Average 列没有返回任何值。我正在使用 sql 服务器。请你能帮我找出我做错了什么吗?

【问题讨论】:

  • 能否添加示例数据和预期输出
  • 尝试 /5 而不是 AVG
  • 表格在这里。 stackoverflow.com/questions/28233022/…@NoDisplayName
  • 在平均函数中总结主题时使用“ISNULL”以避免任何总结错误。并按照 SARATH 所说的那样得到平均值,除以科目总数。
  • @SarathAvanavu - 干得好。连OP都懒得回复

标签: c# sql-server


【解决方案1】:

我建议你在Pivot源查询中找到resultstotalaverage

SELECT pvt.Fname,
       pvt.Lname,
       pvt.[English],
       pvt.[Mathematics],
       pvt.[Geography],
       pvt.[Literature],
       pvt.[French],
       TotalScore,
       AverageScore
FROM   (SELECT s.SubjectName,
               er.Result,
               su.Fname,
               su.Lname,
               TotalScore = Sum(Result)OVER(partition BY Fname, Lname),
                AverageScore = Avg(Result)OVER(partition BY Fname, Lname)
               //AverageScore = Sum(Result)OVER(partition BY Fname, Lname)/5 If you want average for all the subjects
        FROM   Subject AS s
               INNER JOIN ExamResults AS er
                       ON s.SubjectID = er.SubjectID
               INNER JOIN Exam AS e
                       ON er.ExamID = e.ExamID
               LEFT JOIN Student AS su
                      ON er.StudentID = su.StudentID
        WHERE  e.Class = @Class) AS e
       PIVOT ( Max([Result])
             FOR [SubjectName] IN ([English],
                                   [Mathematics],
                                   [Geography],
                                   [Literature],
                                   [French]) ) AS pvt; 

【讨论】:

  • 感谢您的回答,但是,我收到一个错误:“列名“结果”无效...我想对值不为空的主题进行平均...我认为 Avg() 应该忽略这些值吗?所以在这种情况下我不能使用除以 5
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多