如何获得列的平均值?

时间:2014-08-04 16:35:38

标签: sql

我有一个查询,我从每个月得到每个评级1-5的平均值。 我想做的是使用我第一次获得的平均值得到所有月份的平均值。

例如,如果下面的查询输出q1-q5的数据: (1,4,1,4,3)= 1 + 4 + 1 + 4 + 3 = 13/5 = 2.6

我想查询一下我可以获得2.6输出。

我制作了一些样本数据:

create table CSEReduxResponses (employeedept int, rating1 int, rating2 int, rating3 int, rating4 int,
rating5 int,approveddate datetime, execoffice_status int);
-- all columns for the table will never be null
insert into CSEReduxResponses (employeedept, rating1, rating2,rating3,rating4,rating5,approveddate,execoffice_status)
values (1 , 1 ,4,1,2,5,'2014-05-01',1),
(1 , 1 ,4,1,4,4,'2014-05-01',1),
(1 , 1 ,4,2,0,5,'2014-05-01',1),
(1 , 1 ,4,2,4,0,'2014-05-01',1),
(1 , 1 ,4,1,2,5,'2014-05-01',1),
(1 , 1 ,4,1,4,3,'2014-06-01',1),
(2 , 4 ,4,2,5,3,'2014-06-01',1),
(2 , 3 ,4,2,4,0,'2014-06-01',1),
(2 , 3 ,4,5,5,0,'2014-06-01',1),
(3 , 0 ,4,5,3,5,'2014-07-01',1);

create table CSEReduxDepts (csedept_id int, csedept_name varchar(25));
insert into CSEReduxDepts (csedept_id, csedept_name)
values (1,'department 1'),
(2,'department 2'),
(3,'department 3'),
(4,'department 4');

这是我的疑问:

SELECT CAST(employeedept AS INT) as dept,
    ROUND(AVG(case when rating1>0 THEN CAST(rating1 AS FLOAT) ELSE null END), 2) as q1,
    ROUND(AVG(case when rating2>0 THEN CAST(rating2 AS FLOAT) ELSE null END), 2) as q2,
    ROUND(AVG(case when rating3>0 THEN CAST(rating3 AS FLOAT) ELSE null END), 2) as q3,
    ROUND(AVG(case when rating4>0 THEN CAST(rating4 AS FLOAT) ELSE null END), 2) as q4,
    ROUND(AVG(case when rating5>0 THEN CAST(rating5 AS FLOAT) ELSE null END), 2) as q5,
    count(*) as 'totalstars',month_cse= datename(month,approveddate),YEAR_cse =YEAR(approveddate)

    FROM CSEReduxResponses
    WHERE execoffice_status = 1
    and YEAR ([approveddate]) =2014

group by month(approveddate),YEAR(approveddate),DATENAME(month,approveddate),employeedept
    order by month_cse

我希望看到它显示上述查询得到的1-5的平均值。

我做了这个http://sqlfiddle.com/#!6/c9811/1 所以我基本上想要得到q1-q5的平均值。 enter image description here

从上面的结果然后将是 第一行: 空(0)+ 4 + 5 + 3 + 5 = 3.4 为第二行: 1 + 4 + 1 + 4 + 3 = 13/5 = 2.6 等等, 所以我想展示这些平均值。

2 个答案:

答案 0 :(得分:0)

我不是百分百肯定我明白你所追求的是什么,但我相信“WITH”条款可以帮助你。

;
WITH tmp AS (
SELECT CAST(employeedept AS INT) as dept,
    ROUND(AVG(case when rating1>0 THEN CAST(rating1 AS FLOAT) ELSE null END), 2) as q1,
    ROUND(AVG(case when rating2>0 THEN CAST(rating2 AS FLOAT) ELSE null END), 2) as q2,
    ROUND(AVG(case when rating3>0 THEN CAST(rating3 AS FLOAT) ELSE null END), 2) as q3,
    ROUND(AVG(case when rating4>0 THEN CAST(rating4 AS FLOAT) ELSE null END), 2) as q4,
    ROUND(AVG(case when rating5>0 THEN CAST(rating5 AS FLOAT) ELSE null END), 2) as q5,
    count(*) as 'totalstars',month_cse= datename(month,approveddate),YEAR_cse =YEAR(approveddate)

    FROM CSEReduxResponses
    WHERE execoffice_status = 1
    and YEAR ([approveddate]) =2014

group by month(approveddate),YEAR(approveddate),DATENAME(month,approveddate),employeedept
)
SELECT dept, q1 + q2 + q3 + q4+ q5 / 5
FROM tmp

答案 1 :(得分:0)

参见" How to use a calculated column to calculate another column in the same view"有更多选项,但我建议使用嵌套查询:

SELECT q1,
    q2,
    q3,
    q4,
    q5,
    (q1 + q2 + q3 + q4 + q5)/5 as avgQ,
    totalstars,
    month_cse,
    YEAR_cse

FROM (SELECT CAST(employeedept AS INT) as dept,
          ROUND(AVG(case when rating1>0 THEN CAST(rating1 AS FLOAT) ELSE null END), 2) as q1,
          ROUND(AVG(case when rating2>0 THEN CAST(rating2 AS FLOAT) ELSE null END), 2) as q2,
          ROUND(AVG(case when rating3>0 THEN CAST(rating3 AS FLOAT) ELSE null END), 2) as q3,
          ROUND(AVG(case when rating4>0 THEN CAST(rating4 AS FLOAT) ELSE null END), 2) as q4,
          ROUND(AVG(case when rating5>0 THEN CAST(rating5 AS FLOAT) ELSE null END), 2) as q5,
          count(*) as 'totalstars',month_cse= datename(month,approveddate),YEAR_cse= YEAR(approveddate)

      FROM CSEReduxResponses
      WHERE execoffice_status = 1
      and YEAR ([approveddate]) =2014

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

    order by month_cse

这是未经测试的,因此可能存在一些问题,但它会让您走上正轨。