如何PIVOT和计算列平均值

时间:2013-06-06 17:36:54

标签: sql sql-server pivot

我承认这是迄今为止我必须面对的最复杂的SQL语句之一。我在这个问题上遇到了问题,我希望有人可以帮助我。

我在数据库中有这个表

 Item      ActiveTime(sec)      DateTime
 -------------------------------------------
 1         10                   2013-06-03 17:34:22   -> Monday
 2         5                    2013-06-04 17:34:22   -> Tuesday
 1         2                    2013-06-03 12:34:22   -> Monday
 1         3                    2013-06-04 17:33:22   -> Tuesday

我希望它在我的SQL语句之后看起来像这样

 Item     Mon     Tues    Wed    Thurs   Fri  Sat   Sun    Average
 -----------------------------------------------------------------------------------
 1        6       3                                        5
 2                5                                        5

工作原理

对于第1项:

由于(10 + 2)/ 2天,您可以看到周一平均值为6 星期二的平均值只有3,因为它只在星期二发生一次。 第1项的平均值为5,因为(10 + 2 + 3)/ 3 = 5

对于第2项:

它仅在星期二发生一次,因此第2项的星期二平均值为5。 平均值为5,因为它只发生一次,因此5/1 = 5。

到目前为止,我想出了以下SQL语句,该语句旨在显示按工作日细分的每个项目的平均ActiveTime以及每个项目的总体平均ActiveTime:

Select *,((ISNULL([Sunday],0) +ISNULL([Monday],0)+ ISNULL([Tuesday],0)+ 
         ISNULL([Wednesday],0)+ ISNULL([Thursday],0)+ISNULL([Friday],0)+
         ISNULL([Saturday],0)) / 
         ( CASE WHEN [Sunday] is null  
           THEN 0 ELSE 1 END +
           CASE WHEN [Monday] is null 
           THEN 0 ELSE 1 END + 
           CASE WHEN [Tuesday] is null 
           THEN 0 ELSE 1 END + 
           CASE WHEN [Wednesday] is null 
           THEN 0 ELSE 1 END + 
           CASE  WHEN [Thursday] is null 
           THEN 0 ELSE 1 END +   
           CASE WHEN [Friday] is null 
           THEN 0 ELSE 1 END +  
           CASE WHEN [Saturday] is null 
           THEN 0 ELSE 1 END )) as Avg 
        FROM ( SELECT * FROM 
             ( 
             SELECT a.ResetTime as ResetTime,a.ApartmentDescription as  Apartment,
             DATENAME(WEEKDAY,a.DateTime) _WEEKDAY 
             FROM tblECEventLog a 
             ) 
             AS v1 PIVOT (AVG(ResetTime) FOR _WEEKDAY IN  
                ([Sunday],[Monday],[Tuesday],[Wednesday],[Thursday],[Friday], [Saturday]) 
             )
             AS v2 
             )
             AS v3

运行上述SQL将产生以下结果:

 Item     Mon     Tues    Wed    Thurs   Fri  Sat   Sun    Average
 -----------------------------------------------------------------------------------
 1        6       3                                        4.5
 2                5                                        5

所以它几乎可以工作,但注意值4.5,它通过做(6 + 3)/ 2这是不正确的,我不想只是添加平均值。 Andybody可以建议改进我的SQL语句,使用每个项目的 实际 平均ActiveTime进行平均计算?

2 个答案:

答案 0 :(得分:5)

您应该可以使用avg() over()来获得结果。这将允许您按每个item分区数据:

avg(ActiveTime) over(partition by item) Avg_Item

所以完整的查询将是:

SELECT item,
  [Sunday],
  [Monday],
  [Tuesday],
  [Wednesday],
  [Thursday],
  [Friday],
  [Saturday],
  Avg_Item
FROM 
( 
  SELECT a.ActiveTime as ActiveTime,a.Item as  Item,DATENAME(WEEKDAY,a.DateTime) _WEEKDAY,
    avg(ActiveTime) over(partition by item) Avg_Item
  FROM TableA a  
) AS v1 PIVOT 
(
  AVG(ActiveTime) 
  FOR _WEEKDAY IN 
(
  [Sunday],[Monday],[Tuesday],[Wednesday],[Thursday],[Friday],[Saturday])
) AS v2;

请参阅SQL Demo

答案 1 :(得分:2)

您可以使用group by代替pivot

select  Item
,       avg(case when datename(weekday, DateTime) = 'Sunday' then ActiveTime end) d1
,       avg(case when datename(weekday, DateTime) = 'Monday' then ActiveTime end) d2
,       avg(case when datename(weekday, DateTime) = 'Tuesday' then ActiveTime end) d3
,       avg(case when datename(weekday, DateTime) = 'Wednesday' then ActiveTime end) d4
,       avg(case when datename(weekday, DateTime) = 'Thursday' then ActiveTime end) d5
,       avg(case when datename(weekday, DateTime) = 'Friday' then ActiveTime end) d6
,       avg(case when datename(weekday, DateTime) = 'Saturday' then ActiveTime end) d7
,       avg(ActiveTime) AllDays
from    TableA
group by
        Item

Example at SQL Fiddle.