尝试UNNEST时间戳数组字段,但需要GROUP BY

时间:2019-06-05 22:53:43

标签: google-bigquery

我在BigQuery表中有一个TIMESTAMP类型的重复字段。我正在尝试对该字段进行嵌套。但是,我必须按顺序对字段进行分组或汇总。我对SQL不了解,因此可以使用一些帮助。该代码段是一个较大查询的一部分,该查询可以在用GENERATE_TIMESTAMP_ARRAY替换subscription.future_renewal_dates时使用

subscription.future_renewal_dates是ARRAY

TIMESTAMP数组是唯一的(定期订阅),无法使用GENERATE_TIMESTAMP_ARRAY生成,因此我必须先生成日期,然后再上传到BigQuery。 UDF太多了。

SELECT
   subscription.amount AS subscription_amount,
   subscription.status AS subscription_status,
   "1" AS analytic_name,
   ARRAY ( 
   SELECT
      AS STRUCT FORMAT_TIMESTAMP("%x", days) AS type_value, subscription.amount AS analytic_name 
   FROM
      UNNEST(subscription.future_renewal_dates) as days 
   WHERE
      (
         days >= TIMESTAMP("2019-06-05T19:30:02+00:00") 
         AND days <= TIMESTAMP("2019-08-01T03:59:59+00:00") 
      )
) AS forecast 
   FROM
      `mydataset.subscription` AS subscription 
   GROUP BY
      subscription_amount,
      subscription_status,
      analytic_name

无法弄清楚如何成功取消对subscription.future_renewal_dates的嵌套,而不会出现错误“ UNNEST表达式引用了既未分组也未汇总的subscription.future_renewal_dates”

1 个答案:

答案 0 :(得分:1)

当执行GROUP BY时-所有表达式,SELECT中的列(GROUP BY列表中的列除外)应与某些聚合函数一起使用-显然您没有。因此,您需要确定通过分组进行实际尝试才能实现的目标

以下是我认为您要考虑的选项-尽管可以有所不同-但至少您对如何解决它有想法

SELECT
   subscription.amount AS subscription_amount,
   subscription.status AS subscription_status,
   "1" AS analytic_name,
   ARRAY_CONCAT_AGG( ARRAY ( 
   SELECT
      AS STRUCT FORMAT_TIMESTAMP("%x", days) AS type_value, subscription.amount AS analytic_name 
   FROM
      UNNEST(subscription.future_renewal_dates) as days 
   WHERE
      (
         days >= TIMESTAMP("2019-06-05T19:30:02+00:00") 
         AND days <= TIMESTAMP("2019-08-01T03:59:59+00:00") 
      )
)) AS forecast 
   FROM
      `mydataset.subscription` AS subscription 
   GROUP BY
      subscription_amount,
      subscription_status,
      analytic_name