The AND
condition in the below Having
clause behaves like it filters the row even if one of the conditions is met when its supposed to be logical AND
behavior.
with cte as
( select 'A' as name , 10 as Classes , 11 as Fees union all
select 'A' as name , 10 as Classes , 10 as Fees union all
select 'A' as name , 10 as Classes , 10 as Fees union all
select 'A' as name , 10 as Classes , 10 as Fees union all
select 'A' as name , 10 as Classes , 10 as Fees union all
select 'B' as name , 0 as Classes , 10 as Fees union all
select 'B' as name , 0 as Classes , 10 as Fees union all
select 'B' as name , 1 as Classes , 10 as Fees union all
select 'B' as name , -10 as Classes , 10 as Fees union all
select 'B' as name , 10 as Classes , 11 as Fees
)
-- A has 50 rooms and 51 $ -- so it should not be returned
-- B has 1 room and 51 $ and should be fetched
-- why would the AND in the having clause behave like a row level filter on only the indivigual column
-- instead of the combination
-- The weird thing is the logic behaves as expected when using = but fails when using <>
select name , sum(Classes) , sUM(Fees)
from cte
group by name
having ( SUM(Classes) !=50) AND (sum(Fees) !=51 )
Here is the weird part if you replace the != with an = like shown below then it works fine
with cte as
( select 'A' as name , 10 as Classes , 11 as Fees union all
select 'A' as name , 10 as Classes , 10 as Fees union all
select 'A' as name , 10 as Classes , 10 as Fees union all
select 'A' as name , 10 as Classes , 10 as Fees union all
select 'A' as name , 10 as Classes , 10 as Fees union all
select 'B' as name , 0 as Classes , 10 as Fees union all
select 'B' as name , 0 as Classes , 10 as Fees union all
select 'B' as name , 1 as Classes , 10 as Fees union all
select 'B' as name , -10 as Classes , 10 as Fees union all
select 'B' as name , 10 as Classes , 11 as Fees
)
-- A has 50 rooms and 51 $ -- so it should not be returned
-- B has 1 room and 51 $ and should be fetched
-- why would the AND in the having clause behave like a row level filter on only the indivigual column
-- instead of the combination
-- The weird thing is the logic behaves as expected when using = but fails when using <>
select name , sum(Classes) , sUM(Fees)
from cte
group by name
having ( SUM(Classes) =1) AND (sum(Fees) =51 )
Am I missing something when it comes to the way Group by
works? I know I can get around the issue but I don't understand why it behaves this way.
This is what I get
This is what I am supposed to get
after some discuss, try use this
having not(SUM(Classes) =50 AND sum(Fees) =51)
and OP you gotta think twice when you writing boolean.