我们有一个每30秒运行一次的SQL作业(SQL Server 2012)
有时,没有任何理由,没有任何"工作失败"消息,作业停止甚至挂起(我们想要分析的行为)。
我们希望实现的是查询日志表并检测记录丢失的时间(不会每30秒发生一次)。
例如:
final case class AssociatedEvent(
localAddress: Address,
remoteAddress: Address,
inbound: Boolean)
extends AssociationEvent
final case class DisassociatedEvent(
localAddress: Address,
remoteAddress: Address,
inbound: Boolean)
extends AssociationEvent
我希望我的问题很明确。
答案 0 :(得分:1)
这显示所有超过30秒的时段
select A.CREATION_DATE as Start, min(N.CREATION_DATE) as Finish
from logTable A
left join logTable N on A.CREATION_DATE < N.CREATION_DATE
group by A.CREATION_DATE
having datediff(second, A.CREATION_DATE, min(N.CREATION_DATE)) > 30
答案 1 :(得分:0)
这是我的“肮脏”解决方案,同时试图通过Luis使解决方案工作(看起来比我的好)
SELECT TOP 1000 [PROCEDURE_NAME]
,[CREATION_DATE]
,[MESSAGE]
,
DATEDIFF(SECOND,
(SELECT top 1 [CREATION_DATE]
FROM [mactac].[mactac].[ERROR_LOG] f
WHERE f.[CREATION_DATE]<c.[CREATION_DATE]
AND [PROCEDURE_NAME] like '%downl%'
AND [MESSAGE] LIKE '11 : **Fin**'
order by [CREATION_DATE] desc)
,c.[CREATION_DATE]) as DIFF
FROM [mactac].[mactac].[ERROR_LOG] as c
WHERE [PROCEDURE_NAME] like '%downl%'
AND [MESSAGE] LIKE '11 : **Fin**'
order by [CREATION_DATE] desc
此解决方案显示所有记录和已用时间:-D 但是......我还没想出如何添加:
AND DIFF&gt; 35
Invalid column name 'DIFF'.
为了过滤更长的时间
答案 2 :(得分:0)
使用CTE可以很容易。
with ordered_logs (creation_date, rnk) as
(
select creation_date, row_number() over (order by creation_date)
from your_log_table
)
select first.creation_date, second.creation_date, datediff(ss, first.creation_date, second.creation_date) as SecondsDifference
from ordered_logs first
inner join ordered_logs second on (first.rnk + 1) = second.rnk
where datediff(ss, first.creation_date, second.creation_date) > 30
按照创建日期对所有记录进行排序,然后将前一个记录与下一个记录连接起来(按时间顺序)。然后它只检查创建时间的差异是否超过30秒。