Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 子查询Bigquery上的分组依据和平均值_Sql_Group By_Google Bigquery_Average - Fatal编程技术网

Sql 子查询Bigquery上的分组依据和平均值

Sql 子查询Bigquery上的分组依据和平均值,sql,group-by,google-bigquery,average,Sql,Group By,Google Bigquery,Average,我试图按日期对记录进行分组,并针对特定的一天,获取其中一列(称为延迟)的平均值。我有以下标准SQL查询,当我尝试获取表的平均值和分组依据时,它可以工作,但不能工作: SELECT * From ( SELECT 'ADS-B Average Latency for Asia' as metric_name, 'Daily' as metric_period_type, DATE(timestamp) as metric_date, collection_type, UNIX_SECON

我试图按日期对记录进行分组,并针对特定的一天,获取其中一列(称为延迟)的平均值。我有以下标准SQL查询,当我尝试获取表的平均值和分组依据时,它可以工作,但不能工作:


SELECT * 
From 
(
SELECT 'ADS-B Average Latency for Asia' as metric_name, 'Daily' as metric_period_type,
  DATE(timestamp) as metric_date, collection_type, UNIX_SECONDS(ingestion_time) - UNIX_SECONDS(timestamp) as latency
  FROM `analytics.aoi_table` a
  order by metric_date
) table 

这是表格:

当我尝试使用下面的sql执行上表的avg和group by时,我得到了以下错误:语法错误:Expected“),但在[10:3]处得到了关键字group

导致错误的SQL语句:

#standardSQL

SELECT * 
From 
(
SELECT 'ADS-B Average Latency for Asia' as metric_name,
  DATE(timestamp) as metric_date, collection_type, AVG(UNIX_SECONDS(ingestion_time) - UNIX_SECONDS(timestamp)) as latency
  FROM `ais-data-analysis.customer_analytics.itochu_aoi_table` a
  order by metric_date
  group by metric_date, collection_type
) table 



您的
分组依据
排序依据
顺序不正确。此外,您不需要子查询:

select 'ADS-B Average Latency for Asia' as metric_name,
       DATE(timestamp) as metric_date, collection_type, AVG(UNIX_SECONDS(ingestion_time) - UNIX_SECONDS(timestamp)) as latency
from `ais-data-analysis.customer_analytics.itochu_aoi_table` a
group by metric_date, collection_type
order by metric_date

orderby
子句位于
groupby
子句之后。此外,无需包装查询,您只需执行以下操作:

SELECT 
    'ADS-B Average Latency for Asia' as metric_name,
    DATE(timestamp) as metric_date, 
    collection_type, 
    AVG(UNIX_SECONDS(ingestion_time) - UNIX_SECONDS(timestamp)) as latency
FROM `ais-data-analysis.customer_analytics.itochu_aoi_table` a
GROUP BY metric_date, collection_type
ORDER BY metric_date, collection_type