Is it possible to filter records in SQL Server by using a result expression (weight_class) from a CASE
expression in the WHERE
clause? I can't get it to work because I get an error saying:
Invalid column name 'weight_class'
Code:
SELECT
first_name, last_name,
weight_class = CASE
WHEN weight < 172 THEN 'Welterweight'
WHEN weight <= 192 THEN 'Middleweight'
WHEN weight <= 214 THEN 'Light heavyweight'
WHEN weight <= 220 THEN 'Cruiserweight'
ELSE 'Heavyweight'
END
FROM
athletes
This is how I want to do:
SELECT
first_name, last_name,
weight_class = CASE
WHEN weight < 172 THEN 'Welterweight'
WHEN weight <= 192 THEN 'Middleweight'
WHEN weight <= 214 THEN 'Light heavyweight'
WHEN weight <= 220 THEN 'Cruiserweight'
ELSE 'Heavyweight'
END
FROM
athletes
WHERE
weight_class = 'Cruiserweight'