I am trying to fetch all vendors whose product's quantity (SUM
) is equal to zero. This is what I have done so far but it doesn't return any row nor it gives any error. I am making sure there are products whose quantity is zero and script is running on proper database.
select
VI.Name, Cp.ProductName
from
VendorInfo VI
inner join
VendorTrading VT on VI.Id = VT.VendorId
inner join
CustomerProducts CP on VT.Id = CP.VendorTradingId
group by
VI.Name, CP.ProductName
having
sum(CP.ProductQuantity) = 0
I am confused about HAVING
and WHERE
. What do I do?
UPDATE:
This is the VendorInfo
table:
This is the VendorTrading
table:
This is the CustomerProduct
table:
Now expected result result should be like:
There will be multiple rows of same product but with different Tradedate, So it should first SUM all Product Quantity and if it equals to zero then it returns VendorName and Product name that it has 0 Product quantity in certain period of time. later I also need total count of Vendor whose Product Quantity is 0. Hope it clears everything