Take only the values that are different in the query

Asked

Viewed 71 times

0

I made a query that brings the values of credit and debit, but I need to bring only the values that are different from these two fields. At the moment I do not know why it is bringing all. Look at the image: OBS IS IN SQL SERVER

inserir a descrição da imagem aqui

Sql code:

  declare @data date='2018-05-21'
  select * from (select sum (ContabLancValor) as debito,ContabLancNumCtrl, 
  CAST(ContabLancHistComp AS varchar(max)) as NomeCliente  from 
  CONTAB_LANCAMENTO 
  where ContabLancData >=@data
  and ContabLancData <=@data
  and ContabLancCtaCred is not null
  and EmpCod='01.02'
  group by CAST(ContabLancHistComp AS varchar(max)), ContabLancNumCtrl)debito
  inner join(

  select sum (ContabLancValor) as credito,ContabLancNumCtrl from 
  CONTAB_LANCAMENTO 
  where ContabLancData >=@data
  and ContabLancData <=@data
  and ContabLancCtaDeb is not null
  and EmpCod='01.02'
  group by CAST(ContabLancHistComp AS varchar(max)), ContabLancNumCtrl) credito 
  on credito.ContabLancNumCtrl=debito.ContabLancNumCtrl
  where debito.debito<>credito.credito
  • If you need to execute on sql-server, edit the question and remove the other sgbd’s to avoid doubts

  • select A.* from CONTAB_LANCAMENTO A inner join CONTAB_LANCAMENTO B on B.ID = A.ID and A.Valor != B.Valor, with time I elaborate a complete answer

1 answer

0


Felipe, if I understood correctly what you need is to know which releases have divergence between the sum of debts and credits. If this is the case, evaluate if the code below meets you.

-- código #1 v2
;
with agrupaLanc as (
SELECT ContabLancNumCtrl, 
       sum(case when ContabLancCtaCred is not null then ContabLancValor else 0 end) as soma_debito,
       sum(case when ContabLancCtaDeb is not null then ContabLancValor else 0 end) as soma_credito
  from CONTAB_LANCAMENTO
  where ContabLancData = @data
        and EmpCod='01.02'
  group by ContabLancNumCtrl
)
SELECT A.ContabLancNumCtrl, A.soma_debito, A.soma_credito,
       L.ContabLancValor, L.ContabLancHistComp,
       L.ContabLancCtaCred, L.ContabLancCtaDeb
  from agrupaLanc as A
       inner join CONTAB_LANCAMENTO as L on L.ContabLancNumCtrl = A.ContabLancNumCtrl
  where A.soma_debito <> A.soma_credito;
  • My code brings it right, like it brings those who are different. I needed to bring those of the total the name of each. Type are 3 records that add up to that total and I needed to catch them, along with the total

  • Gave the following error in this code: Incorrect syntax near the keyword 'with'. If this statement is a common table Expression, an xmlnamespaces clause or a change tracking context clause, the Previous statement must be terminated with a semicolon.

  • Felipe, the above error message only occurs if the previous WITH command was not terminated with ";". When in doubt, add the character ";" before WITH. I changed code #1 with this suggestion.

Browser other questions tagged

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