Skip to content
Advertisement

COUNT with CASE WHEN is showing the same result when using division

I have the following query which returns the sold products:

I want to calculate the percentage of the sold products comparing to all products for product_category =7 :

I get the result as 100, while I execute each query separately they don’t have the same result.

Advertisement

Answer

count() counts both 0 and 1s, it does not counts NULLs.

Use ELSE null instead of ELSE 0

OR

use SUM() instead of count()

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement