I have a requirement in redshift where I need to combine the result if the data is continuous. I have the following table where user_id, product_id is varchar and login_time, log_out_time is the timestamp.
user_id product_id login_time log_out_time
----------------------------------------------------------------------
ashok facebook 1/1/2017 1:00:00 AM 1/1/2017 2:00:00 AM
ashok facebook 1/1/2017 2:00:00 AM 1/1/2017 3:00:00 AM
ashok facebook 1/1/2017 3:00:00 AM 1/1/2017 4:00:00 AM
ashok linked_in 1/1/2017 5:00:00 AM 1/1/2017 6:00:00 AM
ashok linked_in 1/1/2017 6:00:00 AM 1/1/2017 7:00:00 AM
ashok facebook 1/1/2017 8:00:00 AM 1/1/2017 9:00:00 AM
ram facebook 1/1/2017 9:00:00 AM 1/1/2017 10:00:00 AM
ashok linked_in 1/1/2017 7:00:00 AM 1/1/2017 8:00:00 AM
I need to combine the result if the data is continuous for a given user_id for each product. So my conclusion should look like
user_id product_id login_time log_out_time
----------------------------------------------------------------------
ashok facebook 1/1/2017 1:00:00 AM 1/1/2017 4:00:00 AM
ashok facebook 1/1/2017 8:00:00 AM 1/1/2017 9:00:00 AM
ashok linked_in 1/1/2017 5:00:00 AM 1/1/2017 8:00:00 AM
ram facebook 1/1/2017 9:00:00 AM 1/1/2017 10:00:00 AM
I tried the following query, but it didn’t help me,
SELECT user_id, product_id, MIN(login_time), MAX(log_out_time) FROM TABLE_NAME GROUP BY user_id, product_id
The above query does not give my desired result, since it does not have logic for checking data in continuous mode. I need a request for this without using any custom function, but I am allowed to use any built-in redshift function.