有人可以帮助我理解为什么这不起作用?查询无法识别我的别名“overall_avg”...可能是自从我最近开始使用续集以来的一个主要错误。谢谢!
SELECT
start_station_id,
AVG(tripduration) AS duration_per_station,
overall_avg - duration_per_station AS difference,
(SELECT
AVG(tripduration)
FROM bigquery-public-data.new_york_citibike.citibike_trips) AS overall_avg,
overall_avg - duration_per_station AS difference,
FROM bigquery-public-data.new_york_citibike.citibike_trips
GROUP BY start_station_id
我正在尝试拉取start_station_id、每个车站的平均行程持续时间、所有行程的平均值以及每个车站的平均行程持续时间与所有行程的平均值之间的差值。但代码无法识别“overall_avg”来查找每个站点的平均行程持续时间与所有行程的平均值之间的差异。
在 SELECT 语句中,您只能访问 FROM 子句中表中的名称。但是,您在 select 语句中定义了overall_avg 以及duration_per_station,同时您希望在SELECT 内的其他行中使用它们。这是不可能的,因为他们在这个环境中不为人所知。
我建议把overall_avg的计算独立成一个CTE,然后引用。
WITH
OverallAVG as (
SELECT
AVG(tripduration) as overall_avg
FROM
bigquery-public-data.new_york_citibike.citibike_trips
)
SELECT
start_station_id,
AVG(tripduration) AS duration_per_station,
any_value(OverallAVG.overall_avg) - AVG(tripduration) AS difference,
FROM
bigquery-public-data.new_york_citibike.citibike_trips, OverallAVG
GROUP BY
start_station_id