简体   繁体   English

BigQuery Standard SQL Group by 聚合多列

[英]BigQuery Standard SQL Group by aggregate multiple columns

Sample dataset:示例数据集:

|ownerId|category|aggCategory1|aggCategory2|
--------------------------------------------
|  1    |  dog   |  animal    |   dogs     |
|  1    |  puppy |  animal    |   dogs     |
|  2    |  daisy |  flower    |   ignore   |
|  3    |  rose  |  flower    |   ignore   |
|  4    |  cat   |  animal    |   cats     |
 ...

Looking to do a group by that contains number of owners from category, aggCategory1, aggCategory2 for example outputting:希望做一个包含来自类别、aggCategory1、aggCategory2 的所有者数量的分组,例如输出:

|# of owners|summaryCategory|
-----------------------------
|    1      |     dog       |
|    1      |     puppy     |
|    1      |     daisy     |
|    1      |     rose      |
|    1      |     cat       |
|    2      |     animal    |
|    2      |     flower    |
|    1      |     dogs      |
|    2      |     ignore    |
|    1      |     cats      |

Doesn't have to be that format but looking to get the above data points.不必是那种格式,而是希望获得上述数据点。

Thanks!谢谢!

One method is to use union all to unpivot the data and then aggregation in an outer query:一种方法是使用union all对数据进行反透视,然后在外部查询中进行聚合:

SELECT category, COUNT(*)
FROM (SELECT ownerID, category
      FROM t
      UNION ALL
      SELECT ownerID, aggCategory1
      FROM t
      UNION ALL
      SELECT ownerID, aggCategory2
      FROM t
     ) t
GROUP BY category

The more BigQuery'ish way to write this uses arrays:更 BigQuery 的写法是使用数组:

SELECT cat, COUNT(*)
FROM t CROSS JOIN
     UNNEST(ARRAY[category, aggcategory1, aggcategory2]) cat
GROUP BY cat;
SELECT COUNT(T.ownerID), T.category
FROM (
    SELECT ownerID, category
    FROM table
    UNION
    SELECT ownerID, aggCategory1
    FROM table
    UNION
    SELECT ownerID, aggCategory2
    FROM table
) AS T
GROUP BY T.category

With a GROUP BY and the union with all of yours categories columns, it can be good.使用GROUP BY和与您所有类别列的并集,效果会很好。

use union all使用union all

with cte as
(
    SELECT ownerID, category as summaryCategory
    FROM table
    UNION
    SELECT ownerID, aggCategory1 as summaryCategory
    FROM table
    UNION
    SELECT ownerID, aggCategory2 as summaryCategory
    FROM table
) select count(ownerID),summaryCategory from cte group by summaryCategory

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM