Hello everyone,
Dav,
Thanks for your example and psudo-code. I've converted your code (see below) to run in SQL Query Analyzer but I'm running into a remainder problem. What do you think is the best way to handle the remainder? In the example below, the amount to prorate is 111 but the sum of the amount to adjust equals 108.
Any help would be appreciated.
-- Start of Code --
Drop Table #ProrateList
Create Table #ProrateList (
idno int ,
amt int ,
adjustment int
)
Insert Into #ProrateList Values (1, 37841, 0)
Insert Into #ProrateList Values (2, 8995, 0)
Insert Into #ProrateList Values (3, 4583, 0)
Insert Into #ProrateList Values (4, 2086, 0)
Insert Into #ProrateList Values (5, 1584, 0)
Insert Into #ProrateList Values (6, 1102, 0)
Insert Into #ProrateList Values (7, 1065, 0)
Insert Into #ProrateList Values (8, 815, 0)
Insert Into #ProrateList Values (9, 809, 0)
Insert Into #ProrateList Values (10, 734, 0)
Insert Into #ProrateList Values (11, 317, 0)
Insert Into #ProrateList Values (12, 231, 0)
Insert Into #ProrateList Values (13, 109, 0)
Insert Into #ProrateList Values (14, 99, 0)
Insert Into #ProrateList Values (15, 38, 0)
Insert Into #ProrateList Values (16, 26, 0)
Insert Into #ProrateList Values (17, 25, 0)
Insert Into #ProrateList Values (18, 25, 0)
Insert Into #ProrateList Values (19, 21, 0)
Insert Into #ProrateList Values (20, 16, 0)
Insert Into #ProrateList Values (21, 15, 0)
Insert Into #ProrateList Values (22, 8, 0)
Declare @ProrateAmount Int
Declare @R Float
Declare @OldR Float
Declare @Results Float
Declare @IntTotal Float
Declare @idno Int
Declare @amt Float
Declare @SumAmt Float
Select @ProrateAmount = 111
Select @R = 0
Select @SumAmt = Sum(amt)
From #ProrateList
-- Define the cursor
Declare ProrationList Cursor For
Select idno, amt
From #ProrateList
Order By amt Desc
-- Open the cursor
Open ProrationList
-- Assign the values of the first record
Fetch Next From ProrationList
Into @idno, @amt
-- Loop through the records
While @@FETCH_STATUS = 0
Begin
Select @OldR = @R
Select @R = @R + (@amt / @SumAmt) * @ProrateAmount
Select @Results = @R - @OldR
If Round(@Results, 0) <> 0
Begin
Update #ProrateList Set adjustment = Round(@Results, 0)
Where idno = @idno
End
-- Assign the values of the next record
Fetch Next From ProrationList
Into @idno, @amt
End
-- Close the cursor
Close ProrationList
Deallocate ProrationList
Select * From #ProrateList
Select Sum(adjustment) From #ProrateList
-- End of Code --