Assemble query for specific scenario

1

I have a table with the model below (consistent data):

CODIGO | INICIO | FIM
     1 |   2018 | NULL
     1 |   2017 | 2017
     1 |   2014 | 2016

I need to find the records where the INICIO has the same value as the previous FIM , as below, where the data of lines 1 and 2 are inconsistent:

CODIGO | INICIO | FIM
     1 |   2018 | NULL
     1 |   2017 | 2018
     1 |   2014 | 2016

I tried the query below but I do not know what's wrong ...

SELECT T.CODIGO, T.INICIO, T.FIM, X.*
FROM TABELA T
CROSS APPLY (
                SELECT T1.CODIGO, T1.INICIO, T1.FIM
                FROM TABELA T1
                WHERE T1.CODIGO = T.CODIGO
                AND T1.INICIO = T.FIM    
) X

Thank you!

    
asked by anonymous 29.12.2018 / 17:27

1 answer

0
  

I need to find the records where the START has the same value as the previous END

Rate the following suggestion:

-- código #1
SELECT T1.CODIGO, T1.INICIO, T1.FIM, 
       T2.INICIO as [INICIO L2], T2.FIM as [FIM L2]
  from TABELA as T1
       inner join TABELA as T2 on T2.CODIGO = T1.CODIGO
                                  and T2.INICIO = T1.FIM;
    
29.12.2018 / 20:21