• If you want to check whether any subset of the debits exactly equals any subset of the credits (not to mention being off by a penny), I think you may be beyond what I'd be willing to write in T-SQL.  I started sketching out an approach, but I realized if you have 10 open credits and 10 open debits there would be over 1,000,000 subset comparisons.  And that's just for one account.

    I think I would attack this with a VB program on a good workstation, or you might bring your server to its knees.  There also might be a published algorithm to reduce the number of subset comparisons, but it would almost certainly require a language other than T-SQL.

    The preliminary SQL code, FWIW:

    declare @accounts table (

     id int identity not null primary key clustered,

     accnt_code char(6) not null,

     conv_code char(3) not null)

    declare @i int, @accnt char(6), @conv char(3)

    insert into @accounts

    select distinct accnt_code, conv_code

    from activity where allocation is null or allocation <> 1

    set @i = SCOPE_IDENTITY()

    while @i > 0 begin

     select @accnt = accnt_code, @conv = conv_code, @i = @i - 1

     from @accounts where id = @i

     insert into debits??

     select pk??, other_amt from activity

     where @accnt = accnt_code and @conv = conv_code and other_amount > 0

     insert into credits??

     select pk??, other_amt from activity

     where @accnt = accnt_code and @conv = conv_code and other_amount < 0

     ? generate all possible subsets of credits and debits

     ? if totals match, set corresponding Allocation fields = 1

     ? repeat until either subset is empty or all subsets have been examined

    end