「牛客网-SQL篇」SQL26 计算25岁以上和以下的用户数量

「牛客网-SQL篇」SQL26 计算25岁以上和以下的用户数量

题目

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:

image-20220727185536687

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值