[英]Select rows in one table, adding column where MAX(Date) of rows in other, related table
I have a table containing a set of tasks to perform: 我有一个包含要执行的一组任务的表:
Task
ID Name
1 Washing Up
2 Hoovering
3 Dusting
The user can add one or more Notes to a Note table. 用户可以将一个或多个Notes添加到Note表中。 Each note is associated with a task:
每个笔记都与一个任务相关联:
Note
ID ID_Task Completed(%) Date
11 1 25 05/07/2013 14:00
12 1 50 05/07/2013 14:30
13 1 75 05/07/2013 15:00
14 3 20 05/07/2013 16:00
15 3 60 05/07/2013 17:30
I want a query that will select the Task ID, Name and it's % complete, which should be zero if there aren't any notes for it. 我想要一个查询,它将选择任务ID,名称和它的%完成,如果没有任何注释,它应该为零。 The query should return:
查询应该返回:
ID Name Completed (%)
1 Washing Up 75
2 Hoovering 0
3 Dusting 60
I've really been struggling with the query for this, which I've read is a "greatest n per group" type problem, of which there are many examples on SO, none of which I can apply to my case (or at least fully understand). 我真的一直在努力解决这个问题,我读过这个问题是“每组最大的n”类型问题,其中有很多关于SO的例子,我没有一个可以应用于我的案例(或者至少充分认识)。 My intuition was to start by finding the MAX(Date) for each task in the note table:
我的直觉是从注释表中的每个任务中找到MAX(Date)开始:
SELECT ID_Task,
MAX(Date) AS Date
FROM
Note
GROUP BY
ID_Task
Annoyingly, I can't just add "Complete %" to the above query unless it's contained in a GROUP clause. 令人讨厌的是,我不能只在上面的查询中添加“Complete%”,除非它包含在GROUP子句中。 Argh!
哎呀! I'm not sure how to jump through this hoop in order to somehow get the task table rows with the column appended to it.
我不知道如何跳过这个箍,以某种方式获得任务表行与附加到它的列。 Here is my pathetic attempt, which fails as it only returns tasks with notes and then duplicates task records at that (one for each note, so it's a complete fail).
这是我的可怜尝试,它失败了,因为它只返回带有注释的任务,然后在那里复制任务记录(每个注释一个,所以它完全失败)。
SELECT Task.ID,
Task.Name,
Note.Complete
FROM
Task
JOIN
(SELECT ID_Task,
MAX(Date) AS Date
FROM
Note
GROUP BY
ID_Task) AS InnerNote
ON
Task.ID = InnerNote.ID_Task
JOIN
Note
ON
Task.ID = Note.ID_Task
Can anyone help me please? 有人可以帮我吗?
If we assume that tasks only become more complete, you can do this with a left outer join
and aggregation: 如果我们假设任务变得更加完整,您可以使用
left outer join
和聚合来执行此操作:
select t.ID, t.Name, coalesce(max(n.complete), 0)
from tasks t left outer join
notes n
on t.id = n.id_task
group by t.id, t.name
If tasks can become "less complete" then you want the one with the last date. 如果任务变得“不完整”,那么你想要一个具有最后日期的任务。 For this, you can use
row_number()
: 为此,您可以使用
row_number()
:
select t.ID, t.Name, coalesce(n.complete, 0)
from tasks t left outer join
(select n.*, row_number() over (partition by id_task order by date desc) as seqnum
from notes n
) n
on t.id = n.id_task and n.seqnum = 1;
In this case, you don't need a group by
, because the seqnum = 1
performs the same role. 在这种情况下,您不需要
group by
,因为seqnum = 1
执行相同的角色。
How about this just get the max of completed and group by taskid 怎么样才能获得完成和分组的最大值
SELECT t.ID_Task as ID,n.`name`,MAX(t.completed) AS completed
FROM `task` t RIGHT JOIN `note` n on ( t.ID_Task=n.ID )
GROUP BY t. ID_Task
OR 要么
SELECT t.ID_Task as ID,n.`name`,
(CASE when MAX(t.completed) IS NULL THEN '0' ELSE MAX(t.completed))AS completed
FROM `task` t RIGHT JOIN `note` n on ( t.ID_Task=n.ID )
GROUP BY t. ID_Task
select a.ID,
a.Name,
isnull((select completed
from Note
where ID_Task = b.ID_Task
and Date = b.date),0)
from Task a
LEFT OUTER JOIN (select ID_Task,
max(date) date
from Note
group by ID_Task) b
ON a.ID = b.ID_Task;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.