Skip to content

Instantly share code, notes, and snippets.

@alepuccetti
Created March 8, 2021 11:22
Show Gist options
  • Save alepuccetti/277fe07f200ccac33f98ae4217740fa5 to your computer and use it in GitHub Desktop.
Save alepuccetti/277fe07f200ccac33f98ae4217740fa5 to your computer and use it in GitHub Desktop.
Time Travel with BigQuery
-- returning the total number of devices and events seen by the system at the beginning of the precious hours.
SELECT
count(distinct device_id) as devices,
count(*) as events,
FROM table_a
FOR SYSTEM_TIME AS OF TIMESTAMP_TRUNC(TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 HOUR, HOUR));
-- returning the total number of devices and events seen by the system at a specific timestamp
SELECT
count(distinct device_id) as devices,
count(*) as events,
FROM table_a
FOR SYSTEM_TIME AS OF “point_in_time_timestmap”;
)
CREATE OR REPLACE TABLE `project.dataset.table_a_recovered` as (
SELECT
*
FROM `project_id.datasaet_id.table_a` FOR SYSTEM_TIME AS OF “point_in_time_timestmap”;
)

The gists for "Time Travel with BigQuery" blogpost in puccetti.io

SELECT
*
FROM `project_id.datasaet_id.table_a` FOR SYSTEM_TIME AS OF TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 3 HOUR)
SELECT
*
FROM `project_id.datasaet_id.table_a` FOR SYSTEM_TIME AS OF "point_in_time_timestmap"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment