Created
March 13, 2023 19:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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