-
Notifications
You must be signed in to change notification settings - Fork 3
/
example-bigquery-billing-costs-view.sql
41 lines (41 loc) · 1.41 KB
/
example-bigquery-billing-costs-view.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
SELECT
'<billing_account_name1>' AS billing_account_name
, billing_account_id
, project.id AS project_id
, ROUND(SUM(cost), 2) AS cost_generated
, currency
, DATE_SUB(DATE_TRUNC(current_date, MONTH), INTERVAL 1 MONTH) AS cost_reference_start_date
FROM
`<project1>.<billing_dataset1>.<gcp_billing_export_table1>`
WHERE
project.id IS NOT NULL
-- the cost occurred after cost_reference_start_date (first day of previous month)
AND PARSE_DATE("%Y-%m-%d", FORMAT_TIMESTAMP("%Y-%m-%d", usage_start_time))
>= DATE_SUB(DATE_TRUNC(current_date, MONTH), INTERVAL 1 MONTH)
GROUP BY
billing_account_name
, billing_account_id
, project.id
, currency
, cost_reference_start_date
UNION ALL
SELECT
'<billing_account_name2>' AS billing_account_name
, billing_account_id
, project.id AS project_id
, ROUND(SUM(cost), 2) AS cost_generated
, currency
, DATE_SUB(DATE_TRUNC(current_date, MONTH), INTERVAL 1 MONTH) AS cost_reference_start_date
FROM
`<project2>.<billing_dataset2>.<gcp_billing_export_table2>`
WHERE
project.id IS NOT NULL
-- the cost occurred after cost_reference_start_date (first day of previous month)
AND PARSE_DATE("%Y-%m-%d", FORMAT_TIMESTAMP("%Y-%m-%d", usage_start_time))
>= DATE_SUB(DATE_TRUNC(current_date, MONTH), INTERVAL 1 MONTH)
GROUP BY
billing_account_name
, billing_account_id
, project.id
, currency
, cost_reference_start_date