Otimização de linha perniciosa da consulta do SQL
Aqui está a minha estrutura de tabela
MyTable
-----------
ObjectID int (Identity), -- Primary Key
FileName varchar(10),
CreatedDate datetime
...........
...........
...........
Preciso dedicar o tempo necessário para criar um registro em um arquivo ... ou seja, ... Tempo decorrido entre o registro anterior no mesmo arquivo e o registro atual do mesmo arquivo
ou seja ... Se os registros forem
ObjectID FileName CreatedDate (just showing the time part here)
-------- -------- -----------
1 ABC 10:23
2 ABC 10:25
3 DEF 10:26
4 ABC 10:30
5 DEF 10:31
6 DEF 10:35
A saída necessária é ...
ObjectID FileName CreatedDate PrevRowCreatedDate
-------- -------- ----------- ---------------
1 ABC 10:23 NULL
2 ABC 10:25 10:23
3 DEF 10:26 NULL
4 ABC 10:30 10:25
5 DEF 10:31 10:26
6 DEF 10:35 10:31
Até agora recebi essa consulta, mas está demorando muito tempo que o esperado ... Existe uma maneira melhor de fazer isso ...
Select A.ObjectID,
A.FileName
A.CreatedDate as CreatedDate,
B.PrevRowCreatedDate,
datediff("SS", '1900-01-01 00:00:00', Coalesce((A.CreatedDate - B.PrevRowCreatedDate),0)) as secondsTaken
from MyTable as A
Cross Apply (
(Select PrevRowCreatedDate = Max(CreatedDate) from MyTable as BB
where BB.FileName = A.FileName and
BB.CreatedDate < A.CreatedDate
)
) as B
Entre em contato caso precise de mais informações
obrigado