我有这样的任务进度表1 表1.任务
ID | TaskID | IsPlanned | MondayOfWeekDate| Progress | UserID | TaskCreationDate
--------------------------------------------------------------------------------------------
18998 9917 1 2019-04-01 0 1150 2019-04-04 10:47:52.177
我需要选择进度<100且TaskCreationDate是最新的任务进度
我尝试使用group by和max,但仍然得到重复的结果
到目前为止,这是我的SQL
select TaskProgress.*
from
(select
TaskID, Max(Progress) as Progress, Max(TaskCreationDate) as TaskCreationDate
from
TaskProgress
group by
TaskID) as MaxValueTable
join
TaskProgress on TaskProgress.TaskID = MaxValueTable.TaskID
and MaxValueTable.Progress = TaskProgress.Progress
and TaskProgress.Progress < 100
我希望获得的任务具有最新的创建日期和最高的进度点
ID | TaskID | IsPlanned | MondayOfWeekDate| Progress | UserID | TaskCreationDate
--------------------------------------------------------------------------------------------
28885 19500 0 2019-03-25 90 1717 2019-03-25 07:57:36.560
但是我还是得到这个
ID | TaskID | IsPlanned | MondayOfWeekDate| Progress | UserID | TaskCreationDate
--------------------------------------------------------------------------------------------
28885 19500 0 2019-03-25 90 1717 2019-03-25 07:57:36.560
28696 19500 0 2019-03-11 90 1717 2019-03-16 20:57:57.373
28569 19500 0 2019-03-04 90 1717 2019-03-09 11:29:49.010
28485 19500 0 2019-02-25 90 1717 2019-03-02 18:12:25.587
28368 19500 0 2019-02-18 90 1717 2019-02-23 17:59:21.973
请帮助我纠正。预先谢谢你。
答案 0 :(得分:0)
您错过了MaxValueTable.TaskCreationDate = TaskProgress.TaskCreationDate的条件
select TaskProgress.*
from (
select
TaskID
, Max(Progress) as Progress
, Max(TaskCreationDate) as TaskCreationDate
from TaskProgress
group by TaskID) as MaxValueTable
join TaskProgress on TaskProgress.TaskID = MaxValueTable.TaskID
and MaxValueTable.Progress = TaskProgress.Progress
and TaskProgress.Progress < 100
and MaxValueTable.TaskCreationDate = TaskProgress.TaskCreationDate
答案 1 :(得分:0)
我创建了以下测试方案:
create table TaskProgress
(
TaskID int,
Progress int,
TaskCreationDate date
);
go
insert into TaskProgress values
(1, 10, '2019-04-06'),
(1, 20, '2019-04-07'),
(1, 30, '2019-04-08'),
(1, 60, '2019-04-09'),
(1, 80, '2019-04-10'),
(1, 90, '2019-04-11')
go
有了这个,您的查询似乎可以正常工作。如我所料。 (仅注意,您不需要在表表达式中使用最新的TaskCreationDate,因为您在主查询中不需要或不需要它。)
您真的确定在TaskProgress表中没有重复的TaskID和Progress组合吗?
小更新:
似乎您说的是想要与最年轻的TaskCreationDate匹配的记录。当然也有可能。您可以按以下方式更改查询:
select TaskProgress.*
from
(select
TaskID, Max(Progress) as Progress, Max(TaskCreationDate) as TaskCreationDate
from
TaskProgress
group by
TaskID) as MaxValueTable
join
TaskProgress on TaskProgress.TaskID = MaxValueTable.TaskID
and MaxValueTable.TaskCreationDate = TaskProgress.TaskCreationDate
and TaskProgress.Progress < 100
在这种情况下,您不需要表表达式中的最高进度;没有使用。
编辑2:
AAAAHHHHHH!我知道了!我认为...
您确实需要最高的进度值,因为它应该小于100。因此,您应该检查表表达式的Progress值,而不是TaskProgress的Progress值。
请尝试以下操作:
select TaskProgress.*
from
(select
TaskID, Max(Progress) as Progress, Max(TaskCreationDate) as TaskCreationDate
from
TaskProgress
group by
TaskID) as MaxValueTable
join
TaskProgress on TaskProgress.TaskID = MaxValueTable.TaskID
and MaxValueTable.TaskCreationDate = TaskProgress.TaskCreationDate
and MaxValueTable.Progress < 100
很抱歉造成混乱。
答案 2 :(得分:0)
只需使用TOP
和ORDER BY
:
select top (1) tp.*
from TaskProgress tp
where tp.Progress < 100
order by tp.Progress desc, TaskCreationDate desc;
如果要平放多个行,请使用top (1) with ties
。