Why can't I use a temporary column in the where clause?
For example, this query:
Select
product_brand,
(CASE WHEN COUNT(product_brand)>50 THEN 1 ELSE 0 END) AS brand_count
FROM
products
WHERE
1
GROUP BY
product_brand
This brings up two columns, one called product_brand
and one called brand_count
. brand_count
is created on the fly and is always 1 or 0 depending on whether or not there are 50 or products with that brand.
All this makes sense to me, except that I can't select only if brand_count = 1
as in this query below:
Select
product_brand,
(CASE WHEN COUNT(product_brand)>50 THEN 1 ELSE 0 END) AS brand_count
FROM
products
WHERE
brand_count = 1
GROUP BY
product_brand
which gives me this error:
#1054 - Unknown column 'brand_count' in 'where clause'
Because it has no idea what that column is until after it's done the processing.
If you want to access the column by that name you would have to use a subquery, otherwise you are going to have to qualify the column without the name you gave it, repeating your case statement.
Because in SQL the columns are first "selected" and then "projected".
If I read your intent correctly, you can re-write this query to read:
This will give you all
product_brands
that have acount > 50
and will also show you the count for each.You have to use the full clause, so you will need:
This is the same for any calculated field in any SQL statement .
To simplify:
won't work, but:
will. It's the same in your case.
Use
HAVING
instead:WHERE
is evaluated before theGROUP BY
.HAVING
is evaluated after.