Я хотел бы объединить таблицы на основе геометрии , а также ближайшего совпадения отметок времени между Date1 и Date2 .
Ниже для BigQuery Стандартный SQL
SELECT
ARRAY_AGG(STRUCT(df2.PointWKT, df2.Date2, df1.PolygonWKT, df1.Date1)
ORDER BY ABS(TIMESTAMP_DIFF(df2.Date2, df1.Date1, SECOND))
LIMIT 1)[OFFSET(0)].*
FROM `xxx.yyy.df1` AS df1 ,
`xxx.yyy.df2` AS df2
WHERE ST_CONTAINS(df1.PolygonWKT, df2.PointWKT)
GROUP BY TO_JSON_STRING(STRUCT(df2.PointWKT, df2.Date2))
Если применить к образцу данных, аналогичному приведенному в вашем примере -
WITH `xxx.yyy.df1` AS (
SELECT ST_GEOGPOINT(1,2) PolygonWKT, TIMESTAMP '2020-05-03 9:00:00 UTC' Date1 UNION ALL
SELECT ST_GEOGPOINT(1,2), '2020-05-03 9:30:10 UTC' UNION ALL
SELECT ST_GEOGPOINT(1,3), '2020-05-05 12:05:00 UTC' UNION ALL
SELECT ST_GEOGPOINT(1,3), '2020-05-05 12:25:00 UTC' UNION ALL
SELECT ST_GEOGPOINT(1,4), '2020-05-06 18:05:00 UTC'
), `xxx.yyy.df2` AS (
SELECT ST_GEOGPOINT(1,3) PointWKT, TIMESTAMP '2020-05-05 12:00:00 UTC' Date2 UNION ALL
SELECT ST_GEOGPOINT(1,3), '2020-05-05 12:00:10 UTC' UNION ALL
SELECT ST_GEOGPOINT(1,3), '2020-05-05 12:00:20 UTC' UNION ALL
SELECT ST_GEOGPOINT(1,3), '2020-05-05 12:17:00 UTC' UNION ALL /* this value adjusted based on exapected result sample - as it looks as a typo */
SELECT ST_GEOGPOINT(1,4), '2020-05-06 18:00:00 UTC'
)
, вывод будет
Row PointWKT Date2 PolygonWKT Date1
1 POINT(1 3) 2020-05-05 12:00:00 UTC POINT(1 3) 2020-05-05 12:05:00 UTC
2 POINT(1 3) 2020-05-05 12:00:10 UTC POINT(1 3) 2020-05-05 12:05:00 UTC
3 POINT(1 3) 2020-05-05 12:00:20 UTC POINT(1 3) 2020-05-05 12:05:00 UTC
4 POINT(1 3) 2020-05-05 12:17:00 UTC POINT(1 3) 2020-05-05 12:25:00 UTC
5 POINT(1 4) 2020-05-06 18:00:00 UTC POINT(1 4) 2020-05-06 18:05:00 UTC