Im working on GCP Billing queries in BQ. But while extracting array with the cost I'm getting wrong values like unnest returns array elements in row format. So if I have 2 elements in an array for a single row then I'll get 2 rows.
EG:
Actual Array:
SELECT
TO_JSON_STRING(labels), cost
FROM
billing_export.gcp_billing_export
WHERE
_PARTITIONTIME >= "2018-08-01 00:00:00"
AND _PARTITIONTIME < "2018-09-01 00:00:00"
AND billing_account_id = "xxx-62378F-xxx"
AND TO_JSON_STRING(labels) = '[{"key":"application","value":"scaled-server"},{"key":"department","value":"hrd"}]'
and cost> 0 limit 10
with Unnest:
with cte as (SELECT
labels, cost
FROM
billing_export.gcp_billing_export
WHERE
_PARTITIONTIME >= "2018-08-01 00:00:00"
AND _PARTITIONTIME < "2018-09-01 00:00:00"
AND billing_account_id = "xxx-62378F-xxxx"
AND TO_JSON_STRING(labels) = '[{"key":"application","value":"scaled-server"},{"key":"department","value":"hrd"}]'
and cost> 0
limit 10 )
select labels,cost from cte ,
UNNEST(labels) AS la
Question:
I don't want duplicate cost value, Can anyone help me with this query?