Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help figuring out the SQL to calculate Beginning Balance

Status
Not open for further replies.

namas

Technical User
Aug 31, 2006
31
US
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" = :p_iYear )
AND ("gl_detail"."gl_period" >= :p_iPeriodStart )
AND ("gl_detail"."gl_period" <= :p_iPeriodEnd )
AND ("gl_detail"."fd_id" = '1000')
AND ((Substr("gl_master"."gl_id",1,4) = :p_szGlID) OR :)p_szGlID IS NULL))
AND (("gl_detail"."sy_batch_id" = :p_szBatchID) OR :)p_szBatchID IS NULL))
AND (("fd_master"."fd_entity" = :p_szEntity) OR :)p_szEntity IS NULL))
AND ((Substr("gl_detail"."gl_id",7,2) = :p_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" >= :p_iPeriodStart
AND "bal2"."gl_period" <= :p_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" = :p_iYear
AND "gl_balance"."gl_period" <= :p_iPeriodEnd
AND ("gl_balance"."fd_id" = '1000')
AND ((Substr("gl_master"."gl_id",1,4) = :p_szGlID) OR :)p_szGlID IS NULL))
AND (("fd_master"."fd_entity" = :p_szEntity) OR :)p_szEntity IS NULL))
AND ((Substr("gl_balance"."gl_id",7,2) = :p_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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top