I'm tring to create ranges using CASE
but I have a problem with counting the numbers of appearances. At the moment my code looks like this:
SELECT
CASE
WHEN engine_capacity < 1 THEN 'less than 1'
WHEN engine_capacity >= 1 AND engine_capacity <= 2 THEN '1.0 - 2.0'
WHEN engine_capacity >= 2 AND engine_capacity < 3 THEN '2.0 - 3.0'
WHEN engine_capacity >= 3 AND engine_capacity < 4 THEN '3.0 - 4.0'
WHEN engine_capacity >= 4 AND engine_capacity < 5 THEN '4.0 - 5.0'
ELSE 'more than 5'
END AS 'Range',
COUNT(1) AS 'Count'
FROM cars
GROUP BY 'Range';
and I have an error:
Each GROUP BY expression must contain at least one column that is not an outer reference.
2条答案
按热度按时间sqyvllje1#
You need to include the full
CASE
statement in theGROUP BY
clause:Or, use a subquery before grouping:
vhipe2zx2#