Salary validity ranges built with LEAD
The task
The HR warehouse needs a type 2 style table from the salary_changes log, so analysts can join pay to any date. Build it for employees of the Finance department.
Each row of salary_changes starts a salary period.
valid_fromis theeffective_onday.valid_tois the day the period ends, not included: theeffective_onof the employee's next row.- For the last row of an employee who has left,
valid_tois the day aftertermination_date. For the last row of an employee still employed,valid_toisNULL. - Ignore any row whose
effective_onis after the employee'stermination_date. Those are data entry errors.
Return employee_id, salary, valid_from and valid_to. Sort by employee_id, then valid_from.
Expected output
employee_idsalaryvalid_fromvalid_to- Sorted by employee_id ascending, then valid_from ascending.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables People team · click a name to insert it
departments7 rows in the sample
One row per department. Key: department_id.
| int | Unique id. | |
| text | Name. | |
| text | Main office city. |
employees120 rows in the sample
Everyone who has worked here, including people who left. Key: employee_id.
| int | Unique id. | |
| text | Name. | |
| int | Department. NULL for a few people between teams. | |
| int | Their manager (an employee_id). NULL for the CEO. | |
| text | Title. | |
| date | First day. | |
| date | Last day. NULL while employed. | |
| numeric(10,2) | Current (or final) yearly salary, USD. |
salary_changes367 rows in the sample
Every salary someone has had, from the day it applied. Key: employee_id, effective_on.
| int | The employee. | |
| date | First day of this salary. | |
| numeric(10,2) | Yearly salary, USD. |
reviews301 rows in the sample
Year-end review ratings. Key: employee_id, review_year.
| int | The employee. | |
| int | Year reviewed. | |
| int | 1 (low) to 5 (high). |
Related questions
- Campaign by day fact table with cumulative spendhard
- Daily end-of-day balance from transactions with a date spinehard
- A small data quality test suite as one queryeasy
- Active accounts and seats per day from subscription datesmedium
- Agent by month fact table of tickets created and resolvedmedium
- Daily stock levels with a date spine and carry forwardhard
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL