【发布时间】:2017-03-30 14:27:48
【问题描述】:
我有一个包含以下结构数据的表:
Id | OperationType | ObjectName | dt_created
-- | ------------- | ---------- | ----------
1 | 4 | test.com | 2015-08-30 23:23:57.000
2 | 7 | test.com | 2015-08-30 23:23:57.000
3 | 17 | test.com | 2015-08-30 23:23:57.000
4 | 26 | test.com | 2015-08-30 23:23:57.000
5 | 8 | test.com | 2015-08-30 23:23:57.000
6 | 4 | test.com | 2015-08-30 23:23:57.000
7 | 17 | 123.com | 2015-08-30 23:23:57.000
8 | 18 | 123.com | 2015-08-30 23:23:57.000
9 | 26 | 123.com | 2015-08-30 23:23:57.000
10 | 8 | 123.com | 2015-08-30 23:23:57.000
我想获取操作类型 17 后跟 26
的记录的 ID我尝试了一些方法,例如:
选择 abc.id, abc.PreviousOperationType 从(选择ID, 案子 当 OperationType = 26 然后 Lead(OperationType, 1, 0) over (partition by id order by id) 别的 空值 以 PreviousOperationType 结尾 从操作 其中 dt_created 在 '2015-09-20' 和 '2015-09-30' 之间)为 abc 其中 abc.PreviousOperationType 不为 null 且 abc.PreviousOperationType= 17但无法获得准确的结果。
任何帮助将不胜感激。
谢谢, J
【问题讨论】:
-
向我们展示预期的结果。
-
你使用的是哪个sql server
-
你想要 Id 3 或 4 的结果?
-
看到
dt_created包含一个时间,您可能不想使用between,而是使用where dt_created >= '2015-09-20' and dt_created < '2015-10-01'之类的东西,以免只指定最后一天的第一秒。
标签: sql sql-server select