SQL: Avoid repeating line and adding values

Asked

Viewed 82 times

1

I have this select:

select 
  DISTINCT (clients.name) as nomeempresa, 
  payments.amount_paid as pagamento, 
  receipts.original_amount as recebimento
FROM 
  payments, 
  receipts, 
  receipt_status, 
  payment_status, 
  clients
WHERE 
  payments.id = payment_status.id AND
  receipts.id = receipt_status.id AND
  clients.id = payments.client_id AND
  clients.id = receipts.client_id AND
  payments.created_at < CURRENT_DATE AND payments.created_at > CURRENT_DATE-1 OR 
  receipts.created_at < CURRENT_DATE AND receipts.created_at > CURRENT_DATE-1
 GROUP BY
  nomeempresa, pagamento, recebimento

But the exit: inserir a descrição da imagem aqui

I have to filter the output and show the total in the next column.

  • 1

    SUM( receipts.original_amount) and removes recipes it from group by

1 answer

1


I was able to get the result with a command line:

SUM( receipts.original_amount) 

Full answer:

select 
  DISTINCT (clients.name) as nomeempresa, 
  SUM (payments.amount_paid) as pagamento, 
  SUM (receipts.original_amount)  as recebimento

Browser other questions tagged

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