UNNEST表达式引用的列产品既未分组也未汇总

时间:2018-07-12 13:36:35

标签: sql google-bigquery

我有表结构

enter image description here

和此查询:

select user_id,min(timestamp) as first_order_time,max(timestamp) as most_recent_order_time,count(1) as no_orders,
 (SELECT SUM((item_price - extra_discount_total) / IFNULL(exchange_rate,1)) FROM UNNEST(products)) AS sums
from `order` 
where parent_order_id is null
group by 1
having min(timestamp)<timestamp('2017-12-31') and max(timestamp)>timestamp('2018-01-01')

第二行显示错误:

  

UNNEST表达式引用未分组的列产品   也不汇总在[2:89]

exchange_rate是订单表的一部分。

1 个答案:

答案 0 :(得分:2)

#standardSQL
SELECT 
  user_id,
  MIN(TIMESTAMP) AS first_order_time,
  MAX(TIMESTAMP) AS most_recent_order_time,
  COUNT(1) AS no_orders,
 SUM((SELECT SUM((item_price - extra_discount_total) / IFNULL(exchange_rate,1)) 
  FROM UNNEST(products))
 ) AS sums
FROM `order` 
WHERE parent_order_id IS NULL
GROUP BY 1
HAVING MIN(TIMESTAMP)<TIMESTAMP('2017-12-31') AND MAX(TIMESTAMP)>TIMESTAMP('2018-01-01')