Skip to content

Instantly share code, notes, and snippets.

@jksprattler
Created March 13, 2023 19:03
Show Gist options
  • Save jksprattler/80519ade571714c4415e103ccc8ad2be to your computer and use it in GitHub Desktop.
Save jksprattler/80519ade571714c4415e103ccc8ad2be to your computer and use it in GitHub Desktop.
GCP BigQuery expression tracking monthly invoice of all resources in a specified project based on assigned labels values
SELECT
labels.key as key, labels.value as value, invoice.month,
(SUM(CAST(cost AS NUMERIC))
+ SUM(IFNULL((SELECT SUM(CAST(c.amount AS NUMERIC))
FROM UNNEST(credits) AS c), 0)))
AS total
FROM `<FIXME_project>.<FIXME_dataset>.gcp_billing_export_resource_v1_<FIXME_BILLING_ACCOUNT_ID>`
CROSS JOIN UNNEST(labels) as labels
ON labels.key = "<FIXME_KEY>" and labels.value = "<FIXME_VALUE>"
WHERE project.name = "<FIXME_PROJECT>"
GROUP BY key, value, invoice.month
ORDER BY invoice.month;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment