簡體   English   中英

在選擇中使用Case語句

[英]Using Case statement in select

我有桌子

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語句的解決方案。

有兩種方法可以做到這一點。 這是一個選擇:

select id, 
    case when salary > avgsalary then 'High' else 'Low' end 
from yourtable y
   join (select avg(salary) avgsalary from yourtable) y2

使用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;

嘗試這個:

SELECT A.employee_id, 
       A.salary, 
      (CASE WHEN b.ave > A.salary THEN 'LOW' 
            WHEN b.ave=A.salary THEN 'AVERAGE' 
            ELSE 'HIGH' END) remarks 
FROM oehr_employees A,
     (select avg(salary) ave from oehr_employees) b;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM