对相同的 table 执行 2 个 JOIN

Performing 2 JOINs on same table

我在 Google BigQuery 中使用标准 SQL。我正在尝试列出 2018 年 12 月从格林威治村北到格林威治村南的 tlc 黄色旅行次数(这些是 taxi_zone_geom table 中的 zone_id)和总数。 zone_id 对应于 tlc_yellow_trips_2018 table 中的 pickup_location_id 和 dropoff_location_id。

taxi_zone_geom table:

Row zone_id zone_name   borough zone_geom
1   1   Newark Airport  EWR POLY
2   3   Allerton/Pelham Gardens Bronx   POLYGON((-73.8...

trips_2018 table

Row vendor_id   pickup_datetime dropoff_datetime    passenger_count trip_distance   rate_code   store_and_fwd_flag  payment_type    fare_amount extra   mta_tax tip_amount  tolls_amount    imp_surcharge   total_amount    pickup_location_id  dropoff_location_id 
1   2   2018-05-06T21:13:05 2018-05-06T21:35:33 1   9.83    1   N   1   29  0.5 0.5 6.06    0   0.3 36.36   138 65  
2   1   2018-05-06T21:59:42 2018-05-06T22:25:17 1   10.7    1   N   1   32  0.5 0.5 6.65    0   0.3 39.95   138 25  
3   1   2018-05-06T23:54:05 2018-05-07T00:19:30 2   9.6 1   N   1   30.5    0.5 0.5 6.35    0   0.3 38.15   114 116 

这很好用:

SELECT EXTRACT(MONTH FROM pickup_datetime) AS Month,
COUNT(*) AS Dec_trips_GVNorth_to_GVSouth,
SUM(total_amount) as total,
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2018` AS t 
LEFT JOIN `bigquery-public-data.new_york_taxi_trips.taxi_zone_geom` AS g ON t.pickup_location_id=g.zone_id WHERE g.zone_name='Greenwich Village North'
GROUP BY Month HAVING Month=12

但是,我需要通过连接相同的 table 来 link 到 drop_off,我尝试使用如下的辅助连接来做到这一点:

LEFT JOIN `bigquery-public-data.new_york_taxi_trips.taxi_zone_geom` AS g2 ON t.dropoff_location_id=g2.zone_id WHERE g2.zone_name='Greenwich Village South' 

这会导致错误“语法错误:预期的输入结束但在 [7:1] 处得到了关键字 LEFT”

我做错了什么?谢谢

我将其理解为两个连接,在 WHERE 子句中过滤:

SELECT 
    COUNT(*) AS Dec_trips_GVNorth_to_GVSouth,
    SUM(total_amount) as total
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2018` AS t 
INNER JOIN `bigquery-public-data.new_york_taxi_trips.taxi_zone_geom` AS g1 
    ON t.pickup_location_id = g1.zone_id 
INNER JOIN `bigquery-public-data.new_york_taxi_trips.taxi_zone_geom` AS g2
    ON t.dropoff_location_id = g2.zone_id 
WHERE 
    g1.zone_name = 'Greenwich Village North'
    AND g2.zone_name = 'Greenwich Village South'
    AND pickup_datetime >= date '2018-12-01'
    AND pickup_datetime >= date '2019-01-01'

备注:

  • 因为您想实际过滤上车/下车地点,请使用 INNER JOINs,而不是 LEFT JOINs

  • 我认为不需要明确的 GROUP BY 子句,也不需要 HAVING 子句:您可以直接在 [=11 中过滤目标月份=] 子句,使用半开区间和文字日期

  • 如果table顾名思义只有2018年的数据,那么上面日期范围的过滤条件就多余了