My current data set looks like following
[Table 1]
Key | Group | Amount | Sequence |
---|---|---|---|
1 | 1 | 50 | 1 |
2 | 1 | 60 | 2 |
3 | 1 | 70 | 3 |
4 | 1 | 90 | 4 |
5 | 2 | 120 | 1 |
I have a second table that records a "Rolling rank" of each of these which looks like this:
[Table 2]
Key | KeySK | Rank | TotalAmount | Amount |
---|---|---|---|---|
1 | 1 | 1 | 50 | 50 |
2 | 2 | 1 | 60 | 60 |
3 | 2 | 2 | 110 | 50 |
4 | 3 | 1 | 70 | 70 |
5 | 3 | 2 | 130 | 60 |
6 | 3 | 3 | 180 | 50 |
I perform this by looking for all records in Table 1 where they do not have a subsequent record in Table 2
SELECT
Key
,Group
FROM
[Table 1] [T1]
LEFT JOIN [Table 2] [T2] ON [T1].Key = [T2].KeySK
WHERE
[T2].Key IS NULL
Then, using SSIS ForEach Loop, I run the following stored procedure:
SELECT
@InstalmentSequence = [Sequence]
FROM
[Table 1] [T1]
WHERE
[T1].Key = @Key
INSERT INTO [Table 2]
SELECT
[T1].[Key] AS 'KeySK'
,RANK()
OVER (ORDER BY [T1].[Sequence] DESC) AS 'Rank'
,SUM(ISNULL([T1].Amount,0.00))
OVER (ORDER BY [T1].[Sequence] DESC) AS 'TotalAmount'
,ISNULL([T1].Amount, 0.00) AS 'Amount'
FROM
[Table 1] [T1]
WHERE
[T1].AccountSK = @Group
AND [T1].[Sequence] <= @Sequence
All of this works and produces the correct outcome, but the performance is very poor. I am wondering if these is a better solution to this that I am not thinking of.