I have a table in SQL Server like this:
+----------+-----------+------------+
| DateFrom | Completed | EmployeeID |
+----------+-----------+------------+
DateFrom: date not null -- unique for each EmployeeID
Completed: bit not null
EmployeeID: bigint not null
- Each row belongs to a subperiod defined by a start date and can be completed or not.
- Each employee can have multiple subperiods.
- A period is defined by a list of ordered subperiods until the last subperiod is completed.
I want to create a view that will return the start date of the last period for every EmployeeID like this:
- If there is no Completed is true, get the minimum DateFrom. [The employee has one period which is still not completed]
+----------+-----------+------------+
| DateFrom | Completed | EmployeeID |
+----------+-----------+------------+
|2021-01-01| false | 1 |
|2021-01-05| false | 1 |
|2021-01-09| false | 1 |
|2021-01-10| false | 1 |
|2021-01-07| false | 2 |
|2021-01-15| false | 2 |
+----------+-----------+------------+
Expected Result:
2021-01-01 for EmployeeID = 1
2021-01-07 for EmployeeID = 2
- Else, return the minimum DateFrom after the last Completed is true. [The last period is still not completed]
+----------+-----------+------------+
| DateFrom | Completed | EmployeeID |
+----------+-----------+------------+
|2021-01-01| false | 1 |
|2021-01-05| true | 1 |
|2021-01-09| false | 1 |
|2021-01-10| false | 1 |
|2021-01-07| true | 2 |
|2021-01-15| false | 2 |
+----------+-----------+------------+
Expected Result:
2021-01-09 for EmployeeID = 1
2021-01-15 for EmployeeID = 2
- If the maximum DateFrom has Completed=true, return the minimum DateFrom before the last Completed is true and after the true before it, if exists. [The last period is completed with multiple subperiods]
+----------+-----------+------------+
| DateFrom | Completed | EmployeeID |
+----------+-----------+------------+
|2021-01-01| false | 1 |
|2021-01-05| true | 1 |
|2021-01-09| false | 1 |
|2021-01-10| true | 1 |
|2021-01-07| false | 2 |
|2021-01-15| true | 2 |
+----------+-----------+------------+
Expected Result:
2021-01-09 for EmployeeID = 1
2021-01-07 for EmployeeID = 2
- If the maximum DateFrom has Completed=true and there is no other rows or the row before it has Completed=true, return the maximum DateFrom. [The last period is completed with one subperiod]
+----------+-----------+------------+
| DateFrom | Completed | EmployeeID |
+----------+-----------+------------+
|2021-01-01| false | 1 |
|2021-01-05| false | 1 |
|2021-01-09| true | 1 |
|2021-01-10| true | 1 |
|2021-01-07| true | 2 |
+----------+-----------+------------+
Expected Result:
2021-01-10 for EmployeeID = 1
2021-01-07 for EmployeeID = 2
I am looking for the most optimized solution.
I tried this, but I get a NULL value in the third example:
WITH T AS (
SELECT EmployeeID
, MAX(CASE WHEN Completed = 0 THEN NULL ELSE DateFrom END) MaxDateFrom
FROM TableDates
GROUP BY EmployeeID
)
SELECT TableDates.EmployeeID, MIN(TableDates.DateFrom) DateFrom
FROM T
LEFT JOIN TableDates ON T.EmployeeID = TableDates.EmployeeID
AND (T.MaxDateFrom IS NULL OR TableDates.DateFrom > T.MaxDateFrom)
GROUP BY TableDates.EmployeeID
question from:https://stackoverflow.com/questions/65932964/view-to-get-the-minimum-date-with-a-complicated-condition