如何获得当年所有部门的平均值?

时间:2014-06-25 21:16:19

标签: sql

在下面的查询中,我得到了每个部门的平均值 从一年中的每个月。我想要获得所有人的平均值 这一年的部门。

例如,对于2014年5月,我的平均得分为2.675,         2014年6月将是2.532。

如何获得每个月所有部门的平均值?

以下是我的查询,我得到了每个部门的平均值:

SELECT employeedept,YEAR_cse,csedept_name,
        SUM(January) as January,SUM(February) as February,SUM(March) as March,SUM(April) as April
        ,SUM(May) as May,SUM(June) as June,SUM(July) as July,SUM(August) as August
        ,SUM(September) as September,SUM(October) as October,SUM(November) as November,SUM(December) as December
        FROM (SELECT employeedept,
    (ROUND(AVG(case when rating1>0 THEN CAST(rating1 AS FLOAT) ELSE null END), 2) +
        ROUND(AVG(case when rating2>0 THEN CAST(rating2 AS FLOAT) ELSE null END), 2) +
        ROUND(AVG(case when rating3>0 THEN CAST(rating3 AS FLOAT) ELSE null END), 2) +
        ROUND(AVG(case when rating4>0 THEN CAST(rating4 AS FLOAT) ELSE null END), 2) +
        ROUND(AVG(case when rating5>0 THEN CAST(rating5 AS FLOAT) ELSE null END), 2)) /5 as AG,
        count(*) as 'totalstars',month_cse= datename(month,execoffice_date),YEAR_cse =YEAR(execoffice_date)
        FROM CSEReduxResponses  
        Where YEAR(execoffice_date) = 2014


        group by employeedept,month(execoffice_date),YEAR(execoffice_date),DATENAME(month,execoffice_date)

      )
    AS r   
JOIN CSEReduxDepts d

ON d.csedept_id = r.employeedept
AND YEAR_cse is NOT NULL
PIVOT( SUM(AG)
    FOR [month_cse] IN (
        [January],[February],[March],[April],[May],[June],[July],[August], [September],[October],[November],[December]
        )) AS pvt 
Group BY     employeedept,YEAR_cse,csedept_name

我用虚拟数据制作了http://sqlfiddle.com/#!3/9d97e/1

2 个答案:

答案 0 :(得分:0)

如果您在group by year_case下方的外部选择中获得总和,该怎么办?查看修改过的小提琴http://sqlfiddle.com/#!3/9d97e/17

select  EMPLOYEEDEPT,
YEAR_CSE,
CSEDEPT_NAME,
((sum(isnull(JANUARY,0)) +
sum(isnull(FEBRUARY,0)) +
sum(isnull(MARCH,0)) +
....
sum(isnull(DECEMBER,0))) / 12) as total_AVG_for_year
from
(
 //your inner main query
) X
group by YEAR_CSE,employeedept,csedept_name

答案 1 :(得分:0)

我编辑了你的小提琴例子,包括了一些grouping()和having子句的用法。还清除了从group by子句中选择的数字。这使我们能够通过易于阅读的代码从聚合函数中获取总数。查看示例here

    SELECT employeedept,YEAR_cse,COALESCE(csedept_name,'All Depts') AS csedept_name,
        SUM(January) as January,SUM(February) as February,SUM(March) as March,SUM(April) as April
        ,SUM(May) as May,SUM(June) as June,SUM(July) as July,SUM(August) as August
        ,SUM(September) as September,SUM(October) as October,SUM(November) as November
        ,SUM(December) as December
        FROM (
          SELECT 
              employeedept,
              (ROUND(AVG(case when rating1>0 THEN CAST(rating1 AS FLOAT) ELSE null END), 2) +
               ROUND(AVG(case when rating2>0 THEN CAST(rating2 AS FLOAT) ELSE null END), 2) +
               ROUND(AVG(case when rating3>0 THEN CAST(rating3 AS FLOAT) ELSE null END), 2) +
               ROUND(AVG(case when rating4>0 THEN CAST(rating4 AS FLOAT) ELSE null END), 2) +
               ROUND(AVG(case when rating5>0 THEN CAST(rating5 AS FLOAT) ELSE null END), 2)) /5 as AG,
              count(*) as 'totalstars'
              ,month_cse=datename(month,execoffice_date)
              ,YEAR_cse =YEAR(execoffice_date)
              , grouping(DATENAME(month,execoffice_date)) as MonthRollup
              , grouping(YEAR(execoffice_date)) as YearRollup
              , grouping(employeedept) as EmployeeDeptRollup
          FROM CSEReduxResponses    
          Where YEAR(execoffice_date) = 2014
          group by rollup(DATENAME(month,execoffice_date),YEAR(execoffice_date),employeedept)
          having (1^grouping(DATENAME(month,execoffice_date)))&(1^grouping(YEAR(execoffice_date))) = 1
      )
    AS r   
left JOIN CSEReduxDepts d

ON d.csedept_id = r.employeedept
AND YEAR_cse is NOT NULL
PIVOT( SUM(AG)
    FOR [month_cse] IN (
        [January],[February],[March],[April],[May],[June],[July],[August], [September],[October],[November],[December]
        )) AS pvt 
Group BY     employeedept,YEAR_cse,csedept_name
order by case when employeedept is null then 2 else 1 end, csedept_name