the following SQL calculates Beginning Balance.
I am not sure what is the logic behind Beginning Balance
calculation in the SQL. Can anyone explain the logic behind it?
I want to calculate beginning balance in Crystal report using the two tables mentioned below. Thanks.
SELECT "gl_detail"."gl_year"
, "gl_detail"."gl_period"
, "gl_detail"."fd_id"
, "gl_detail"."gl_id"
, Substr("gl_detail"."gl_id",1,4) AS GL_ACCOUNT_DESC
, "gl_detail"."gl_tran_date"
, "gl_detail"."gl_short_ref"
, "gl_detail"."gl_source"
, "gl_detail"."sy_batch_id"
, (IF "gl_detail"."gl_amount" > 0 THEN "gl_detail"."gl_amount" ELSE 0 ENDIF) AS GL_AMOUNT_DEBIT
, (IF "gl_detail"."gl_amount" < 0 THEN "gl_detail"."gl_amount" ELSE 0 ENDIF)* -1 AS GL_AMOUNT_CREDIT
, "gl_detail"."gl_amount"
, "gl_detail"."gt_id"
, "gl_detail"."sa_id"
, "gl_detail"."dn_id"
, "gl_detail"."gl_je_id"
, "gl_detail"."ck_id"
, "gl_detail"."ap_inv_id"
, "ap_inv_hdr"."ap_name1" AS AP_NAME
, "ck_master"."ck_payee1" AS CK_NAME
, "fd_master"."fd_abbrev_name"
, "gt_master"."gt_org_name1" AS GT_NAME
, Month("gl_detail"."gl_tran_date") AS MONTH
, Coalesce(AP_NAME,GT_NAME,CK_NAME) AS SORT_NAME
,fd_master.fd_entity
, "gl_master"."gl_description"
, "gl_department"."gl_department_desc"
, "gl_cost_center"."gl_cost_center_desc"
, "gl_detail"."gl_id" AS GENERAL_LEDGER
FROM {oj "gl_detail" LEFT OUTER JOIN "ap_inv_hdr" ON "gl_detail"."ct_id" = "ap_inv_hdr"."ct_id" AND
"gl_detail"."ap_inv_id" = "ap_inv_hdr"."ap_inv_id" AND
"gl_detail"."ap_ictype" = "ap_inv_hdr"."ap_ictype" AND
"gl_detail"."fd_company" = "ap_inv_hdr"."fd_company"}
, {oj "gl_detail" LEFT OUTER JOIN "gt_master" ON "gl_detail"."gt_id" = "gt_master"."gt_id"}
, {oj "gl_detail" LEFT OUTER JOIN "ck_master" ON "gl_detail"."ck_id" = "ck_master"."ck_id" AND "gl_detail"."ck_bank_account" = "ck_master"."ck_bank_account" }
, "fd_master"
, "gl_master"
, "gl_department"
, "gl_cost_center"
WHERE ("gl_detail"."fd_id" = "fd_master"."fd_id" )
AND (Substr("gl_detail"."gl_id",1,4) = "gl_master"."gl_id")
AND ("gl_detail"."gl_year" = _iYear )
AND ("gl_detail"."gl_period" >= _iPeriodStart )
AND ("gl_detail"."gl_period" <= _iPeriodEnd )
AND ("gl_detail"."fd_id" = '1000')
AND ((Substr("gl_master"."gl_id",1,4) = _szGlID) OR p_szGlID IS NULL))
AND (("gl_detail"."sy_batch_id" = _szBatchID) OR p_szBatchID IS NULL))
AND (("fd_master"."fd_entity" = _szEntity) OR p_szEntity IS NULL))
AND ((Substr("gl_detail"."gl_id",7,2) = _szSubDept) OR p_szSubDept IS NULL))
AND ("gl_master"."gl_id" = Substr("gl_detail"."gl_id",1,4))
AND ("gl_department"."gl_department" = Substr("gl_detail"."gl_id",5,2))
AND ("gl_cost_center"."gl_cost_center" = Substr("gl_detail"."gl_id",7,2))
UNION ALL
SELECT "gl_balance"."gl_year"
, 0
, "gl_balance"."fd_id"
, "gl_balance"."gl_id"
, Substr("gl_balance"."gl_id",1,4)
, NULL
, ' Beginning Balance:'
, ''
, ''
, (IF BEG_BALANCE > 0 THEN BEG_BALANCE ELSE 0 ENDIF) AS GL_AMOUNT_DEBIT
, (IF BEG_BALANCE < 0 THEN BEG_BALANCE ELSE 0 ENDIF)* -1 AS GL_AMOUNT_CREDIT
, (COALESCE(SUM("gl_balance"."gl_balance_amt"),0) ) -
(SELECT COALESCE(SUM("bal2"."gl_balance_amt"),0)
FROM gl_balance bal2
WHERE "bal2"."gl_year" = "gl_balance"."gl_year"
AND "bal2"."gl_period" >= _iPeriodStart
AND "bal2"."gl_period" <= _iPeriodEnd
AND "bal2"."fd_id" = "gl_balance"."fd_id"
AND "bal2"."gl_id" = "gl_balance"."gl_id") AS BEG_BALANCE
, ''
, ''
, ''
, ''
, ''
, ''
, ''
, ''
, "fd_master"."fd_abbrev_name"
, ''
, ''
, ''
,fd_master.fd_entity
, "gl_master"."gl_description"
, "gl_department"."gl_department_desc"
, "gl_cost_center"."gl_cost_center_desc"
, "gl_balance"."gl_id" AS GENERAL_LEDGER
FROM "gl_master"
, {oj "gl_balance" LEFT OUTER JOIN "fd_master" ON "gl_balance"."fd_id" = "fd_master"."fd_id"}
, "gl_department"
, "gl_cost_center"
WHERE (Substr("gl_balance"."gl_id",1,4) = "gl_master"."gl_id")
AND "gl_balance"."gl_year" = _iYear
AND "gl_balance"."gl_period" <= _iPeriodEnd
AND ("gl_balance"."fd_id" = '1000')
AND ((Substr("gl_master"."gl_id",1,4) = _szGlID) OR p_szGlID IS NULL))
AND (("fd_master"."fd_entity" = _szEntity) OR p_szEntity IS NULL))
AND ((Substr("gl_balance"."gl_id",7,2) = _szSubDept) OR p_szSubDept IS NULL))
AND ("gl_department"."gl_department" = Substr("gl_balance"."gl_id",5,2))
AND ("gl_cost_center"."gl_cost_center" = Substr("gl_balance"."gl_id",7,2))
GROUP BY
"gl_balance"."gl_year"
, "gl_balance"."fd_id"
, fd_master.fd_entity
, "gl_balance"."gl_id"
,"fd_master"."fd_abbrev_name"
, "gl_master"."gl_description"
, "gl_department"."gl_department_desc"
, "gl_cost_center"."gl_cost_center_desc"
ORDER BY
3 ASC
, 25 ASC
, 4 ASC
, 6 ASC
, 8 ASC
, 24 ASC
I am not sure what is the logic behind Beginning Balance
calculation in the SQL. Can anyone explain the logic behind it?
I want to calculate beginning balance in Crystal report using the two tables mentioned below. Thanks.
SELECT "gl_detail"."gl_year"
, "gl_detail"."gl_period"
, "gl_detail"."fd_id"
, "gl_detail"."gl_id"
, Substr("gl_detail"."gl_id",1,4) AS GL_ACCOUNT_DESC
, "gl_detail"."gl_tran_date"
, "gl_detail"."gl_short_ref"
, "gl_detail"."gl_source"
, "gl_detail"."sy_batch_id"
, (IF "gl_detail"."gl_amount" > 0 THEN "gl_detail"."gl_amount" ELSE 0 ENDIF) AS GL_AMOUNT_DEBIT
, (IF "gl_detail"."gl_amount" < 0 THEN "gl_detail"."gl_amount" ELSE 0 ENDIF)* -1 AS GL_AMOUNT_CREDIT
, "gl_detail"."gl_amount"
, "gl_detail"."gt_id"
, "gl_detail"."sa_id"
, "gl_detail"."dn_id"
, "gl_detail"."gl_je_id"
, "gl_detail"."ck_id"
, "gl_detail"."ap_inv_id"
, "ap_inv_hdr"."ap_name1" AS AP_NAME
, "ck_master"."ck_payee1" AS CK_NAME
, "fd_master"."fd_abbrev_name"
, "gt_master"."gt_org_name1" AS GT_NAME
, Month("gl_detail"."gl_tran_date") AS MONTH
, Coalesce(AP_NAME,GT_NAME,CK_NAME) AS SORT_NAME
,fd_master.fd_entity
, "gl_master"."gl_description"
, "gl_department"."gl_department_desc"
, "gl_cost_center"."gl_cost_center_desc"
, "gl_detail"."gl_id" AS GENERAL_LEDGER
FROM {oj "gl_detail" LEFT OUTER JOIN "ap_inv_hdr" ON "gl_detail"."ct_id" = "ap_inv_hdr"."ct_id" AND
"gl_detail"."ap_inv_id" = "ap_inv_hdr"."ap_inv_id" AND
"gl_detail"."ap_ictype" = "ap_inv_hdr"."ap_ictype" AND
"gl_detail"."fd_company" = "ap_inv_hdr"."fd_company"}
, {oj "gl_detail" LEFT OUTER JOIN "gt_master" ON "gl_detail"."gt_id" = "gt_master"."gt_id"}
, {oj "gl_detail" LEFT OUTER JOIN "ck_master" ON "gl_detail"."ck_id" = "ck_master"."ck_id" AND "gl_detail"."ck_bank_account" = "ck_master"."ck_bank_account" }
, "fd_master"
, "gl_master"
, "gl_department"
, "gl_cost_center"
WHERE ("gl_detail"."fd_id" = "fd_master"."fd_id" )
AND (Substr("gl_detail"."gl_id",1,4) = "gl_master"."gl_id")
AND ("gl_detail"."gl_year" = _iYear )
AND ("gl_detail"."gl_period" >= _iPeriodStart )
AND ("gl_detail"."gl_period" <= _iPeriodEnd )
AND ("gl_detail"."fd_id" = '1000')
AND ((Substr("gl_master"."gl_id",1,4) = _szGlID) OR p_szGlID IS NULL))
AND (("gl_detail"."sy_batch_id" = _szBatchID) OR p_szBatchID IS NULL))
AND (("fd_master"."fd_entity" = _szEntity) OR p_szEntity IS NULL))
AND ((Substr("gl_detail"."gl_id",7,2) = _szSubDept) OR p_szSubDept IS NULL))
AND ("gl_master"."gl_id" = Substr("gl_detail"."gl_id",1,4))
AND ("gl_department"."gl_department" = Substr("gl_detail"."gl_id",5,2))
AND ("gl_cost_center"."gl_cost_center" = Substr("gl_detail"."gl_id",7,2))
UNION ALL
SELECT "gl_balance"."gl_year"
, 0
, "gl_balance"."fd_id"
, "gl_balance"."gl_id"
, Substr("gl_balance"."gl_id",1,4)
, NULL
, ' Beginning Balance:'
, ''
, ''
, (IF BEG_BALANCE > 0 THEN BEG_BALANCE ELSE 0 ENDIF) AS GL_AMOUNT_DEBIT
, (IF BEG_BALANCE < 0 THEN BEG_BALANCE ELSE 0 ENDIF)* -1 AS GL_AMOUNT_CREDIT
, (COALESCE(SUM("gl_balance"."gl_balance_amt"),0) ) -
(SELECT COALESCE(SUM("bal2"."gl_balance_amt"),0)
FROM gl_balance bal2
WHERE "bal2"."gl_year" = "gl_balance"."gl_year"
AND "bal2"."gl_period" >= _iPeriodStart
AND "bal2"."gl_period" <= _iPeriodEnd
AND "bal2"."fd_id" = "gl_balance"."fd_id"
AND "bal2"."gl_id" = "gl_balance"."gl_id") AS BEG_BALANCE
, ''
, ''
, ''
, ''
, ''
, ''
, ''
, ''
, "fd_master"."fd_abbrev_name"
, ''
, ''
, ''
,fd_master.fd_entity
, "gl_master"."gl_description"
, "gl_department"."gl_department_desc"
, "gl_cost_center"."gl_cost_center_desc"
, "gl_balance"."gl_id" AS GENERAL_LEDGER
FROM "gl_master"
, {oj "gl_balance" LEFT OUTER JOIN "fd_master" ON "gl_balance"."fd_id" = "fd_master"."fd_id"}
, "gl_department"
, "gl_cost_center"
WHERE (Substr("gl_balance"."gl_id",1,4) = "gl_master"."gl_id")
AND "gl_balance"."gl_year" = _iYear
AND "gl_balance"."gl_period" <= _iPeriodEnd
AND ("gl_balance"."fd_id" = '1000')
AND ((Substr("gl_master"."gl_id",1,4) = _szGlID) OR p_szGlID IS NULL))
AND (("fd_master"."fd_entity" = _szEntity) OR p_szEntity IS NULL))
AND ((Substr("gl_balance"."gl_id",7,2) = _szSubDept) OR p_szSubDept IS NULL))
AND ("gl_department"."gl_department" = Substr("gl_balance"."gl_id",5,2))
AND ("gl_cost_center"."gl_cost_center" = Substr("gl_balance"."gl_id",7,2))
GROUP BY
"gl_balance"."gl_year"
, "gl_balance"."fd_id"
, fd_master.fd_entity
, "gl_balance"."gl_id"
,"fd_master"."fd_abbrev_name"
, "gl_master"."gl_description"
, "gl_department"."gl_department_desc"
, "gl_cost_center"."gl_cost_center_desc"
ORDER BY
3 ASC
, 25 ASC
, 4 ASC
, 6 ASC
, 8 ASC
, 24 ASC