Hi
I have below code and how to get Opening Balance Amount. If user enters date from 01/04/2024 to 27/12/2024. How i can get Opening Balance i.e Balance before 01/04/2024.
SELECT T3."ShortName" AS "Vendor Code",(Select Max("CardName") from OCRD where "CardCode" = T3."ShortName") AS "Vendor Name",
T3."Debit", T3."Credit"
FROM "TEST"."OJDT" T2 INNER JOIN JDT1 T3 ON T2."TransId" = T3."TransId" WHERE
(T2."TransId" not IN (SELECT T0."StornoToTr" FROM OJDT T0 where T0."StornoToTr" is not NULL)) AND (T2."StornoToTr" is NULL)
AND (T3."ShortName"=:Vendor )
and T2."TaxDate">= TO_DATE(:FromDate) and T2."TaxDate"<= TO_DATE(:ToDate)
UNION ALL
SELECT T2."CardCode" AS "Vendor Code", T2."CardName" AS "Vendor Name", (T2."DocTotal") + (T2."DpmAmnt") + (T2."DpmVat"),0
FROM "TEST"."OINV" T2
WHERE T2."DocDate" >= TO_DATE(:FromDate) and T2."DocDate" <= TO_DATE(:ToDate)
AND (T2."CardCode"=:Vendor ) AND T2."CANCELED" not in ('Y','C')
Thanks
Chintan GandhiPosted Apr 15, 2025, 6:52 AM
Opening Balance = Total Credit - Total Debit BEFORE :FromDate
You already have the main transactions between
:FromDateand:ToDate, but the Opening Balance needs to be calculated separately using a subquery or a Common Table Expression (CTE).Here's how you can get the Opening Balance
You can wrap your existing query with a
UNION ALLblock for the opening balance. Below is how you could do it:-- Opening Balance before FromDate
SELECT
T3."ShortName" AS "Vendor Code",
(SELECT MAX("CardName") FROM OCRD WHERE "CardCode" = T3."ShortName") AS "Vendor Name",
NULL AS "Debit",
SUM(T3."Credit") - SUM(T3."Debit") AS "Credit"
FROM "TEST"."OJDT" T2
INNER JOIN JDT1 T3 ON T2."TransId" = T3."TransId"
WHERE
(T2."TransId" NOT IN (SELECT T0."StornoToTr" FROM OJDT T0 WHERE T0."StornoToTr" IS NOT NULL))
AND (T2."StornoToTr" IS NULL)
AND T3."ShortName" = :Vendor
AND T2."TaxDate" < TO_DATE(:FromDate)
GROUP BY T3."ShortName"
UNION ALL
-- Journal Transactions in date range
SELECT
T3."ShortName" AS "Vendor Code",
(SELECT MAX("CardName") FROM OCRD WHERE "CardCode" = T3."ShortName") AS "Vendor Name",
T3."Debit",
T3."Credit"
FROM "TEST"."OJDT" T2
INNER JOIN JDT1 T3 ON T2."TransId" = T3."TransId"
WHERE
(T2."TransId" NOT IN (SELECT T0."StornoToTr" FROM OJDT T0 WHERE T0."StornoToTr" IS NOT NULL))
AND (T2."StornoToTr" IS NULL)
AND T3."ShortName" = :Vendor
AND T2."TaxDate" >= TO_DATE(:FromDate)
AND T2."TaxDate" <= TO_DATE(:ToDate)
UNION ALL
-- Invoice transactions
SELECT
T2."CardCode" AS "Vendor Code",
T2."CardName" AS "Vendor Name",
(T2."DocTotal" + T2."DpmAmnt" + T2."DpmVat") AS "Debit",
0 AS "Credit"
FROM "TEST"."OINV" T2
WHERE
T2."DocDate" >= TO_DATE(:FromDate)
AND T2."DocDate" <= TO_DATE(:ToDate)
AND T2."CardCode" = :Vendor
AND T2."CANCELED" NOT IN ('Y','C')
Notes
Opening Balance is shown as a row where only the Credit column is populated (Net balance = Credit - Debit before FromDate).
You can label the row as
"Opening Balance"by replacing"Vendor Code"with a string like'Opening Balance', but that may affect sorting or formatting.If needed, you can use an alias or extra column to indicate that it's the opening balance row, like:
SELECT 'Opening Balance' AS "Description", ...
Jignesh KumarPosted Dec 27, 2024, 11:37 AM
Hello Ramco,
Use this query which will help you out, Create CTE(Common table expression) and then filter records based on from date, Here we need to consider only date before from date.