Skip to content

Instantly share code, notes, and snippets.

View gidutz's full-sized avatar

Gad Benram gidutz

View GitHub Profile
-- https://stackoverflow.com/questions/41707583/get-total-no-of-days-in-given-month-in-google-bigquery
CREATE TEMP FUNCTION DaysInMonth(d TIMESTAMP) AS (
32 - EXTRACT(DAY FROM DATE_ADD(DATE_TRUNC(CAST (d AS DATE), MONTH), INTERVAL 31 DAY))
);
WITH monthly_cost_table AS (SELECT
account_name,
EXTRACT (MONTH FROM bill_datetime) month,
EXTRACT (YEAR FROM bill_datetime) year,
SUM(cost) monthly_cost