Je voudrais d'abord unir les tables (dans BigQuery, la syntaxe de l'union est une virgule). Ensuite, il y a deux approches :
- Utilisez les fonctions analytiques FIRST_VALUE et LAST_VALUE.
SELECT id, timestamp_first, timestamp_last, data FROM (SELECT id, timestamp, FIRST_VALUE(timestamp) OVER( PARTITION BY id ORDER BY timestamp ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS timestamp_first, LAST_VALUE(timestamp) OVER( PARTITION BY id ORDER BY timestamp ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS timestamp_last FROM table1, table2, table3
- Utilisez l'agrégation MIN/MAX sur l'horodatage pour rechercher le premier/le dernier, puis rejoignez les mêmes tables.
SELECT a.id id, timestamp_first, timestamp_last, data FROM (SELECT id, data FROM table1,table2,table3) a INNER JOIN (SELECT id, MIN(timestamp) timestamp_first, MAX(timestamp) timestamp_last FROM table1,table2,table3 GROUP BY id) b ON a.id = b.id