converting C/AL code to sql

I would like to understand a piece of code that calculates the highest balance for a customer.

I am not familiar with C-SIDE coding. Ideally I would like to convert the part in the code that calculates the highest balance to sql syntax.

I understand the code uses two records, one is custledgentry2, and the other one is dtlCustLedgEntry2 but i do not understand how the two records are link and used to get the balance.

Any help with this would be awesome.

this is the code that calculates the highest balance

FOR i := 1 TO 3 DO BEGIN CustLedgEntry2.RESET; CustLedgEntry2.SETCURRENTKEY(“Customer No.”,“Posting Date”); CustLedgEntry2.SETRANGE(“Customer No.”,“No.”); CustLedgEntry2.SETFILTER(“Posting Date”,CustDateFilter[i]); CustLedgEntry2.SETRANGE(“Posting Date”,0D,CustLedgEntry2.GETRANGEMAX(“Posting Date”)); DtldCustLedgEntry2.SETCURRENTKEY(“Customer No.”,“Posting Date”); CustLedgEntry2.COPYFILTER(“Customer No.”,DtldCustLedgEntry2.“Customer No.”); CustLedgEntry2.COPYFILTER(“Posting Date”,DtldCustLedgEntry2.“Posting Date”); DtldCustLedgEntry2.CALCSUMS(“Amount (LCY)”); CustBalanceLCY := DtldCustLedgEntry2.“Amount (LCY)”; HighestBalanceLCY[i] := CustBalanceLCY; DaysToPay := 0; NoOfInv := 0; CustLedgEntry2.SETFILTER(“Posting Date”,CustDateFilter[i]); IF CustLedgEntry2.FIND(’+’) THEN REPEAT j := CustLedgEntry2.“Document Type”; IF j > 0 THEN NoOfDoc[i][j] := NoOfDoc[i][j] + 1; CustLedgEntry2.CALCFIELDS(“Amount (LCY)”); CustBalanceLCY := CustBalanceLCY - CustLedgEntry2.“Amount (LCY)”; IF CustBalanceLCY > HighestBalanceLCY[i] THEN HighestBalanceLCY[i] := CustBalanceLCY; END;

Use SQL Profiler to record what SQL is doing when you run this code. Then you’ll know.