我有桌子
id salary
1 100
2 200
3 300
4 400
现在,我想找到平均薪水,然后将其与每个人的薪水进行比较,然后说“高”或“低”。
这是否可以通过案例陈述来完成,或者请建议我一些好方法?
我的查询是
select xxxxxxxxxx =
case
when(salary>(select avg(salary)from table_name))
then 'High'
when(salary<(select avg(salary)from table_name))
then 'low'
end;
但是这里我的问题是我的表中没有那个xxxxxxxxx列。
无论如何,我可以在没有添加/指定该表中的列来存储值的情况下获取值-“高”或“低”?
因此,请为我建议使用case语句的解决方案。
使用join
:
select tn.*,
(case when tn.salary > a.avgs then 'High'
when tn.salary < a.avgs then 'low'
end)
from table_name tn cross join
(select avg(salary) as avgs from tn) a;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句