根据 event_time 和 is_active 列计算 ended_at 列
Calculating ended_at column based on event_time and is_active columns
这是一个示例 table,其中包含数据库中单个拆分测试对象的事件。拆分测试可以打开和关闭,如 is_active
.
中所述
EVENT_ID
SPLIT_TEST_ID
BRANCH
IS_ACTIVE
EVENT_TIME
1
595aa50
a
TRUE
2021-11-11 22:53:08.360
2
595aa50
b
TRUE
2021-11-11 22:53:08.360
3
595aa50
a
FALSE
2021-11-11 22:34:39.235
4
595aa50
b
FALSE
2021-11-11 22:34:39.235
5
595aa50
a
TRUE
2021-11-02 23:40:27.001
6
595aa50
b
TRUE
2021-11-02 23:40:27.001
7
595aa50
a
FALSE
2021-11-02 20:54:29.620
8
595aa50
b
FALSE
2021-11-02 20:54:29.620
9
595aa50
a
TRUE
2021-11-02 20:31:08.297
10
595aa50
b
TRUE
2021-11-02 20:31:08.297
11
595aa50
c
FALSE
2021-10-05 20:33:36.394
12
595aa50
b
FALSE
2021-10-05 20:33:36.394
13
595aa50
c
TRUE
2021-09-15 21:33:58.856
14
595aa50
b
TRUE
2021-09-15 21:33:58.856
15
595aa50
c
FALSE
2021-09-08 18:42:35.728
16
595aa50
b
FALSE
2021-09-08 18:42:35.728
17
595aa50
c
TRUE
2021-09-01 23:09:15.596
18
595aa50
b
TRUE
2021-09-01 23:09:15.596
我正在尝试转换 table 以便每次测试 运行 时每个分支都有一行,其中 started_at
和 ended_at
值而不是 event_time
。如果测试还没有关闭,ended_at
的值应该是 null
.
这是我正在寻找的结果:
SPLIT_TEST_ID
BRANCH
STARTED_AT
ENDED_AT
595aa50
a
2021-11-11 22:53:08.360
null
595aa50
b
2021-11-11 22:53:08.360
null
595aa50
a
2021-11-02 23:40:27.001
2021-11-11 22:34:39.235
595aa50
b
2021-11-02 23:40:27.001
2021-11-11 22:34:39.235
595aa50
a
2021-11-02 20:31:08.297
2021-11-02 20:54:29.620
595aa50
b
2021-11-02 20:31:08.297
2021-11-02 20:54:29.620
595aa50
c
2021-09-15 21:33:58.856
2021-10-05 20:33:36.394
595aa50
b
2021-09-15 21:33:58.856
2021-10-05 20:33:36.394
595aa50
c
2021-09-01 23:09:15.596
2021-09-08 18:42:35.728
595aa50
b
2021-09-01 23:09:15.596
2021-09-08 18:42:35.728
我正在努力根据 is_active
拉取 ended_at
。我尝试过
select
split_test_id,
branch,
event_time as started_at,
last_value(started_at) over (partition by split_test_id, branch, is_active order by event_time) as ended_at
from example
where is_active = true
但我知道它们在逻辑上是有缺陷的。
SQL 创建上面的例子 table:
drop table if exists example;
create table example (
event_id varchar, -- unique
split_test_id varchar,
branch varchar,
is_active boolean,
started_at timestamp_ntz
);
insert into example values
('1', '595aa50', 'a', true, '2021-11-11 22:53:08.360'),
('2', '595aa50', 'b', true, '2021-11-11 22:53:08.360'),
('3', '595aa50', 'a', false, '2021-11-11 22:34:39.235'),
('4', '595aa50', 'b', false, '2021-11-11 22:34:39.235'),
('5', '595aa50', 'a', true, '2021-11-02 23:40:27.001'),
('6', '595aa50', 'b', true, '2021-11-02 23:40:27.001'),
('7', '595aa50', 'a', false, '2021-11-02 20:54:29.620'),
('8', '595aa50', 'b', false, '2021-11-02 20:54:29.620'),
('9', '595aa50', 'a', true, '2021-11-02 20:31:08.297'),
('10', '595aa50', 'b', true, '2021-11-02 20:31:08.297'),
('11', '595aa50', 'c', false, '2021-10-05 20:33:36.394'),
('12', '595aa50', 'b', false, '2021-10-05 20:33:36.394'),
('13', '595aa50', 'c', true, '2021-09-15 21:33:58.856'),
('14', '595aa50', 'b', true, '2021-09-15 21:33:58.856'),
('15', '595aa50', 'c', false, '2021-09-08 18:42:35.728'),
('16', '595aa50', 'b', false, '2021-09-08 18:42:35.728'),
('17', '595aa50', 'c', true, '2021-09-01 23:09:15.596'),
('18', '595aa50', 'b', true, '2021-09-01 23:09:15.596');
select
*
from example;
我在重新阅读问题时弄明白了。
with base as (
select
*,
lag(event_time, 1) over (partition by split_test_id, branch order by event_time desc) as ended_at
from example
)
select
split_test_id,
branch,
event_time as started_at,
ended_at
from base
where is_active = true
order by started_at desc
这是一个示例 table,其中包含数据库中单个拆分测试对象的事件。拆分测试可以打开和关闭,如 is_active
.
EVENT_ID | SPLIT_TEST_ID | BRANCH | IS_ACTIVE | EVENT_TIME |
---|---|---|---|---|
1 | 595aa50 | a | TRUE | 2021-11-11 22:53:08.360 |
2 | 595aa50 | b | TRUE | 2021-11-11 22:53:08.360 |
3 | 595aa50 | a | FALSE | 2021-11-11 22:34:39.235 |
4 | 595aa50 | b | FALSE | 2021-11-11 22:34:39.235 |
5 | 595aa50 | a | TRUE | 2021-11-02 23:40:27.001 |
6 | 595aa50 | b | TRUE | 2021-11-02 23:40:27.001 |
7 | 595aa50 | a | FALSE | 2021-11-02 20:54:29.620 |
8 | 595aa50 | b | FALSE | 2021-11-02 20:54:29.620 |
9 | 595aa50 | a | TRUE | 2021-11-02 20:31:08.297 |
10 | 595aa50 | b | TRUE | 2021-11-02 20:31:08.297 |
11 | 595aa50 | c | FALSE | 2021-10-05 20:33:36.394 |
12 | 595aa50 | b | FALSE | 2021-10-05 20:33:36.394 |
13 | 595aa50 | c | TRUE | 2021-09-15 21:33:58.856 |
14 | 595aa50 | b | TRUE | 2021-09-15 21:33:58.856 |
15 | 595aa50 | c | FALSE | 2021-09-08 18:42:35.728 |
16 | 595aa50 | b | FALSE | 2021-09-08 18:42:35.728 |
17 | 595aa50 | c | TRUE | 2021-09-01 23:09:15.596 |
18 | 595aa50 | b | TRUE | 2021-09-01 23:09:15.596 |
我正在尝试转换 table 以便每次测试 运行 时每个分支都有一行,其中 started_at
和 ended_at
值而不是 event_time
。如果测试还没有关闭,ended_at
的值应该是 null
.
这是我正在寻找的结果:
SPLIT_TEST_ID | BRANCH | STARTED_AT | ENDED_AT |
---|---|---|---|
595aa50 | a | 2021-11-11 22:53:08.360 | null |
595aa50 | b | 2021-11-11 22:53:08.360 | null |
595aa50 | a | 2021-11-02 23:40:27.001 | 2021-11-11 22:34:39.235 |
595aa50 | b | 2021-11-02 23:40:27.001 | 2021-11-11 22:34:39.235 |
595aa50 | a | 2021-11-02 20:31:08.297 | 2021-11-02 20:54:29.620 |
595aa50 | b | 2021-11-02 20:31:08.297 | 2021-11-02 20:54:29.620 |
595aa50 | c | 2021-09-15 21:33:58.856 | 2021-10-05 20:33:36.394 |
595aa50 | b | 2021-09-15 21:33:58.856 | 2021-10-05 20:33:36.394 |
595aa50 | c | 2021-09-01 23:09:15.596 | 2021-09-08 18:42:35.728 |
595aa50 | b | 2021-09-01 23:09:15.596 | 2021-09-08 18:42:35.728 |
我正在努力根据 is_active
拉取 ended_at
。我尝试过
select
split_test_id,
branch,
event_time as started_at,
last_value(started_at) over (partition by split_test_id, branch, is_active order by event_time) as ended_at
from example
where is_active = true
但我知道它们在逻辑上是有缺陷的。
SQL 创建上面的例子 table:
drop table if exists example;
create table example (
event_id varchar, -- unique
split_test_id varchar,
branch varchar,
is_active boolean,
started_at timestamp_ntz
);
insert into example values
('1', '595aa50', 'a', true, '2021-11-11 22:53:08.360'),
('2', '595aa50', 'b', true, '2021-11-11 22:53:08.360'),
('3', '595aa50', 'a', false, '2021-11-11 22:34:39.235'),
('4', '595aa50', 'b', false, '2021-11-11 22:34:39.235'),
('5', '595aa50', 'a', true, '2021-11-02 23:40:27.001'),
('6', '595aa50', 'b', true, '2021-11-02 23:40:27.001'),
('7', '595aa50', 'a', false, '2021-11-02 20:54:29.620'),
('8', '595aa50', 'b', false, '2021-11-02 20:54:29.620'),
('9', '595aa50', 'a', true, '2021-11-02 20:31:08.297'),
('10', '595aa50', 'b', true, '2021-11-02 20:31:08.297'),
('11', '595aa50', 'c', false, '2021-10-05 20:33:36.394'),
('12', '595aa50', 'b', false, '2021-10-05 20:33:36.394'),
('13', '595aa50', 'c', true, '2021-09-15 21:33:58.856'),
('14', '595aa50', 'b', true, '2021-09-15 21:33:58.856'),
('15', '595aa50', 'c', false, '2021-09-08 18:42:35.728'),
('16', '595aa50', 'b', false, '2021-09-08 18:42:35.728'),
('17', '595aa50', 'c', true, '2021-09-01 23:09:15.596'),
('18', '595aa50', 'b', true, '2021-09-01 23:09:15.596');
select
*
from example;
我在重新阅读问题时弄明白了。
with base as (
select
*,
lag(event_time, 1) over (partition by split_test_id, branch order by event_time desc) as ended_at
from example
)
select
split_test_id,
branch,
event_time as started_at,
ended_at
from base
where is_active = true
order by started_at desc