Confusion between HAVING and WHERE, SQL Server

放肆的年华 提交于 2019-12-12 03:53:05

问题


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


回答1:


I believe your problem is this: if the Product Quantity of a vendor is zero, there will be no rows in the Trading table for that vendor, and none in the result of the inner join either. You need to do something like:

select VI.Name, Cp.ProductName
FROM VendorInfo VI 
LEFT OUTER join VendorTrading VT  
on VI.Id = VT.VendorId 
LEFT OUTER join CustomerProducts CP 
on VT.Id = CP.VendorTradingId
GROUP BY VI.Name, CP.ProductName
HAVING  SUM( CP.ProductQuantity ) = 0

then what you are doing with the HAVING clause is correct.




回答2:


I think you should add WHERE TradeDate = 'datehere', because with your sample:

WITH VendorInfo AS (
SELECT  2 as Id,
        100 as Code,
        'Yousuf M/s' as Name
),
VendorTrading AS (
SELECT  1 as Id,
        2 as VendorId,
        '2015-12-25' as TradeDate
),
CustomerProducts AS (
SELECT * FROM (VALUES
(1, 1, 'ULTRA', 0),
(2, 1, 'EXTASY', 5),
(3, 1, 'XXL', 5),
(4, 1, 'KPT', 5),
(5, 1, 'ORANGE', 5),
(6, 1, 'STRAWBERRY', 5),
(7, 1, 'PERFORM', 5),
(8, 1, 'INTENSE', 5),
(9, 1, 'SENSUAL', 5),
(10, 2, 'ULTRA', 0),
(11, 2, 'EXTASY', 5),
(12, 2, 'XXL', 5),
(13, 2, 'KPT', 5),
(14, 2, 'ORANGE', 5),
(15, 2, 'STRAWBERRY', 5),
(16, 2, 'PERFORM', 5),
(17, 2, 'INTENSE', 5),
(18, 2, 'SENSUAL', 5) 
)as cp(Id,VendorTradingId,ProductName,ProductQuantity)
)

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

The result is:

Name       ProductName
---------- -----------
Yousuf M/s ULTRA

(1 row(s) affected)

So it can be more strings with ULTRA with more then zero ProductQuantity on another dates.




回答3:


Thanks every one for your efforts, I finally did it this way.

select  Count(*) From 
(select distinct 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
Where VT.Tradedate = '2015-12-25' and CP.ProductName = 'ULTRA'
GROUP BY VI.Name, Cp.ProductName, CP.ProductQuantity
HAVING  COUNT( CP.ProductQuantity ) = 0) as x

but here is another problem, I posted it as new question. see here. Thanks



来源:https://stackoverflow.com/questions/35684293/confusion-between-having-and-where-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!