Search last registered date in cpf_cnpj sql

-1

I have a basis that I need one of the columns to be the search for the lowest date registered in that CPF / CNPJ at the base.

In the example would create the column data_anterior :

cpf_cnpj  | aplicado| data_aplic | data_anterior
-------------------------------------------------    
123456789 | sim     | 22/03/2018 | missing    
123456789   não     | missing    | 22/03/2018

When the date is the same as data_aplic , the information should not appear.

Can you help me to schedule this function in SQL ?

    
asked by anonymous 18.05.2018 / 01:31

1 answer

0

I suggest you use the structure below:

SELECT
  cpf_cnpj,
  aplicado,
  data_aplic,
  IS_NULL((SELECT MIN(data_aplic)
   FROM tabela T2 
   WHERE T1.cpf_cnpj = T2.cpf_cnpj
     AND T1.data_aplicada <> T2.data_aplic), 'missing') AS data_anterior
FROM tabela T1

data_anterior will be filled by subquery , checking the lowest date of cpf_cnpj consulted; in addition, the ISNULL is used to display the text you want (in this case," missing ").

    
18.05.2018 / 12:31