BigQuery 取消嵌套数组 - 获取重复项
BigQuery Unnest an Array - Getting dupliates
我正在 BQ 中处理 GCP 计费查询。但是,在使用成本提取数组时,我得到了错误的值,例如行格式的 unnest returns 数组元素。因此,如果我在单行数组中有 2 个元素,那么我将得到 2 行。
EG:
实际数组:
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
与 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
问题:
我不想要重复的成本值,谁能帮我解决这个问题?
而不是
SELECT labels,cost from cte ,
UNNEST(labels) AS la
尝试
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