SQL中CASE WHEN THEN的用法
答案:5 悬赏:20 手机版
解决时间 2021-03-20 20:21
- 提问者网友:流星是天使的眼泪
- 2021-03-20 09:39
SQL中CASE WHEN THEN的用法
最佳答案
- 五星知识达人网友:像个废品
- 2021-03-20 10:48
select a,count(*) from
(select a=case when age<30 then '青年' --查询age <30的为青年,“青年”直接赋给a
when age>=30 and age<50 then '中年'
when age>=50 then '老年' end
from test --括号里查出每条记录中对应年龄段属于哪个值 )
a_test --将查出的值 放到 a_test中去
group by a --按a_test 中的字段 a 分组统计数据
(select a=case when age<30 then '青年' --查询age <30的为青年,“青年”直接赋给a
when age>=30 and age<50 then '中年'
when age>=50 then '老年' end
from test --括号里查出每条记录中对应年龄段属于哪个值 )
a_test --将查出的值 放到 a_test中去
group by a --按a_test 中的字段 a 分组统计数据
全部回答
- 1楼网友:往事埋风中
- 2021-03-20 15:40
SELECt (case when age <30 THEN '青年' when age BETWEEN 30 AND 49 then '中年' ELSE '老年' END) AS age_scope, COUNT(`name`) as num FROM test_case_when GROUP BY age_scope
- 2楼网友:天凉才是好个秋
- 2021-03-20 14:49
select case when age<30 then N'青年' when age between 30 and 49 then N'中年' else N'老年' end as [年龄段],count(name) as [数目] from table
group by case when age<30 then N'青年' when age between 30 and 49 then N'中年' else N'老年' end
group by case when age<30 then N'青年' when age between 30 and 49 then N'中年' else N'老年' end
- 3楼网友:英雄的欲望
- 2021-03-20 13:15
给你一个参考。我相信你看了后就知道应该如何处理了。有表student(id,name,score)根据分数列(score)每10分为一段,查询每段分数的人数。
SELECt a, COUNT(*)
FROM (SELECt a = CASE WHEN score >= 0 AND
score < 10 THEN '0-9' WHEN score >= 10 AND
score < 20 THEN '10-19' WHEN score >= 20 AND
score < 30 THEN '20-29' WHEN score >= 30 AND
score < 40 THEN '30-39' WHEN score >= 40 AND
score < 50 THEN '40-49' WHEN score >= 50 AND
score < 60 THEN '50-59' WHEN score >= 60 AND
score < 70 THEN '60-69' WHEN score >= 70 AND
score < 80 THEN '70-79' WHEN score >= 80 AND
score < 90 THEN '80-89' WHEN score >= 90 AND
score < 100 THEN '90-99' ELSE '100' END
FROM student) a
GROUP BY a
SELECt a, COUNT(*)
FROM (SELECt a = CASE WHEN score >= 0 AND
score < 10 THEN '0-9' WHEN score >= 10 AND
score < 20 THEN '10-19' WHEN score >= 20 AND
score < 30 THEN '20-29' WHEN score >= 30 AND
score < 40 THEN '30-39' WHEN score >= 40 AND
score < 50 THEN '40-49' WHEN score >= 50 AND
score < 60 THEN '50-59' WHEN score >= 60 AND
score < 70 THEN '60-69' WHEN score >= 70 AND
score < 80 THEN '70-79' WHEN score >= 80 AND
score < 90 THEN '80-89' WHEN score >= 90 AND
score < 100 THEN '90-99' ELSE '100' END
FROM student) a
GROUP BY a
- 4楼网友:舍身薄凉客
- 2021-03-20 12:19
select 年龄段,count(num) as nums from
(select '年龄段'=(case when age<30 then '青年' when age>=30 and age<50 then '中年' else '老年' end),count(*) as num from tage group by age) as a
group by 年龄段
你把你的sql语句这样改试试
(select '年龄段'=(case when age<30 then '青年' when age>=30 and age<50 then '中年' else '老年' end),count(*) as num from tage group by age) as a
group by 年龄段
你把你的sql语句这样改试试
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯