Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQLITE Split Datetime to Logon/Logoff

Hy, i want to split the time to 2 columns by Event type.

My Table looks like this:

User EventType Time
name1 Logon 14.08.2022 23:51:12
name2 Logoff 12.08.2022 09:12:44
name2 Logon 11.08.2022 23:51:01
name1 Logoff 16.08.2022 08:27:55

And i want to split the Time with EventType like this in SQLite:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

User Logon Logoff
name1 14.08.2022 23:51:12 16.08.2022 08:27:55
name2 11.08.2022 23:51:01 12.08.2022 09:12:44

any ideas?

>Solution :

assuming there is no double logons without logoff, as in your example, you can do this:

select user, 
       logon_time, 
       logoff_time 
  from (select user, 
               time logon_time,
               lead(time) over(partition by user order by time) logoff_time,
               event_type
          from test_table)
 where event_type = 'Logon';

The LEAD function takes value from next line using ordering from "order by" part. In your case a logoff event takes place only after a logon, so it might working

db_fiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading