gpt4 book ai didi

sql - 扩大日期范围时,数组索引 1 超出范围(溢出)

转载 作者:行者123 更新时间:2023-12-02 20:13:11 31 4
gpt4 key购买 nike

我正在运行以下查询以返回每个用户最常购买的类别,这在 30 天和 60 天的日期范围内运行良好,但是当我尝试在过去 100 天运行它时,我收到错误:

数组索引 1 越界(溢出)

为什么扩大日期范围会导致此错误?

WITH `query_result` AS (

SELECT customDimension.value AS UserID,
SUM(CASE WHEN LOWER(hits_product.v2ProductName) LIKE "% mens%" THEN 1 ELSE 0 END) AS mens,
SUM(CASE WHEN LOWER(hits_product.v2ProductName) LIKE "%womens%" THEN 1 ELSE 0 END) AS womens,
SUM(CASE WHEN LOWER(hits_product.v2ProductName) LIKE "%boys%"
OR LOWER(hits_product.v2ProductName) LIKE "%girls%"
THEN 1 ELSE 0 END) AS kids
FROM `xxx.xxx.ga_sessions_20*` AS t
CROSS JOIN UNNEST(hits) AS hits
CROSS JOIN UNNEST(t.customdimensions) AS customDimension
CROSS JOIN UNNEST(hits.product) AS hits_product
WHERE parse_date('%y%m%d', _table_suffix) between
DATE_sub(current_date(), interval 100 day) and
DATE_sub(current_date(), interval 1 day)
AND customDimension.index = 2
AND hits.eCommerceAction.action_type = "6"
GROUP BY UserID
HAVING
SUM(CASE WHEN LOWER(hits_product.v2ProductName) LIKE "%boys%"
OR LOWER(hits_product.v2ProductName) LIKE "%girls%"
THEN 1 ELSE 0 END) > 0
)
SELECT *,
ARRAY_TO_STRING(ARRAY(
SELECT SPLIT(kv, ':')[OFFSET(0)]
FROM UNNEST(SPLIT(REGEXP_REPLACE(TO_JSON_STRING(t), r'[{"}]', ''))) kv
WHERE LOWER(SPLIT(kv, ':')[OFFSET(0)]) <> LOWER('UserID')
ORDER BY CAST(SPLIT(kv, ':')[OFFSET(1)] AS INT64) DESC
LIMIT 1
), ',') top_purchased_gender
FROM `query_result` t

最佳答案

只需使用[safe_offset (1)]而不是[offset (1)]然后处理null异常

像这样:

ORDER BY CAST(ifnull(SPLIT(kv, ':')[safe_offset (1)], 0) AS INT64) DESC

https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#array_subscript_operator

https://cloud.google.com/bigquery/docs/reference/standard-sql/conditional_expressions#ifnull

关于sql - 扩大日期范围时,数组索引 1 超出范围(溢出),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53061365/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com