我有一张这样的桌子
+-----+-------------------+------+-------+-------+-------+---+
| 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
)
email
按, year
,分组month
,输出表需要有true
值(对于两列中的每一flag
列),如果在分组数据中至少有一行有true
值
结果表应该是这个
+-----+-------------------+------+-------+-------+-------+---+
| 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