Complex SQL: fetch the users who logged in consecutively 3 or more times (lead perfect example)
From the login_details table, fetch the users who logged in consecutively 3 or more times.
Table Name: LOGIN_DETAILS
Approach: We need to fetch users who have appeared 3 or more times consecutively in login details table. There is a window function which can be used to fetch data from the following record. Use that window function to compare the user name in current row with user name in the next row and in the row following the next row. If it matches then fetch those records.
–Table Structure:
drop table login_details;
create table login_details(
login_id int primary key,
user_name varchar(50) not null,
login_date date);
delete from login_details;
insert into login_details values
(101, ‘Michael’, current_date),
(102, ‘James’, current_date),
(103, ‘Stewart’, current_date+1),
(104, ‘Stewart’, current_date+1),
(105, ‘Stewart’, current_date+1),
(106, ‘Michael’, current_date+2),
(107, ‘Michael’, current_date+2),
(108, ‘Stewart’, current_date+3),
(109, ‘Stewart’, current_date+3),
(110, ‘James’, current_date+4),
(111, ‘James’, current_date+4),
(112, ‘James’, current_date+5),
(113, ‘James’, current_date+6);
select * from login_details;
output should be James and Stewart
Solution:
SELECT USER_NAME FROM(
SELECT DISTINCT USER_NAME,
case when USER_NAME = lead(USER_NAME) over (order by login_id) and USER_NAME = lead(USER_NAME,2) over (order by login_id) then USER_NAME else null
END AS REPEATED_NAMES
FROM login_details
) WHERE REPEATED_NAMES IS NOT NULL;