Context
VLink's employee directory needs to identify active employees who are not currently assigned to any department. Department assignments may be expired, cancelled, inactive, or scheduled for a future date.
Task
Write a PostgreSQL query using an as-of date of 2025-02-15 to find all active VLink employees without a valid current department assignment.
Requirements
- Consider an assignment current only when its status is
ACTIVE, its start date is on or before the as-of date, and its end date is null or on or after the as-of date.
- Return each qualifying employee once, even if assignment history contains multiple rows.
- Include the employee's most recent recorded department assignment, department hierarchy path, and assignment end date when available.
- Exclude employees whose employment status is not
ACTIVE.
- Use the department hierarchy to produce a readable path from the top-level department to the assigned department.
- Sort the results by employee name.
Output details
- Return columns in this order:
employee_id, full_name, email, last_department_name, last_department_path, last_assignment_end_date.
- Sort by
full_name in ascending order.
- When selecting the most recent assignment, break ties by descending
effective_start_date, then descending assignment_id.
- Build department paths with
> between names, beginning at a top-level department.
- Treat assignments as current only when their departments are reachable from a top-level department in the hierarchy.
- Return
NULL for unavailable department names, paths, and assignment end dates.