对至少有一列具有真值的行进行分组

Group rows where there is at least one column with true value

我有一个table这样的

+-----+-------------------+------+-------+-------+-------+---+
| Row |       email       | year | month | flag1 | flag2 |   |
+-----+-------------------+------+-------+-------+-------+---+
|   1 | user1@example.com | 2018 |     1 | true  | true  |   |
|   2 | user1@example.com | 2018 |     1 | false | true  |   |
|   3 | user1@example.com | 2018 |     1 | true  | true  |   |
|   4 | user2@example.com | 2018 |     1 | false | false |   |
|   5 | user2@example.com | 2018 |     1 | false | false |   |
|   6 | user2@example.com | 2018 |     1 | false | false |   |
|   7 | user3@example.com | 2018 |     1 | true  | false |   |
|   8 | user3@example.com | 2018 |     1 | true  | false |   |
|   9 | user3@example.com | 2018 |     1 | false | false |   |
+-----+-------------------+------+-------+-------+-------+---+

可以用这个语句生成

#standardSQL
WITH table AS (
  SELECT "user1@example.com" as email, 2018 as year, 1 as month, TRUE AS flag1, TRUE as flag2
  UNION ALL
  SELECT "user1@example.com",2018,1,FALSE,TRUE
  UNION ALL
  SELECT "user1@example.com",2018,1,TRUE,TRUE
  UNION ALL
  SELECT "user2@example.com",2018,1,FALSE,FALSE
  UNION ALL
  SELECT "user2@example.com",2018,1,FALSE,FALSE
  UNION ALL
  SELECT "user2@example.com",2018,1,FALSE,FALSE
  UNION ALL
  SELECT "user3@example.com",2018,1,TRUE,FALSE
  UNION ALL
  SELECT "user3@example.com",2018,1,TRUE,FALSE
  UNION ALL
  SELECT "user3@example.com",2018,1,FALSE,FALSE
)

emailyearmonth 分组,输出 table 需要具有 true 值(对于两个 flag列),如果分组数据中至少有一行具有true

结果table应该是这个

+-----+-------------------+------+-------+-------+-------+---+
| Row |       email       | year | month | flag1 | flag2 |   |
+-----+-------------------+------+-------+-------+-------+---+
|   1 | user1@example.com | 2018 |     1 | true  | true  |   |
|   2 | user2@example.com | 2018 |     1 | false | false |   |
|   3 | user3@example.com | 2018 |     1 | true  | false |   |
+-----+-------------------+------+-------+-------+-------+---+

我开始按前 3 列对所有标志进行分组,但现在我不得不确定每个数组中是否至少有一个 true

SELECT email,
  year,
  month,
  ARRAY_AGG(flag1) as flag1,
  ARRAY_AGG(flag2) as flag2
FROM table
GROUP BY 1,2,3
#standardSQL
SELECT email,
  year,
  month,
  LOGICAL_OR(flag1) AS flag1,
  LOGICAL_OR(flag2) AS flag2
FROM table
GROUP BY 1,2,3