2013-06-21 127 views
0

我有以下查询是从2 subquerys采取计数,它应该给我两个计数的平均值。它在最后一个查询中给出C1的错误。Sql查询求和的平均值

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 

回答

0

为什么你使用union?

下面的查询应该工作,我已经删除了union。你需要在where子句中加入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 
+0

您可能想要加入c0和c1 –

+0

我解决了与union的问题......但现在AVG In和AVG out完全一样!为什么? – Egidio

+0

@Egidio如果AVG​​ In和Avg Out相同,那么您没有解决问题。 – Paparazzi

1

与您查询的问题实际上是c0。工会并不需要一个别名第一查询:

试试这个版本:

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以将它们转换为非整数值。整数的平均值是整数,可能会引起误解。我还删除了列名中的单引号;使用双引号或方括号(以避免与实际常量混淆)。

+0

谢谢你们,我尝试让你知道! – Egidio

+0

这是非常优雅的解决方案,但我得到一些null验证! – Egidio