Вопрос интересный, так как могут быть случаи, когда не будет частичной оплаты, следующий запрос должен делать то, что вы хотите:
CREATE TABLE #temp (ID INT,JO VARCHAR(10),CID VARCHAR(10),L VARCHAR(10), Invoice VARCHAR(10), Amount NUMERIC(10,2), Payment NUMERIC(10,2))
INSERT INTO #temp VALUES
(279810,'B1','CL0001','a','FA0001',55638,170000),
(279812,'B1','CL0001','a','FA0002',40720,170000),
(279814,'B1','CL0001','a','FA0003',73642,170000),
(279868,'B1','CL0001','a','FA0004',51500,170000),
(279868,'B1','CL0001','a','FA0004',200,170000),
(279868,'B1','CL0001','a','FA0004',1234678,170000),
(279868,'B1','CL0001','a','FA0004',90,170000),
(279870,'B1','CL0002','a','FA0005',10000,60000),
(279871,'B1','CL0002','a','FA0006',15000,60000),
(279872,'B1','CL0002','a','FA0007',30000,60000),
(279873,'B1','CL0002','a','FA0008',25000,60000)
SELECT ID,JO,CID,L,Invoice,Amount,Payment
,CASE WHEN t.[Balance] >=0 THEN 0
WHEN t.[Balance] <0 AND LAG(t.[Balance]) OVER( ORDER BY (SELECT 1)) > 0 THEN ABS(t.[Balance])
ELSE [Amount] END AS [Balance Amount]
,CASE WHEN t.[Balance] >=0 THEN 'Paid'
WHEN t.[Balance] <0 AND LAG(t.[Balance]) OVER( ORDER BY (SELECT 1)) > 0 THEN 'Partial Payment'
ELSE 'Not Paid' END AS [Status]
FROM (
SELECT *
,SUM(Amount) OVER (PARTITION BY CID ORDER BY (SELECT 1) ROWS UNBOUNDED PRECEDING ) Cumulative
FROM #temp ) A
CROSS APPLY (VALUES(Payment - Cumulative)) AS T(Balance)
Результат как ниже,
ID JO CID L Invoice Amount Payment Balance Amount Status
279810 B1 CL0001 a FA0001 55638.00 170000.00 0.00 Paid
279812 B1 CL0001 a FA0002 40720.00 170000.00 0.00 Paid
279814 B1 CL0001 a FA0003 73642.00 170000.00 0.00 Paid
279868 B1 CL0001 a FA0004 51500.00 170000.00 51500.00 Not Paid
279868 B1 CL0001 a FA0004 200.00 170000.00 200.00 Not Paid
279868 B1 CL0001 a FA0004 1234678.00 170000.00 1234678.00 Not Paid
279868 B1 CL0001 a FA0004 90.00 170000.00 90.00 Not Paid
279870 B1 CL0002 a FA0005 10000.00 60000.00 0.00 Paid
279871 B1 CL0002 a FA0006 15000.00 60000.00 0.00 Paid
279872 B1 CL0002 a FA0007 30000.00 60000.00 0.00 Paid
279873 B1 CL0002 a FA0008 25000.00 60000.00 20000.00 Partial Payment