我正在学习 SQL 解决 Codewars 任务。我不明白如何计算一列,其中所有相应的状态都设置为相同的值。
这是任务:
一家家庭维护公司使用数据库来跟踪需要在各个房屋执行的任务。数据库有两个表:house_tasks 和task_status。 house_tasks 表包含为 house_id 标识的每个房屋安排的任务列表。 task_status 表跟踪每个任务的进度,包括描述及其当前状态。
房子任务:
task_id (int, primary key): A unique identifier for each task.
house_id (int): The identifier of the house for which the task is scheduled.
task_name (varchar): The name of the task to be performed.
任务状态:
id (int, primary key): A unique identifier for each task status record.
task_id (int): The identifier of the task which this status relates to.
description (varchar): A brief description of the task status.
task_status (varchar): The current status of the task; it can be 'Completed', 'In Progress', or NULL if the status has not been set.
编写一个 SQL 查询,为每个房屋生成报告。报告应包括以下列:
house_id: The identifier of the house.
total_tasks: The total number of tasks scheduled for the house.
completed_tasks: The number of tasks that have been completed. A task is considered completed if all of its corresponding statuses in the task_status table are set to 'Completed'.
incomplete_tasks: The number of tasks that are not completed. A task is considered incomplete if any of its statuses are not 'Completed' or if the task does not have any status record in the task_status table.
The result should be ordered by house_id in descending order.
对于此示例数据:
房子任务:
| task_id | house_id | task_name |
+---------+----------+-----------+
| 1 | 1 | Paint |
| 2 | 1 | Plumb |
| 3 | 1 | Garden |
| 4 | 2 | Electric |
| 5 | 2 | Roof |
| 6 | 3 | Cleanup |
| 7 | 4 | Extra Work|
任务状态:
| task_id | description | task_status |
+---------+-------------+-------------+
| 1 | Desc 1 | Completed |
| 1 | Desc 2 | Completed |
| 1 | Desc 3 | Completed |
| 2 | Desc 4 | In Progress |
| 2 | Desc 5 | In Progress |
| 3 | Desc 6 | In Progress |
| 3 | Desc 7 | Completed |
| 4 | Desc 8 | Completed |
| 5 | Desc 9 | NULL |
| 7 | Desc 10 | NULL |
| 7 | Desc 11 | Completed |
所需的输出如下:
| house_id | total_tasks | completed_tasks | incomplete_tasks |
+----------+-------------+-----------------+------------------+
| 4 | 1 | 0 | 1 |
| 3 | 1 | 0 | 1 |
| 2 | 2 | 1 | 1 |
| 1 | 3 | 1 | 2 |
这是我的查询,不计算正确完成的任务:
SELECT
house_id,
COUNT(DISTINCT house_tasks.task_id) AS total_tasks,
COUNT(DISTINCT CASE WHEN task_status.task_status = 'Completed' THEN house_tasks.task_id END) AS completed_tasks,
COUNT(DISTINCT CASE WHEN task_status.task_status IS NULL OR task_status.task_status <> 'Completed' THEN house_tasks.task_id END) AS incomplete_tasks
FROM
house_tasks
LEFT JOIN
task_status ON house_tasks.task_id = task_status.task_id
GROUP BY
house_id
ORDER BY
house_id DESC;
我的查询显示:
house_id total_tasks completed_tasks incomplete_tasks
4 1 1 1
3 1 0 1
2 2 1 1
1 3 1 2
如评论中所述,似乎每个任务都有多行,每次状态更新时都有一行。
假设任务完成后无法重新打开,计算未完成任务的最简单方法是计算已完成的任务(就像您所做的那样),然后从任务总数中减去它:
SELECT house_id,
total_tasks,
completed_tasks
total_tasks - completed_tasks AS incomplete_tasks
FROM (SELECT house_id,
COUNT(DISTINCT house_tasks.task_id) AS total_tasks,
COUNT(DISTINCT CASE WHEN task_status.task_status = 'Completed'
THEN house_tasks.task_id
END) AS completed_tasks,
FROM house_tasks
LEFT JOIN task_status ON house_tasks.task_id = task_status.task_id
GROUP BY house_id) t
ORDER BY house_id DESC;