В нашей базе данных есть такая таблица (извините за форматирование):
Id EventLogId EventLogDate BatchName EventType UserName RoleName Details VerificationResult BatchId TaskId Workstation
4054618 1135432394 2020-02-28 17: 48: 29.000 CRB2020280217150603_1 Comleted task ipugacheva2 Senior DEC operator NULL SAVE_AND_FORWARD 11741417 4736352 SRVTE298
4054617 1135432379 2020-02-28 17: 48: 28.000 CRB2020280217225632_1 Received task oryazepova CWP operator advanced Receiving a task by a user NULL 11741432 4736382 SRVTE297
4054616 1135432360 2020-02-28 17: 48: 26.000 CRB2020280216560813_1 Comleted task oryazepova CVP operator extended NULL SAVE_AND_FORWARD 11741421 4736355 SRVTE297
4054615 1135432308 2020-02-28 17: 48: 11.000 CRB2020280217175055_1 Canceled task ymikheeva2 CVP operator NULL NULL 11741428 4736362 SRVTE296
4054614 1135432246 2020-02-28 17: 47: 58.000 CRB2020280217082625_1 Received task eluzina DPC Operator Receiving a job by a user NULL 11741429 4736378 SRVTE296
4054613 1135432027 2020-02-28 17: 47: 24.000 CRB2020280216230826_4 Received task oyukhimets Senior CVP operator Receiving a job by a user NULL 11741430 4736377 SRVTE295
4054612 1135432013 2020-02-28 17: 47: 21.000 CRB2020280216554384_2 Comleted task oyukhimets Senior DPC operator NULL SAVE_AND_FORWARD 11741411 4736329 SRVTE295
Номера BatchName имеют три события EventType: Received, Cancelled, Comleted (да, здесь отсутствует буква). Мне нужно понять, если какие-либо числа имели повторяющиеся назначения (полученное задание).
Примеры - правильная ситуация:
EventLogDate BatchName EventType UserName
2020-02-28 07: 56: 38.000 WPS2020260218521199_1 Received task sshmeleva2
2020-02-28 07: 57: 02.000 WPS2020260218521199_1 Canceled task sshmeleva2
2020-02-28 09: 48: 45.000 WPS2020260218521199_1 Received task yeremina
2020-02-28 09: 50: 11.000 WPS2020260218521199_1 Comleted task yeremina
Правильная ситуация:
EventLogDate BatchName EventType UserName
2020-02-28 12: 55: 27.000 EUR2020280212384237_1 Received task oguryleva
2020-02-28 12: 58: 01.000 EUR2020280212384237_1 Comleted task oguryleva
2020-02-28 13: 01: 49.000 EUR2020280212414737_1 Received task ivolkova2
2020-02-28 13: 03: 28.000 EUR2020280212414737_1 Canceled task ivolkova2
2020-02-28 13: 03: 30.000 EUR2020280212414737_1 Received task egureva
2020-02-28 13: 06: 52.000 EUR2020280212414737_1 Comleted task egureva
Неверно ситуация:
EventLogDate BatchName EventType UserName
2020-02-28 17: 23: 17.000 CRB2020280216535440_1 Received task ivolkova2
2020-02-28 17: 24: 52.000 CRB2020280216535440_1 Received task adasaeva2
2020-02-28 17: 26: 33.000 CRB2020280216535440_1 Comleted task adasaeva2
В этом случае мы дважды получили задачу Received, которая указывает на какую-то ошибку в программе.
В данный момент я ищу такое BatchName со следующим script:
select *
from
(select pe.BatchName, count (*) as PP1
from ProcessLog pe
where 1 = 1
and pe.EventType = 'Received task'
and pe.EventLogDate> = DATEADD (HOUR, -24, GETDATE ())
group by pe.BatchName
having count (pe.BatchName) > 1) p1,
(select pe.BatchName, count (*) as PP2
from ProcessLog pe
where 1 = 1
and (pe.EventType = 'Comleted task' or pe.EventType = 'Canceled task')
and pe.EventLogDate> = DATEADD (HOUR, -24, GETDATE ())
group by pe.BatchName
having count (pe.BatchName) > 1) p2
where
p1.BatchName = p2.BatchName
and abs (pp1 - pp2) > 1
Но это выглядит довольно отвратительно, и не факт, что во всех случаях он будет работать правильно.
Как сделать скрипт более элегантным и лаконичным (или переделанным) так что выдает требуемое?
Спасибо.