Table by the name Products where column consist of Apple(A) and Grape(G).
A G
1 1
1 1
1 Null
I ran the Query..
Select count(Apple) as 'Apple' count(Grape) as 'Grape'
from products
where Apple = 1 and Grape = 1
The Output which i get from the above query is
Apple=2
Grape=2
Where i should be getting Apple=3 and Grape=2. Kindly help.
check fiddle too. http://sqlfiddle.com/#!3/126e05/5
Your where condition is only including rows
where Apple = 1 and Grape = 1
so the complete row withgrape NULL
is ignored.It seems this may be safer if you need a total. It handles null values more safely and doesn't depend on different behaviors from the aggregates default handling of nulls, which can trip some queries up.