1;配置-01"> "用户-01"> "Dev"> "2022:08:28"> 2 "配置-02"> "用户-01"> "Dev"> "2022:08:27"> 3 "配置-01"> "用户-01"> "Dev"> "2022:08:26"> 4;配置-01"> "用户-01"> "QA"> "2022:08:25"> 5 "配置-01"> "用户-01"> "QA"> "2022:08:24"> 6;配置20"> "用户-01"> "QA"> "2022:08:23"> 7;配置-01"> "用户-01"> "QA"> "2022:08:22"> 8;配置-01"> "用户-01"> "Prod"> "2022:08:21"> 9;配置-01"> "用户-01"> "Prod"> "2022:08:20"> 10;配置-11"> "用户-02"> "Prod"> "2022:08:19"> 11 "配置15"> "用户-02"> "Prod"> "2022:08:18">
我有一个存储历史数据的表。表格的样本结构类似
配置不知道为什么MySQL和Presto都标记了这一点,但这里是Presto/Trino的方法。
您可以使用窗口函数(特别是lag
来比较"当前"one_answers"以前"记录(并按分组
-- sample data
with dataset (id, config, userid, team, createdon) as (
values (1 , 'config-01', 'user-01', 'Dev', '2022:08:28'),
(2 , 'config-02', 'user-01', 'Dev', '2022:08:27'),
(3 , 'config-01', 'user-01', 'Dev', '2022:08:26'),
(4 , 'config-01', 'user-01', 'QA', '2022:08:25'),
(5 , 'config-01', 'user-01', 'QA', '2022:08:24'),
(6 , 'config-20', 'user-01', 'QA', '2022:08:23'),
(7 , 'config-01', 'user-01', 'QA', '2022:08:22'),
(8 , 'config-01', 'user-01', 'Prod','2022:08:21'),
(9 , 'config-01', 'user-01', 'Prod','2022:08:20'),
(10 , 'config-11', 'user-02', 'Prod','2022:08:19'),
(11 , 'config-15', 'user-02', 'Prod','2022:08:18')
)
-- query
select userid,
team,
max_by(configchanged, createdon) configchanged -- get last change status
from (
select *,
config != coalesce(lag(config) over (partition by userid, team order by createdon), config) configchanged -- coalesce is used just in case there is only one row
from dataset
)
group by userid, team
输出:
userid | 团队 | configchanged
---|---|
用户-01 | QA | false
user-01 | 产品 | false
user-01 | 开发 | true//tr>
user-02 | 产品 | true[/tr>