Skip to content

Instantly share code, notes, and snippets.

@lfy79001
Last active April 11, 2024 02:31
Show Gist options
  • Save lfy79001/2a6410daa0800166b73217297736964c to your computer and use it in GitHub Desktop.
Save lfy79001/2a6410daa0800166b73217297736964c to your computer and use it in GitHub Desktop.
// How many of my deliveries will be delayed due to snowfall?
/*
When it snows in excess of six inches per day, my company experiences delivery delays. How many of my deliveries were impacted during the third week of January for the previous year?
*/
WITH timestamps AS
(
SELECT
DATE_TRUNC(year,DATEADD(year,-1,CURRENT_DATE())) AS ref_timestamp,
LAST_DAY(DATEADD(week,2 + CAST(WEEKISO(ref_timestamp) != 1 AS INTEGER),ref_timestamp),week) AS end_week,
DATEADD(day, day_num - 7, end_week) AS date_valid_std
FROM
(
SELECT
ROW_NUMBER() OVER (ORDER BY SEQ1()) AS day_num
FROM
TABLE(GENERATOR(rowcount => 7))
)
)
SELECT
country,
postal_code,
date_valid_std,
tot_snowfall_in
FROM
standard_tile.history_day
NATURAL INNER JOIN
timestamps
WHERE
country='US' AND
tot_snowfall_in > 6.0
ORDER BY
postal_code,date_valid_std
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment