问题
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?
回答1:
instead of
SELECT labels,cost from cte ,
UNNEST(labels) AS la
try
SELECT la, cost from cte ,
UNNEST(labels) AS la
Update
SELECT
ARRAY(
SELECT AS STRUCT
JSON_EXTRACT_SCALAR(kv, '$.key') key,
JSON_EXTRACT_SCALAR(kv, '$.value') value
FROM UNNEST(SPLIT(labels, '},{')) kv_temp,
UNNEST([CONCAT('{', REGEXP_REPLACE(kv_temp, r'^\[{|}]$', ''), '}')]) kv
) labels,
cost
FROM cte
来源:https://stackoverflow.com/questions/53368435/bigquery-unnest-an-array-getting-dupliates