BQ中STRUCT的聚合

时间:2017-08-28 14:20:20

标签: sql google-bigquery

当我运行以下查询时

WITH total_count AS (
  SELECT
    id,
    pid,
    click_ts,
    stacked ,
    SUM(stacked) OVER (PARTITION BY id, pid) AS total_count
  FROM limited_count),   
stacked AS (
  SELECT *
  FROM total_count
  WHERE stacked > 1
)

SELECT
  id,
  pid,
  total_count,
  SUM(stacked) AS stacked 
FROM stacked
GROUP BY 1, 2, 3

我收到错误:

  

参数类型的聚合函数SUM没有匹配的签名:STRUCT。支持的签名:   SUM(INT64); SUM(FLOAT64)在[21:47]

当我尝试在以下视图上执行SUM(堆叠)时:

WITH total_count AS (
  SELECT
    id,
    pid,
    click_ts,
    stacked ,
    SUM(stacked) OVER (PARTITION BY id, pid) AS total_count
  FROM limited_count), 

SELECT *
FROM total_count
WHERE stacked > 1 

一切正常! 有任何想法如何在同一个查询中?

1 个答案:

答案 0 :(得分:1)

#standardSQL
WITH total_count AS (
  SELECT id, pid, click_ts, stacked ,
  SUM(stacked) OVER (PARTITION BY id, pid) AS total_count
  FROM limited_count), 
stacked AS (
  SELECT *
  FROM total_count
  WHERE stacked > 1 ) 
SELECT id, pid, total_count, SUM(stacked.stacked) AS stacked 
FROM stacked
GROUP BY 1,2, 3