W
W
Webber2021-02-03 11:20:21
PostgreSQL
Webber, 2021-02-03 11:20:21

How to make a selection by interval and condition?

There is a field events table:
id, operation_id, type, date

1, 100, open, 2021-02-03 07:36:55
2, 100, open_page_1, 2021-02-03 07:36:55
3, 100, close_page_1, 2021-02-03 07: 36:56
4, 200, open, 2021-02-03 07:36:55
5, 200, open_page_2, 2021-02-03 07:36:55

We need to take all the fields for the last hour and only those with open types , open_page_1 and this operation_id has no other events

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Slava Rozhnev, 2021-02-03
@AKLZephyr

Use for health:

select * 
from events
where 
  date > current_timestamp - interval '1 hour'
  and type in ('open', 'open_page_1')
  and not exists (
    select type from events e 
    	where e.operation_id = events.operation_id
    		and type not in ('open', 'open_page_1')
  )
;

Share SQL code

F
Fallenyasha, 2021-02-03
@Fallenyasha

About filtering by types

select operation_id 
from <table>
group by operation_id 
having sum(case when type in ('open', 'open_page_1') then 1 else 3 end) = 2

K
ky0, 2021-02-03
@ky0

What's stopping you?
https://www.postgresql.org/docs/current/functions-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question