DDL:
create table domain_lifecycle (
domain varchar,
timestamp timestamp,
lifecycle varchar);
DML:
insert into domain_lifecycle values
('example_1.com', '2020-01-01 01:01:00', 'start'),
('example_1.com', '2020-01-01 01:01:05', 'end'),
('example_2.com', '2020-01-01 01:01:07', 'start'),
('example_2.com', '2020-01-01 01:01:17', 'end'),
('example_3.com', '2020-01-01 01:01:20', 'start'),
('example_1.com', '2020-01-01 01:01:25', 'start'),
('example_3.com', '2020-01-01 01:01:23', 'end'),
('example_1.com', '2020-01-01 01:01:38', 'end'),
('example_3.com', '2020-01-01 01:01:40', 'start'),
('example_3.com', '2020-01-01 01:01:42', 'end');
Question: find the domains whose any of the lifecycles (the time elapsed between lifecycle start and end) is at least 5 seconds.
I know how to use window function (lag()) to resolve this:
select distinct domain from
(select *,
lag(timestamp) over(partition by domain order by timestamp) as pre_timestamp
from domain_lifecycle) t
where lifecycle = 'end'
and extract(epoch from (timestamp - pre_timestamp)) >= 5;
expected result:
How can we resolve this question without using window function? Thanks.
Update:
I updated the above DML by adding one more lifecycle(the last two rows) for domain example_3.com to make it clear that the 5 seconds criterion only applies to the same lifecycle that has adjacent start and end for the same domain (it could be separated by other domains' lifecycle though).
