Subtracting a next value based on a condition in the same SQL column

0

I need to create a query that every time I find a value "Sent" in the Status column, then subtract it in the Events column taking into account a next value that is after Sent.

Table that I am using:

 Catalogo    Status       Eventos
 --------    ------       -------
 1012        Recibido     2018-05-24 10:07:46.560
 1000        Enviado      2018-05-25 12:35:07.023
 1000        Revisado     2018-06-15 21:02:32.567
 1234        Declinado    2018-05-01 13:00:35.257
 1000        Enviado      2018-06-16 10:46:41.607
 1000        Recibido     2018-06-20 13:08:27.697
 2135        Aprobado     2018-06-20 16:18:27.240
 1000        Cancelado    2018-06-27 15:11:33.930 

Taking into account the next date of the events column, subtracting the date sent to the next date after sending. (Catalog = 1000)

Assume this form of calculation With the catalog = 1000:

(2018-06-27 15:11:33.930 - 2018-06-15 21:02:32.567) + (2018-06-16 10:46:41.607 - 2018-06-20 13:08:27.697) = 25.45 (Dias)

It would be something similar to:

Catalogo      Resultado
--------      ---------
1012           NULL
1000           25.45
1234           NULL
2135           NULL

Note: I can not use the LEAD or LAG functions because it is SQL 2008.

    
asked by JCaceres 09.07.2018 в 19:30
source

0 answers