Sql查询总和的平均值

时间:2013-06-21 13:40:37

标签: sql

我有以下查询从2个子查询中获取计数,它应该给我两个计数的平均值。它在上一次查询中给出了C1的错误。

Select c0.hour , AVG(c0.frequency)as 'AVG In', AVG(c1.frequency)as 'AVG Out' from
(SELECT  [Network]
,cast ([date time]as date)as 'date'
,datepart(hh,[date time])as 'hour'
,[Scan Type]
,count ([scan type])as frequency    
 FROM [Pallex-DW].[dbo].[Scans]
 where Network like 'fr'and [Scan Type] like '3'
 group by Network ,datepart(hh,[date time]),cast ([date time]as date),[Scan Type])as c0

 Union  

 (SELECT  [Network]
,cast ([date time]as date)as 'date'
,datepart(hh,[date time])as 'hour'
,[Scan Type]
,count ([scan type])as frequency
 FROM [Pallex-DW].[dbo].[Scans] as c1
 where Network like 'fr'and [Scan Type] like '11'
 group by Network ,datepart(hh,[date time]),cast ([date time]as date),[Scan Type])c1

2 个答案:

答案 0 :(得分:1)

您的查询问题实际上是c0。 union在第一个查询中不需要别名:

试试这个版本:

Select
c0.hour,AVG(c0.frequency)as 'AVG In',AVG(c1.frequency)as 'AVG Out'
from
((SELECT  [Network]
    ,cast ([date time]as date)as 'date'
    ,datepart(hh,[date time])as 'hour'
    ,[Scan Type]
    ,count ([scan type])as frequency
  FROM [Pallex-DW].[dbo].[Scans]
  where Network like 'fr'and [Scan Type] like '3'
  group by Network ,datepart(hh,[date time]),cast ([date time]as date),[Scan Type])
  Union  
(SELECT  [Network]
    ,cast ([date time]as date)as 'date'
    ,datepart(hh,[date time])as 'hour'
    ,[Scan Type]
    ,count ([scan type])as frequency
  FROM [Pallex-DW].[dbo].[Scans] as c1
  where Network like 'fr'and [Scan Type] like '11'
  group by Network ,datepart(hh,[date time]),cast ([date time]as date),[Scan Type])) as c

但是,我认为您可以通过删除union并选择一个子查询中的所有行来大大简化查询:

Select c.hour, AVG(case when [scan type] = '3' then 1.0*c.frequency end) as "AVG In",
       AVG(case when [scan type] = '3' then 1.0*c.frequency end) as "AVG Out"
from ((SELECT  [Network], 
               cast([date time]as date)as "date", 
               datepart(hh,[date time])as "hour",
               [Scan Type],
               count ([scan type])as frequency
       FROM [Pallex-DW].[dbo].[Scans]
       where Network like 'fr'and [Scan Type] in ( '3', '11')
       group by Network ,datepart(hh,[date time]),cast ([date time]as date),[Scan Type]
      ) as c
group by hour

您还需要group by子句来获得最终结果。我还将值乘以1.0以将它们转换为非整数值。整数的平均值是整数,这可能会产生误导。我还删除了列名称周围的单引号;使用双引号或方括号(以避免与实际常量混淆)。

答案 1 :(得分:0)

你为什么使用工会?

下面的查询应该可行,我已删除了union。你需要在where子句中放置somthing来加入c1和c2。

Select
c0.hour,AVG(c0.frequency)as 'AVG In',AVG(c1.frequency)as 'AVG Out'
from
(SELECT  [Network]
    ,cast ([date time]as date)as 'date'
    ,datepart(hh,[date time])as 'hour'
    ,[Scan Type]
    ,count ([scan type])as frequency
  FROM [Pallex-DW].[dbo].[Scans]
  where Network like 'fr'and [Scan Type] like '3'
  group by Network ,datepart(hh,[date time]),cast ([date time]as date),[Scan Type])as c0, 
(SELECT  [Network]
    ,cast ([date time]as date)as 'date'
    ,datepart(hh,[date time])as 'hour'
    ,[Scan Type]
    ,count ([scan type])as frequency
  FROM [Pallex-DW].[dbo].[Scans] as c1
  where Network like 'fr'and [Scan Type] like '11'
  group by Network ,datepart(hh,[date time]),cast ([date time]as date),[Scan Type])c1