Distância entre o início e data de fim BigQuery

0

Pergunta

Em BigQuery, eu estou tentando escrever uma consulta que calcula a distância entre a data inicial e final para cada ID. Eu não estou interessado nos pontos entre os intervalos de datas. Eu só preciso pegar a lat/long das datas de início e fim e calcular a distância euclidiana.

Tabela 1 tem esta aparência:

    ID     startdate     enddate
    A      2016-9-16     2016-10-9
    A      2017-3-18     2017-4-9

Tabela 2 tem-se as coordenadas e se parece com isso:

ID  Date        Latitude      Longitude
A   2016-9-16   40.76        -109.33
A   2016-9-17   40.72        -109.33
A   2016-10-9   40.75        -109.33
A   2017-3-18   40.81        -109.33
A   2017-4-8    40.83        -109.33
A   2017-4-9    40.96        -109.32

Minha resultados desejados como seria:

ID t1.startdate   t2.Latitude t2.Longitude t1.enddate t2.Latitude t2.Longitude  distance
A   2017-3-18    40.81       -109.33      2017-4-9    40.96       -109.32      150
A   2016-9-16    40.76       -109.33      2016-10-9   40.75      -109.33       200

Isso é o mais perto que eu cheguei, mas já que não pode ser de vários IDENTIFICAÇÃO, não é a correspondência correta datas de início e fim para cada ID:

SELECT
t1,t2, 
ST_DISTANCE(Point1, Point2 ) as distance
from (
SELECT
    ID, 
      st_geogpoint(Longitude,Latitude) as Point1
      from `t2` AS t2 INNER JOIN  
      `t1` AS t1 ON t1.ID = t2.ID
        WHERE  t1.ID = t2.ID AND
        t2.Date = t1.startdate ) t1,
          (
   SELECT 
        ID, 
        st_geogpoint(Longitude, Latitude) as Point2
        from `t2` AS t2 INNER JOIN  
       `t1` AS t1 ON t1.ID = t2.ID
        WHERE  t1.ID = t2.ID AND
        t2.Date = t1.enddate
    ) t2
WHERE  t1.ID = t2.ID

Os resultados obtidos parecem:

ID t1.startdate   t2.Latitude t2.Longitude t1.enddate t2.Latitude t2.Longitude  distance
A   2016-9-16    40.76       -109.33      2016-10-9    40.75       -109.33      150
A   2016-9-16    40.76       -109.33      2017-4-9     40.96      -109.32       250
A   2017-3-18    40.81       -109.33      2017-4-9     40.96      -109.32       200
A   2017-3-18    40.81       -109.33      2016-10-9    40.75      -109.33       250
google-bigquery sql
2021-11-23 19:03:52
1

Melhor resposta

2

Então, isso pode resolver seu problema:

Eu uso t1 como a origem e JOIN t2 duas vezes sobre as datas (início e fim), em seguida, a referência a esses no ST_DISTANCE. Há talvez uma rápida/de maio de alto desempenho maneira para resolver isso.

WITH t1 as (
        SELECT 'A' AS ID, '2016-9-16'AS startdate, '2016-10-9' AS enddate
        UNION ALL SELECT 'A','2017-3-18', '2017-4-9'
), t2 as (
SELECT 'A' AS ID,'2016-9-16' AS date, 40.76 AS Latitude, -109.33 AS Longitude    
UNION ALL SELECT 'A','2016-9-17',40.72,-109.33 
UNION ALL SELECT 'A','2016-10-9',40.75,-109.33
UNION ALL SELECT 'A','2017-3-18',40.81,-109.33
UNION ALL SELECT 'A','2017-4-8',40.83,-109.33
UNION ALL SELECT 'A','2017-4-9',40.96,-109.32
)

SELECT
  t1.ID, 
  start_point.date AS start_date,
  start_point.Longitude AS start_long,
  start_point.Latitude AS start_lat,
  end_point.date AS end_date,
  end_point.Longitude AS end_long,
  end_point.Latitude AS end_lat,
  ST_DISTANCE(
    ST_GEOGPOINT(start_point.Longitude,start_point.Latitude), 
    ST_GEOGPOINT(end_point.Longitude,end_point.Latitude)) AS distance

FROM t1 
JOIN t2 AS start_point
ON t1.id = start_point.id AND t1.startdate = start_point.date

JOIN t2 AS end_point
ON t1.id = end_point.id AND t1.enddate = end_point.date

Resultados:

Linha ID data_inicial start_long start_lat data_final end_long end_lat distância
1 Um 2016-9-16 -109.33 40.76 2016-10-9 -109.33 40.75 1111.9510117740244
2 Um 2017-3-18 -109.33 40.81 2017-4-9 -109.32 40.96 16700.437093959285
2021-11-24 01:32:33

Em outros idiomas

Esta página está em outros idiomas

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................