总结BigQuery中的重复字段

时间:2016-08-16 15:26:34

标签: sql google-bigquery

我会尽可能清楚地解释我的问题,如果不是,请告诉我。

我有一张表[MyTable],如下所示:

----------------------------------------
|chn:integer | auds:integer (repeated) |
----------------------------------------
|1           |3916                     |
|1           |4983                     |
|1           |6233                     |
|1           |1214                     |
|2           |1200                     |
|2           |900                      |
|2           |2030                     |
|2           |2345                     |
----------------------------------------

Auds总是重复4次。

如果我查询SELECT chn, auds FROM [MyTable] WHERE chn = 1,我会得到以下结果:

-------------------
|Row | chn | auds |
-------------------
|1   |1    |3916  |
|2   |1    |4983  |  
|3   |1    |6233  |  
|4   |1    |1214  |
-------------------

如果我查询SELECT chn, auds FROM [MyTable] WHERE (chn = 1 OR chn = 2),我会得到以下结果:

-------------------
|Row | chn | auds |
-------------------
|1   |1    |1200  |
|2   |1    |900   |  
|3   |1    |2030  |  
|4   |2    |2345  |
-------------------

从逻辑上讲,我获得了两倍的结果,但我想得到的是SUM()auds的重复字段chn = 1的{​​{1}},或者视觉效果,像这样:

chn = 2

我试着做点什么:

-------------------
|Row | chn | auds |
-------------------
|1   |3    |5116  |
|2   |3    |5883  |  
|3   |3    |8263  |  
|4   |3    |3559  |
-------------------

但是我收到以下错误:

SELECT a1+a2 FROM

(SELECT auds AS a1 FROM [MyTable] WHERE chn = 1),
(SELECT auds AS a2 FROM [MyTable] WHERE chn = 2)

3 个答案:

答案 0 :(得分:3)

standard SQL表达这种逻辑要容易得多(在“显示选项”下取消选中“使用旧版SQL”)。这是一个计算auds数组总和的例子:

WITH MyTable AS (
  SELECT
    1 AS chn,
    [2, 3, 4, 5, 6] AS auds
  UNION ALL SELECT
    2 AS chn,
    [7, 8, 9, 10, 11] AS auds
)
SELECT
  chn,
  (SELECT SUM(aud) FROM UNNEST(auds) AS aud) AS auds_sum
FROM MyTable;
+-----+----------+
| chn | auds_sum |
+-----+----------+
|   1 |       20 |
|   2 |       45 |
+-----+----------+

另一个计算chn = 1chn = 2的成对总和(根据您的问题,我认为这是你想要的):

WITH MyTable AS (
  SELECT
    1 AS chn,
    [2, 3, 4, 5, 6] AS auds
  UNION ALL SELECT
    2 AS chn,
    [7, 8, 9, 10, 11] AS auds
)
SELECT
  ARRAY(SELECT first_aud + second_auds[OFFSET(off)]
        FROM UNNEST(first_auds) AS first_aud WITH OFFSET off)
    AS summed_auds
FROM (
  SELECT
    (SELECT auds FROM MyTable WHERE chn = 1) AS first_auds,
    (SELECT auds FROM MyTable WHERE chn = 2) AS second_auds
);
+---------------------+
|    summed_auds      |
+---------------------+
| [9, 11, 13, 15, 17] |
+---------------------+

编辑:在所有行中对相应数组元素求和的又一个示例。这可能不会特别有效,但它应该产生预期的结果:

WITH MyTable AS (
  SELECT
    1 AS chn,
    [2, 3, 4, 5, 6] AS auds
  UNION ALL SELECT
    2 AS chn,
    [7, 8, 9, 10, 11] AS auds
  UNION ALL SELECT
    3 AS chn,
    [-1, -6, 2, 3, 2] AS auds
)
SELECT
  ARRAY(SELECT
          (SELECT SUM(auds[OFFSET(off)]) FROM UNNEST(all_auds))
        FROM UNNEST(all_auds[OFFSET(0)].auds) WITH OFFSET off)
    AS summed_auds
FROM (
  SELECT
    ARRAY_AGG(STRUCT(auds)) AS all_auds
  FROM MyTable
);
+--------------------+
|    summed_auds     |
+--------------------+
| [8, 5, 15, 18, 19] |
+--------------------+

答案 1 :(得分:1)

Elliott的答案总是给我灵感!如果它适合你,请投票并接受他的答案(它应该:o))
同时,希望使用Scalar JS UDF

添加替代选项
CREATE TEMPORARY FUNCTION mySUM(a ARRAY<INT64>, b ARRAY<INT64>)
RETURNS ARRAY<INT64>
LANGUAGE js AS """
  var sum = [];
  for(var i = 0; i < a.length; i++){
     sum.push(parseInt(a[i]) + parseInt(b[i]));
  }  
  return sum
  """;

WITH MyTable  AS (
  SELECT
    1 AS chn,
    [2, 3, 4, 5, 6] AS auds
  UNION ALL SELECT
    2 AS chn,
    [7, 8, 9, 10, 11] AS auds
)
SELECT 
  first_auds.chn AS first_auds_chn, 
  second_auds.chn AS second_auds_chn, 
  mySUM(first_auds.auds, second_auds.auds) AS summed_auds
FROM MyTable AS first_auds
JOIN MyTable AS second_auds
ON first_auds.chn = 1 AND second_auds.chn = 2

我喜欢这个选项,因为它少了多个UNNEST,ARRAY等,所以读起来要干净得多。

答案 2 :(得分:0)

只需将GROUP BYSUM结合使用。

SELECT SUM(auds), chn FROM [MyTable] GROUP BY chn