表名:INCLog
UID Incident Number Modified Date Modified By
1 INC000011193511 2/18/2018 12:04 zuck
1 INC000011193511 2/19/2018 1:42 shei
1 INC000011193511 3/14/2018 5:08 byrr
1 INC000011193511 3/17/2018 5:08 byrr
2 INC000011193513 1/23/2018 2:58 r070
2 INC000011193513 1/27/2018 9:27 r070
2 INC000011193513 2/21/2018 5:42 gont
2 INC000011193513 3/16/2018 6:06 r070
3 INC000011193514 2/1/2018 6:07 shei
3 INC000011193514 2/13/2018 6:07 r070
4 INC000011193515 2/20/2018 21:05 moha
4 INC000011193515 2/21/2018 8:05 moha
4 INC000011193515 3/15/2018 18:34 doss
如何使用Select语句返回过去5天内修改的事件编号? 例如; INC000011193511最后一次更新于03/17/2018,该查询应该返回相同票证的整个日志
1 INC000011193511 2/18/2018 12:04 zuck
1 INC000011193511 2/19/2018 1:42 shei
1 INC000011193511 3/14/2018 5:08 byrr
1 INC000011193511 3/17/2018 5:08 byrr
提前感谢您的帮助!
答案 0 :(得分:1)
使用最大窗口函数查找每个事件的上次修改日期。然后与当前日期进行比较。您的查询应该是:
select
*
from (
select
*, mDate = max([modified date]) over (partition by [Incident Number])
from
myTable
) t
where
mDate >= dateadd(dd, -5, getdate())
答案 1 :(得分:0)
如果您只想要特定交易的最近5天交易,那么您可以使用以下查询。
SELECT * FROM INCLog where IncedentNumber ='INC000011193511' AND ModifiedDate = DATEADD(DAY, - 5,GETDATE () )
答案 2 :(得分:0)
我建议这样做:
SELECT Incident_Number, MAX(Modified_Date) maxDate
FROM TempTabel
GROUP BY Incident_Number
HAVING MAX(Modified_Date)) > = dateadd(dd, -5, getdate())
谢谢!
答案 3 :(得分:0)
如果我理解正确,您可以使用group by
子句查找每个事件编号的最大日期,并将其与过去5天进行比较
SELECT t.* FROM table t
CROSS APPLY (
SELECT Incident, MAX([Modified Date]) Mdate FROM table
WHERE Incident = t.Incident
GROUP BY Incident) c
WHERE c.Mdate >= DATEADD(DD, -5 , getdate())