2017-10-13 6 views
1

この問題では、$ 100,000が使い尽くされるまで最低給与(昇順)からemployees.salaryを20%増やす必要があります。私は$ 100,000が使用されるまで、更新された金額を保存する方法の解決策を見つけるのが難しいです。これは私がこれまで持っていたものです。Oracle SQLカーソル最大金額に達するまで給与を引き上げる

declare 
cursor mancur is 
    select salary from employees order by salary asc; 
tempcur  mancur%ROWTYPE; 
profits  number := 100000; 
tempsalary employees.salary%type; 
tempinc  number(8,2); 
begin 
open mancur; 
loop 
    fetch mancur into tempcur; 
    tempinc := tempcur.salary * 1.20; 
    tempsalary := profits - tempcur.salary; 
    dbms_output.put_line(tempcur.salary || ' increased by 20% ' || tempinc || ', Bonus amount left ' || tempsalary); 
    exit when mancur%notfound; --when 100,000 has been used 
    --update employees set salary = salary * 1.20 where employee_id = tempcur.employee_id; 
end loop; 
close mancur; 
end; 
/
+0

これは生産上の問題か宿題の問題ですか?宿題の場合は、PL/SQLクラス用ですか?この要件は、単一のSQL文ではるかに効率的に解決できます。 – mathguy

+2

また、ネクタイをどうやって扱いますか?あなたがリストに上がったとしましょう。そして今、(給与がまだ増加していない)次の従業員は、同じ給与を持つ3人の従業員です。彼らの現在の給料はそれぞれ30,000ドルであるため、それぞれ6,000ドルを受け取るべきですが、残ったのはわずか12,000ドルです。誰がどれくらい得るの? – mathguy

答えて

1
begin 
open mancur; 
    loop 
    fetch mancur into tempcur; 
    tempinc := tempcur.salary * 1.20; 
    profits := profits - (tempinc-tempcur.salary); -- You have to keep subtracting the increment amount to find if bonus is exhausted or not 
     if profits <=0 Then --When all your funds are exhausted 
     Exit 
     End if 
       dbms_output.put_line(tempcur.salary || ' increased by 20% ' || tempinc || ', Bonus amount left ' || profits); 
     exit when mancur%notfound; --when 100,000 has been used 
    --update employees set salary = salary * 1.20 where employee_id = 
     tempcur.employee_id; 
    end loop; 
close mancur; 
end; 
/
+0

ありがとう! Idk私はそれを得ることができなかった。画面から休憩時間ha – namesjj

0
declare 
profits  number := 100000; 
tempinc  number(8,2); 
begin 
for hike_loop in (select employee_id,salary from employees order by salary asc) loop 
if profits <= 0 then 
break; 
end if; 
tempinc := hike_loop.salary * 0.20; 
if (tempinc <= profits) then 
update employees set salary = salary * 1.20 where employee_id = hike_loop.employee_id; 
profits := profits - tempinc; 
else 
break; 
end if; 
end loop; 
end; 
/
0

ありがとう楽しみのためだけに:ここで、これは普通のSQLで実行する方法です。 PL/SQLクラスの宿題でない限り、このタスクの関数やプロシージャは必要ありません。そしてその場合でも、同じMERGE文をPL/SQLコードから実行する必要があります。そのため、処理は行単位ではなく、設定レベルで実行されます。

これは、これまでに投稿されたソリューションとは異なり、「残量が少ない」ことも解決します。 「最後の」給与を20%引き上げるのに十分でない場合、多くの人が$ 100,000を上限として残されます。そして、同じ給与を持つ2人以上の従業員が「最初に除外される」場合、「残りの金額」はこれらの従業員の間で等しく分割されます。

merge into employees e 
    using (select employee_id, 
       sum (salary) over (order by salary)  as sum_salary, 
       count(salary) over (partition by salary) as cnt 
      from employees 
     ) x 
    on (e.employee_id = x.employee_id) 
when matched then update 
    set salary = 1.2 * salary + case when sum_salary <= 100000/0.2 then 0 
            else (100000 - 0.2 * sum_salary)/cnt end 
    where sum_salary - cnt * salary <= 100000/0.2 
; 
関連する問題