Display message as per condition in query

Asked

Viewed 49 times

0

I’m making the difference between dates when the column designacao_circuito are equal and the difference (days) between the dt_hr_fecom is <= 10, I need that in the column Reinc_10_Dias write Reincidente =<10 dias, but in the first record and not in the second.

I made the following command :

select 
        BASE_RREIP.designacao_circuito,
        BASE_RREIP.num_rec,
        BASE_RREIP.dt_abertura_rec,
        BASE_RREIP.dt_hr_abertura,
        BASE_RREIP.[dt_rec_fechamento_tecnico],
        BASE_RREIP.[dt_hr_fetec],
        BASE_RREIP.dt_hr_fecom,
        BASE_RREIP.rreip,
        UDS.[GERENCIA OPERACIONAL SUB] AS GERENCIATECNICASUB,
        BASE_RREIP.centro_funcional_local_anor,
        BASE_RREIP.UDS_Ofensor,
        BASE_RREIP.Piramide,
        BASE_RREIP.Tipo_RECs,
        BASE_RREIP.nome_causa_anor_rec,
        BASE_RREIP.nome_guerra,     
        ROW_NUMBER() OVER(PARTITION BY designacao_circuito ORDER BY designacao_circuito,dt_hr_fecom asc) as qtde,  ----- Numerando a quantidade vezes que se repete a Designação do Circuito

        (designacao_circuito + '-' + cast(row_number() over (partition by designacao_circuito order by dt_hr_abertura) as varchar)) as circuito_RREIP, ---- CONCAT da Designação com o Campo acima

         case when datediff(day,lag(dt_hr_fecom) over (partition by designacao_circuito order by dt_hr_fecom asc),dt_hr_fecom) <= 10 
                 then  'Reincidente =<10 dias' else 'Ñ reincidente =<10 dias' end  as 'Reinc_10_Dias', ----- INFORMA SE É REINCIDENTE OU NÃO        

        MONTH(dt_hr_abertura) as mês_ABERT,
        MONTH(dt_hr_fecom) as mês_FECOM 

 from  BASE_RREIP  LEFT JOIN UDS
                    ON BASE_RREIP.UDS_Ofensor = UDS.UDS

The result was:

inserir a descrição da imagem aqui

Unlike the above print, the information " Repeat offender =<10 days" is appearing in the second record, I need it to always appear in the previous record.

  • Show the query complete, please

  • Ready, I put the full query.

1 answer

0


  • The 'N recurring value =< 10' in the first record is taken as the default of the LAG, because there will be no one before him to make the comparison, so will print the valord of 'I'';

  • As for the other tuples, is there another record with the same value of designat_circuit? If so, it is probably <= 10 of the date dt_hr_fecom, and so be printing 'Repeat offender =< 10 days'...

  • Thanks Denis, a friend gave the tip to use LEAD in the LAG lugas and apparently it worked. case when Datediff (day,dt_hr_fecom,lead(dt_hr_fecom) over (Partition by designacao_circuito order by dt_hr_fecom)) <= 10 then 'Repeat offender =<10 days' Else 'Repeat offender Ñ =<10 days' end as 'Repeat offender 10_days'when case Datediff (day, dt_hr_fecom, lead(dt_hr_fecom) over (Partition by designacao_circuit order by dt_hr_fecom)) <= 10 then 'Repeat offender =<10 days' Else 'Ñ repeat offender =<10 days' end as 'Repeat 10th days''

Browser other questions tagged

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