SELECT 
    device_id, 
    gender, 
    CASE
        WHEN age<20 THEN "20岁及以下"
        WHEN age BETWEEN 20 and 24 THEN "20-24岁"
        WHEN age>=25 THEN "25岁及以上"
        ELSE "其他" 
    END AS age_cut
FROM user_profile;

原先的错误代码是:

SELECT device_id, gender, 
    CASE
    WHEN age<20 then "20岁及以下"
    WHEN age between 20 and 24 then "20-24岁"
    WHEN age>=25 then "25岁及以上"
    ELSE "其他" AS age_cut,
FROM user_profile

对着别人的答案看了半天,没看出问题。只好交给chatgpt。

gpt老师给出回复:

The AS age_cut is placed outside the CASE statement to properly rename the resulting column.

Single quotes (') are used for the string literals, which is the standard in SQL.

改了下格式,没改单双引号,再测试了一下,还是没过。

问题出在哪呢?我写的错误代码里,age_cut后面有个逗号!

还学会了END AS的正确用法。END AS不要放在CASE后面的循环语句里