2010-05-05 57 views
2

問題是將所有員工的工資由5個或多於5個項目工作的50%更新爲30%(> = 3個項目) ,減少20%(> = 1個項目),通過在EMPLOYEE_PROJECT_HISTORY上查詢來執行一個組,可以獲得項目的數量;有條件地使用oracle中的group by子查詢中的值更新表

我嘗試了這些查詢

update emp set emp.sal= 
case 
when jemp.pcount >=5 then emp.sal+ (emp.sal*50)/100 
when jemp.pcount >=3 then emp.sal+ (emp.sal*30)/100 
when jemp.pcount >=1 then emp.sal+ (emp.sal*20)/100 
else emp.sal+ (emp.sal*20)/100 
end 
from employee emp join (select empno as jempno,count(projectno) as pcount from EMPLOYEE_PROJECT_HISTORY by empno) jemp on emp.empno=jemp.jempno ; 

update employee a set a.sal= case (select count(b.projectno) as pcount from EMPLOYEE_PROJECT_HISTORY b group by b.empno) 
    when b.pcount >5 then 
    a.sal = a.sal+ (a.sal*50)/100 
    when pcount >3 then 
    a.sal = a.sal+ (a.sal*30)/100 
    when pcount >1 then 
    a.sal = a.sal+ (a.sal*20)/100 
    end; 

回答

2

您將無法更新是否包含GROUP BY子句中的Oracle的加入。您可以使用內嵌子查詢,像這樣:

UPDATE employee e 
    SET sal = sal * (SELECT CASE 
           WHEN COUNT(*) >= 5 THEN 
           1.5 
           WHEN COUNT(*) >= 3 THEN 
           1.3 
           WHEN COUNT(*) >= 1 THEN 
           1.2 
           ELSE 
           1 
          END 
         FROM employee_project_history eph 
        WHERE eph.empno = e.empno); 
+0

神......你這麼好的..how我可以學習像這樣的事情......我一直在敲打我的頭兩天 – Ramkumar 2010-05-05 09:04:18

+0

@Ramkumar:有網上有很多很好的資源可供學習。 Tom Kytes的網站[asktom](http://asktom.oracle.com)是一個很好的開始。你可以在這個SO問題中找到更多的東西:[我在哪裏可以學到更多關於Oracle數據庫的知識?](http://stackoverflow.com/questions/286260/where-can-i-learn-more-about-oracle-databases)。如果你喜歡看書,我會建議[有效的Oracle設計](http://www.amazon.com/exec/obidos/redirect?tag=asktom03-20&path=tg%2Fdetail%2F-%2F0072230657)。 – 2010-05-05 09:41:55