[英]SQL query for max date value
我需要编写此查询来获取所有类型为401的记录,除非最新的记录类型为400。ID 16是使我不满意的记录的示例。 我确信这很容易,而且我想不出来,但是我已经坚持了几天。 请帮忙。
PK ID Type Changeddate
1 10 400 9/30/15 20:08
2 11 401 10/7/15 18:55
3 11 401 10/7/15 18:55
4 12 400 10/9/15 20:08
5 12 400 10/9/15 20:08
6 13 401 10/14/15 14:12
7 13 401 10/14/15 14:12
8 13 400 10/15/15 15:06
9 13 400 10/15/15 15:06
10 14 401 10/26/15 17:08
11 14 401 10/26/15 17:08
12 15 401 10/29/15 3:48
13 15 401 10/29/15 3:48
14 15 400 10/29/15 19:52
15 16 400 12/29/15 13:04
16 16 400 12/29/15 13:04
17 16 401 12/29/15 13:04
18 16 401 12/29/15 13:04
19 16 400 12/29/15 13:42
20 16 400 12/29/15 13:42
对于SQL Server,可以使用last_value
窗口函数:
select *
from (
select *
, last_value(Type) over (
partition by ID
order by Changeddate
rows between unbounded preceding and unbounded following)
as last_type
from YourTable yt1
where Type in (400, 401)
) sub
where last_type <> 400
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.