Sql query bringing the products that are with zero balance

Asked

Viewed 89 times

1

I have the tables :

Produto                Departamento        
=========              ========
Id_Prod                id_Departamento                                       
Descricao              Descricao     


Produto_Departamento
=====================
id_Prod
Id_Departamento
Saldo                                              

Having the following scenario, I would like your help to develop this consultation.

I would like to bring all products whose balance sum is = 0. Remembering that the product may be registered in several departments

  • It would not only give a sum of the balance and group by product?

  • Which database are you using? Pro in case of a more specific query, of course.

1 answer

2

You didn’t specify very well, but I believe this example will help you:

select p.id_Prod, prod.Descricao from Produto_Departamento p
inner join Produto prod on prod.Id_Prod=p.Id_Prod
having sum(p.Saldo)>0
group by p.id_Prod,prod.Descricao
  • But this query does not bring the products, only the sum.

  • Yes, just make the Join and bring the products

  • But that’s exactly what he asks in the question. I suggest you add in the answer, so it will answer the question and be complete :)

  • Okay, I completed the answer.

Browser other questions tagged

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