我有一个包含id、时间和事件(A或B(的表。我需要写一个案例(或另一个条件(,在表条目中找到event='B'并计数一次,按id分组。
例如,对于id 1:该表具有与事件B对应的行,因此计数器为1。对于id 2:该表具有与事件B对应的行,因此计数器为1。对于id 3:表没有与事件B对齐,因此计数器为0。对于id 4:该表具有与事件B对应的行,因此计数器为1。
然后它计算百分比,在我的例子中,它将是(1+1+0+1(*100/4=75%
id | 日期时间 | 事件|||
---|---|---|---|---|
1 | 2021-04-01 15:00:00 | A|||
1 | 2021-04-01 15:00:00 | B | ||
1 | 2021-04-01 15:00:00 | B | ||
2 | 2021-04-01 21:00:00 | A | ||
2 | 2021-04-05 21:00:00 | B | ||
3 | 2021-04-05 10:00:00 | A | ||
1 | 2021-04-07 15:00:00 | B | ||
4 | 2021-04-10 17:00:00 | A|||
4 | 2021-04-11 17:00:00 | B | ||
4 | 2021-04-11 17:00:00 | B |
试试这个:
SELECT
uniq(id) AS uniqEventCount,
uniqIf(id, event = 'B') AS uniqBEventCount,
(uniqBEventCount * 100) / uniqEventCount AS percentage
FROM (
/* emulate the test dataset */
SELECT data.1 AS id, data.2 AS DateTime, data.3 AS event
FROM (
SELECT arrayJoin([
(1, '2021-04-01 15:00:00', 'A'),
(1, '2021-04-01 15:00:00', 'B'),
(1, '2021-04-01 15:00:00', 'B'),
(2, '2021-04-01 21:00:00', 'A'),
(2, '2021-04-05 21:00:00', 'B'),
(3, '2021-04-05 10:00:00', 'A'),
(1, '2021-04-07 15:00:00', 'B'),
(4, '2021-04-10 17:00:00', 'A'),
(4, '2021-04-11 17:00:00', 'B'),
(4, '2021-04-11 17:00:00', 'B')]) as data))
/*
┌─uniqEventCount─┬─uniqBEventCount─┬─percentage─┐
│ 4 │ 3 │ 75 │
└────────────────┴─────────────────┴────────────┘
*/