2014-06-25 53 views
0

在下面,我发现了平均在今年各部门 从每个月的查询。我试图获得平均从今年所有 部门。如何获得一年中所有部门的平均值?

例如五月在2014年我会得到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

+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 
+0

谢谢,但你不能有一个平均大于5 – user3591637

+0

@ user3591637,如果你已经看到小提琴的所有..我没有做一个平均而只是一个总和。为了得到'平均数'除以12.无论如何,编辑我的答案和小提琴。再看看(编辑答案中包含新的小提琴链接)。 – Rahul

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 
+0

谢谢,但我不认为你理解我已经做了一些编辑的问题,结果应该是1行,显示当年的月平均值 – user3591637

+0

@ user3591637:的确如此。对不起,读错了。我在作品中有一个解决方案,在SQLFiddle发生故障时正在为最后一个查询的子句进行操作。将尽快更新。 –