Если в вашей таблице есть поле identity_:
Пример:
DECLARE @A table ( identity_field int identity(1,1),prod_id NVARCHAR(10) , v int)
INSERT into @A(prod_id,v)
VALUES('A',10),('A',20),('B',30),('A',40),('B',50),('B',60),('B',70),('A',80),('C',90);
WITH A AS (
select row_number()over (order by prod_id,identity_field) nr,*
from @A)
,B AS(
SELECT identity_field-nr group_field,*
FROM A
)
select MIN(identity_field) tran_id,prod_id, SUM(v) sum_v
FROM B
GROUP BY prod_id,group_field
order by MIN(identity_field)
для вашего стола:
WITH A AS (
select identity_field-row_number()over (order by ACCOUNT_NUMBER,DESCRIPTION,identity_field) group_field,*
from YOUT_TABLE)
select MIN(identity_field) tran_id,ACCOUNT_NUMBER,DESCRIPTION, SUM(DEBIT) transact_debit,SUM(CREDIT) transact_credit
FROM A
GROUP BY ACCOUNT_NUMBER,DESCRIPTION,group_field
order by MIN(identity_field)