编写一个sql来为员工数据集生成报告,如果平均年龄> 35则给出条件,然后状态值为ok,否则为notok数据集
id name age dept salary
1 tt 51 it 4000
2 kk 56 it 6000
3 mm 45 sales 7000
4 kk 25 sales 9000
5 op 24 hr 4000
6 op 24 hr 8000
输出
dept avgage states
it 53.5 ok
sales 35 ok
hr 24 notok
答案 0 :(得分:0)
使用此查询。
SELECT a.dept,
a.avgage,
CASE
WHEN a.avgage >= 35 THEN 'ok'
ELSE 'notok'
END states
FROM (SELECT dept,
Avg (age) avgage
FROM employee
GROUP BY dept) a
ORDER BY avgage DESC;
注意:请自行了解并撰写查询。