Assemble query for specific scenario

Asked

Viewed 36 times

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 FIM previous, 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 with the query below but do not know what is 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!

1 answer

0


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

Evaluate 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.