「牛客网-SQL篇」SQL26 计算25岁以上和以下的用户数量
题目
我的解答:
做不出来。
知识点补充:CASE函数
CASE
函数是一种多分支函数
Go through conditions and return a value when the first condition is met:
SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30 THEN "The quantity is greater than 30"
WHEN Quantity = 30 THEN "The quantity is 30"
ELSE "The quantity is under 30"
END
FROM OrderDetails;
例子:
运行SQL:
Definition and Usage
The CASE statement goes through conditions and return a value when the first condition is met (like an IF-THEN-ELSE statement). So, once a condition is true, it will stop reading and return the result.
If no conditions are true, it will return the value in the ELSE clause.
If there is no ELSE part and no conditions are true, it returns NULL.
Syntax
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
我的订正
select
(case
when age<25 then '25岁以下'
when age>=25 then '25岁及以上'
else '25岁以下'
end) as age_cut,
count(*) as number
from
user_profile
group by
age_cut;
结果:pass