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!