Show only positive delay days in SQL query?

Asked

Viewed 773 times

0

I am making a query where I intend to calculate the amount of days of delay in a salary, I would like to show only if there are days of delay, at the moment I am showing days remaining for the salary, which should come zero.

Ex:
Data de vencimento
2015-04-20 00:00:00.000
2015-05-20 00:00:00.000
2015-05-20 00:00:00.000
2015-06-15 00:00:00.000

Resultado para a data de hoje:
0
-30
-30
-56

select a.* 
, b.tipododocumento
, b.contacontabil
, d.nome 
, b.descricao
, a.formapagamentoA
, a.formapagamentoB
, a.data_inc
, a.data_alt
, a.letranota
, a.numeroparcela
,case a.statusregistro 
  when 'A' then 
    DATEDIFF (DAY, a.datavencimento,GETDATE() ) 
  when 'L' then
    0
  end AS diasatraso
 
from 
tb_recebimento a  
left outer join  tb_plano_contas b  on  a.controleplano = b.controleplano                     
left outer join   tb_cliente_fornecedor d   on  a.controlecliente = d.controlecliente         
Full Outer Join   tb_c_vendas c   on  c.controle = a.controlevenda                     
where a.statusregistro = 'A' 
and b.tipododocumento = 'D' 
and a.controleempresa = '1' 
order by a.datavencimento   

  • What is being shown and what is the desired behavior?

  • Add in my question the result of the fields I need, when the date is negative is because it has not yet reached the due date, after that becomes positive. want shows only days after due date

  • Won if the due date is less than today’s date (or a date base date type last date low cnab file of the bank) , the subtraction of the two dates gives the days in arrears , without taking into account holidays or weekend , functions and examples to rodo on the web believe ...

  • @Motta, you don’t understand my question!

  • @itasouza, I should not have understood even , sorry , anyway seems to have been cured, []s

1 answer

2


Seems pretty simple to solve:

select * from (
    select a.* 
    , b.tipododocumento
    , b.contacontabil
    , d.nome 
    , b.descricao
    , a.formapagamentoA
    , a.formapagamentoB
    , a.data_inc
    , a.data_alt
    , a.letranota
    , a.numeroparcela
    ,case a.statusregistro 
      when 'A' then 
        DATEDIFF (DAY, a.datavencimento,GETDATE() ) 
      when 'L' then
        0
      end AS diasatraso

    from 
    tb_recebimento a  
    left outer join  tb_plano_contas b  on  a.controleplano = b.controleplano                     
    left outer join   tb_cliente_fornecedor d   on  a.controlecliente = d.controlecliente         
    Full Outer Join   tb_c_vendas c   on  c.controle = a.controlevenda                     
    where a.statusregistro = 'A' 
    and b.tipododocumento = 'D' 
    and a.controleempresa = '1'  
) tabela where tabela.diasatraso >= 0
order by tabela.datavencimento 
  • I had an error: Message 1033, Level 15, Status 1, Line 29 The ORDER BY clause is invalid in views, inline functions, derived Tables, Subqueries, and common table Expressions, unless TOP or FOR XML is also specified.

  • @itasouza I updated the response.

  • i had already tested so: Incorrect syntax near the keyword 'Where'.

  • I updated it again.

  • The column 'Description' was specified Multiple times for 'table'.

  • Exchange the a.* by specification of columns. descricao appears twice, so the error.

  • It worked!! thank you!

Show 2 more comments

Browser other questions tagged

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