2017-12-17 23 views
0

この例では、異なる従業員の給与とボーナスがwhileループで混合されているため、計算が無効になります。whileループで独立して列IDを反復する方法

各IDを個別に反復して、そのIDに基づいて特定の計算を行う方法はありますか?

while (select count(*) from db.employee) > 0 
begin 
    select top 1 ID, SALARY, [month] 
    from db.employee 
    order by [month] asc 

    if deserve = yes 
    begin 
     set SALARY = SALARY + (bonus * 2) 
    end 
end 

これは私のスクリプトではありません。私は複雑なスクリプトを持っている、それは目的を理解する時間がかかります。しかし、私はこの単純な考え方に似ています。だから後で私は提案に基づいて私の元のスクリプトを微調整することができます。元のものはIterate through tables and do calculation based on one column SQL server

答えて

0

です。これにはいくつかの方法があります。

まず、通常のアップデートを行うだけではない理由がわかりましたか?

update emp 
set salary = emp.salary + (bonus * 2) 
from employee emp 
join bonuses bon on bon.employee_id = emp.employee_id 
where bon.deserved = 1 

もう一つの方法は、カーソルを使用することですが、人々は、そのパフォーマンスの問題を実際にそれら好きではない:https://docs.microsoft.com/en-us/sql/t-sql/language-elements/declare-cursor-transact-sql

もう一つの方法は、しばらくそれから、従業員の数を保持するコンテナを作成することです現在の反復はカウントと等しくなく、各ループで必要な従業員を取得します。

declare @employeecount int = (select count(*) from employee) 
declare @current int = 1 

while @current <= @employeecount 
begin 

    declare @employeeid uniqueidentifier = (select top 1 employee_id from (select top (@current) * from employee order by employee_id asc) tmp order by employee_id desc) 

    -- Grab Employee and run logic 

set @current = @current + 1; 
end 

ボトムはCURSORSの機能とほとんど同じですが、カーソルはそれより優れています。

私が最近取ったことは次のとおりです。私はそれに従うだけで簡単だとわかります。

declare @ids table (id uniqueidentifier) 
insert into @ids 
select employee_id 
from employee 

while (select count(*) from @ids) > 0 
begin 

    declare @employeeid uniqueidentifier = (select top 1 employee_id from @ids) 

    -- Grab Employee and run logic 

delete from @ids where id = @employeeid 
end 
+0

私は最後のものが好きですが、いくつかの問題があるようです。イテレートは各IDにはなく、いくつかのボーナスはIDの間に混在することがあります。 – jou

+0

私は似たようなことをしています。(上から1つのemployee_idを選択してください(上から(*現在)*をemployee_idから順に選択してください)。私はそれをやり直す別の方法を作りました。大規模なコードで読むのが好きかもしれません。 – Monofuse

+0

あなたのロジックを試してみます。ありがとうございます。 – jou

関連する問題