i'm currently a student and while doing an SQL excercise i had a question and i tried to do a thing. So this is the main query
SELECT
Date,
Region,
Total_Bags,
Small_Bags,
(Small_Bags / Total_Bags) *100 AS Small_BagsPercent
FROM sql-data-analys-corso.avocado_dataset.avocado_prices
WHERE Total_Bags <>0
ORDER BY Small_BagsPercent
I wanted SQL to show me only Small_BagsPercent > 10 but the query tells me that Unrecognized name: Small_BagsPercent at [9:5] if i write the query like this
SELECT
Date,
Region,
Total_Bags,
Small_Bags,
(Small_Bags / Total_Bags) *100 AS Small_BagsPercent
FROM sql-data-analys-corso.avocado_dataset.avocado_prices
WHERE Total_Bags <>0
AND Small_BagsPercent >10
ORDER BY Small_BagsPercent
and i dont understand why, do i have to use a subquery? How can i do that?
WHERE (Small_Bags / Total_Bags) *100 > 10
or use a cross apply to create the alias first. Note that Small_Bags / Total_Bags can probably become 0 unless you cast(total_bags into a numeric or float first because sql server does integer division