The document contains a SQL query that processes employee attendance data using Common Table Expressions (CTEs). It identifies periods of 'PRESENT' and 'ABSENT' statuses for each employee, calculating the start and end dates for these periods. The final output lists each employee along with their attendance status and the corresponding date ranges.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
5 views1 page
Amazon SQL Queries-Solution
The document contains a SQL query that processes employee attendance data using Common Table Expressions (CTEs). It identifies periods of 'PRESENT' and 'ABSENT' statuses for each employee, calculating the start and end dates for these periods. The final output lists each employee along with their attendance status and the corresponding date ranges.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1
with cte as
(select *, row_number() over(partition by employee order by employee,
dates) as rn from emp_attendance), cte_present as (select *, row_number() over(partition by employee order by employee, dates) , rn - row_number() over(partition by employee order by employee, dates) as flag from cte where status='PRESENT' ), cte_absent as (select *, row_number() over(partition by employee order by employee, dates) , rn - row_number() over(partition by employee order by employee, dates) as flag from cte where status='ABSENT' ) select employee , first_value(dates) over(partition by employee, flag order by employee, dates) as from_date , last_value(dates) over(partition by employee, flag order by employee, dates range between unbounded preceding and unbounded following) as to_date , status from cte_present union select employee , first_value(dates) over(partition by employee, flag order by employee, dates) as from_date , last_value(dates) over(partition by employee, flag order by employee, dates range between unbounded preceding and unbounded following) as to_date , status from cte_absent order by employee, from_date