Return the average number of days between start_date and end_date across all completed projects in Projects. Ignore rows where either date is NULL, and return a single column avg_completion_days, rounded to 2 decimals.
Input Table(s)
Projects
| Column | Type | Meaning |
|---|
| project_id | INT | Project identifier |
| project_name | VARCHAR(100) | Project name |
| start_date | DATE | Date the project started, may be NULL |
| end_date | DATE | Date the project finished, NULL if not finished |
Primary key: project_id.
Output Contract
Exactly one row with one column:
| Column | Definition |
|---|
| avg_completion_days | Mean of end_date - start_date in days over projects with both dates present, rounded to 2 decimal places |
The result is always a single row, so no ordering is needed.
Example
Projects
| project_id | project_name | start_date | end_date |
|---|
| 1 | Alpha | 2024-01-01 | 2024-01-08 |
| 2 | Beta | 2024-02-01 | NULL |
| 3 | Gamma | NULL | 2024-03-10 |
| 4 | Delta | 2024-04-01 | 2024-04-15 |
Expected result:
Only projects 1 and 4 have both dates. Their durations are 7 and 14 days, and the average is 10.50.
Constraints and Clarifications
-
Projects
has 1 to 100,000 rows.
-
project_id
is the primary key, so each project is counted once.
-
start_date
is on or before
end_date
when both are present.
-
A completed project is one with both dates present. Rows with either date
NULL
are left out of both the sum and the count.
-
The duration is the plain difference in days: a project that starts and ends on the same day has a duration of 0, and 2024-01-01 to 2024-01-08 is 7 days.
-
If no project has both dates, the result is one row with
avg_completion_days
equal to
NULL
.