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

时间:2018-10-30 09:42:19

标签: sql google-bigquery

我正在运行以下查询,以返回每个用户购买量最高的类别,这在30天和60天的日期范围内可以正常工作,但是当我尝试在过去100天中运行它时,出现错误消息:

Array index 1 is out of bounds (overflow)

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

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

1 个答案:

答案 0 :(得分:0)

至少有一行有一个空值字段,数组应该是空值,空值或空而不是空数组。 它不是在过去 30 天或过去 60 天,而是在过去 100 天,您至少会发生。这使您的 sql 跳跃。 您需要相应地设置默认值(使用一个空数组或一个函数来保证您将要搜索的字段数最少的数组)并且不允许该字段为空。