0
votes

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

enter image description here

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

enter image description here

Question:

I don't want duplicate cost value, Can anyone help me with this query?

1
Yeah, I have marked this as an Answer :)TheDataGuy
Just now gave an upvote.TheDataGuy
thank you! appreciate itMikhail Berlyant

1 Answers

3
votes

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