0

我有以下查询从 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
4

2 回答 2

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

于 2013-06-21T13:46:38.807 回答
0

你为什么使用联合?

下面的查询应该可以工作,我已经删除了联合。您需要在 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
于 2013-06-21T13:44:49.083 回答