Skip to content

Instantly share code, notes, and snippets.

@janbenetka
Last active November 20, 2022 12:46
Show Gist options
  • Save janbenetka/27f7015006beea2c42f7e67582d1c43d to your computer and use it in GitHub Desktop.
Save janbenetka/27f7015006beea2c42f7e67582d1c43d to your computer and use it in GitHub Desktop.
[BQ Loop over days] Date loop for backfills #bigquery #sql #date #loop
DECLARE i INT64 DEFAULT 0;
DECLARE DATES ARRAY<DATE>;
DECLARE event_date DATE;
SET DATES = GENERATE_DATE_ARRAY(DATE(2019,1,1), DATE(2019, 12, 31), INTERVAL 1 DAY);
LOOP
SET i = i + 1;
IF i > ARRAY_LENGTH(DATES) THEN
LEAVE;
END IF;
SET event_date = DATES[ORDINAL(i)];
DELETE FROM `uc-puzzle-data.xxx.yyy` WHERE local_event_date = event_date;
INSERT INTO `uc-puzzle-data.xxx.yyy`
WITH
.....
;
END LOOP;
@cyanred9
Copy link

love your code. thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment