简体   繁体   English

带有 sum 的 Google Big Query 数组聚合

[英]Google Big Query array aggregation with sum

SQL beginner here. SQL 初学者在这里。

WITH vals AS
  (
    SELECT 1 x, 'a' y, 120 z UNION ALL
    SELECT 1 x, 'a' y, 359 z UNION ALL
    SELECT 1 x, 'b' y, 130 z UNION ALL
    SELECT 2 x, 'a' y, 140 z UNION ALL
    SELECT 2 x, 'c' y, 160 z
  )
SELECT x, ARRAY_AGG(y) as array_aggy,  ARRAY_AGG(z) as array_aggz
FROM vals
GROUP BY x;

The output of this query is这个查询的 output 是

Row     x         array_aggy    array_aggz  
1       1             a            120
                      a            359
                      b            130

2       2             a            140
                      c            160

I want to sum array_aggz values if they have the same value in array_aggy如果它们在 array_aggy 中具有相同的值,我想对 array_aggz 值求和

Row     x         array_aggy    array_aggz  
1       1             a            479
                      b            130

2       2             a            140
                      c            160

Use below下面使用

select x, array_agg(y) as array_aggy,  array_agg(aggz) as array_aggz
from (
  select x, y, sum(z) aggz
  from vals
  group by x, y    
)
group by x             

with output与 output

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL / 大查询中的多个时间序列聚合 - Multiple Time series aggregation in SQL / Big query 为 Google Big Query 替换正则表达式 - Replace a Regex look ahead for Google Big Query 如果数组是 NULL,则 Big Query Array 数据类型相关问题 - Big Query Array data type related issue if the array is NULL Google Cloud DataStream to Bigquery 模板无法将数据同步到大查询 - Google Cloud DataStream to Bigquery template not able to sync data to big query golang中的mongodb聚合查询 - mongodb aggregation query in golang 大查询 - 嵌套在 array_concat 中的 array_agg 不支持正确忽略空值 - big query - array_agg nested within array_concat doesn't support ignore nulls properly 可以在由 Big Query 提供支持的 Google Connected Sheet 列之间插入手动列 - Possibility of inserting manual columns between a Google Connected Sheet columns Powered by Big Query 使用谷歌大查询 sql 将一列中的字符串拆分为多列而不断词 - Using Google big query sql split the string in a column to multiple columns without breaking words 大查询中的 substring - substring in big query Google Big Query + PHP -> 如何在不用完 memory 的情况下获取大型数据集 - Google Big Query + PHP -> How to fetch a large data set without running out of memory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM