This is my data.
username | building | action | timestamp |
---|---|---|---|
user-1 | building-1 | IN | 2024-04-10 01:00:00.000 |
user-1 | building-1 | OUT | 2024-04-10 02:00:00.000 |
user-1 | building-1 | IN | 2024-04-10 02:30:00.000 |
user-1 | building-1 | OUT | 2024-04-10 04:00:00.000 |
user-1 | building-1 | IN | 2024-04-11 10:00:00.000 |
user-1 | building-1 | OUT | 2024-04-11 11:00:00.000 |
user-2 | building-2 | IN | 2024-04-10 08:00:00.000 |
user-2 | building-2 | OUT | 2024-04-10 09:00:00.000 |
user-2 | building-3 | OUT | 2024-04-11 02:30:00.000 |
user-2 | building-4 | IN | 2024-04-11 04:00:00.000 |
user-2 | building-1 | IN | 2024-04-12 10:00:00.000 |
user-2 | building-1 | OUT | 2024-04-12 11:00:00.000 |
I need to calculate swipe in and swipe out time. Sometimes we received partial record and we need to include those records also.
My expected output is:
Followed using windowing functions like lead
, lag
. After using those functions, I’m not able to continue further to get this output.
attached table data:
select 'user-1' as username,'building-1' as building,'IN' as action,'2024-04-10 01:00:00'::timestamp as timestamp
union all
select 'user-1' as username,'building-1' as building,'OUT' as action,'2024-04-10 02:00:00'::timestamp as timestamp
union all
select 'user-1' as username,'building-1' as building,'IN' as action,'2024-04-10 02:30:00'::timestamp as timestamp
union all
select 'user-1' as username,'building-1' as building,'OUT' as action,'2024-04-10 04:00:00'::timestamp as timestamp
union all
select 'user-1' as username,'building-1' as building,'IN' as action,'2024-04-11 10:00:00'::timestamp as timestamp
union all
select 'user-1' as username,'building-1' as building,'OUT' as action,'2024-04-11 11:00:00'::timestamp as timestamp
union all
select 'user-2' as username,'building-2' as building,'IN' as action,'2024-04-10 08:00:00'::timestamp as timestamp
union all
select 'user-2' as username,'building-2' as building,'OUT' as action,'2024-04-10 09:00:00'::timestamp as timestamp
union all
select 'user-2' as username,'building-3' as building,'OUT' as action,'2024-04-11 02:30:00'::timestamp as timestamp
union all
select 'user-2' as username,'building-4' as building,'IN' as action,'2024-04-11 04:00:00'::timestamp as timestamp
union all
select 'user-2' as username,'building-1' as building,'IN' as action,'2024-04-12 10:00:00'::timestamp as timestamp
union all
select 'user-2' as username,'building-1' as building,'OUT' as action,'2024-04-12 11:00:00'::timestamp as timestamp
)
select * from _data
2
Answers
To calculate swipe in and swipe out times in PostgreSQL, you’ll typically use SQL queries to extract the relevant information from your database tables. This involves identifying the fields that store the swipe times and then manipulating the data using SQL functions to calculate the durations. If you provide the table structure and field names, I can help you draft a query tailored to your specific setup.
You can use lead/lag along with some CASE/WHEN/END for this.
Schema (PostgreSQL v15)
Query #1
View on DB Fiddle
Another way could be to use a recursive CTE, which might be easier to understand if the conditions get more complex (and it would also perform differently, though you would need to check this against some actual data).