http://generalledger-gl.blogspot.com/2016/07/trial-balance-complete-ptd-ytd-account.html
Trial Balance Complete PTD & YTD (Account Wise, Month Wise)
--Ledger_ID & Fiscal_Year is Mandatory
--Fiscal_Year in which Month Falls
Currency Type = Total
select gcc.SEGMENT4,gb.PERIOD_NAME,gb.PERIOD_YEAR,gb.PERIOD_NUM,sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR) Opening_Balance_PTD,
sum(gb.PERIOD_NET_DR) PTD_DEBIT, sum(gb.PERIOD_NET_CR) PTD_CREDIT,
NVL(hh.OPENING_BALANCE_YTD,0) OPENING_BALANCE_YTD,
/*sum(gb.PROJECT_TO_DATE_DR+gb.PERIOD_NET_DR),
sum(gb.PROJECT_TO_DATE_CR+gb.PERIOD_NET_CR),
kk.Opening_DR,kk.Opening_CR,*/
sum(gb.PROJECT_TO_DATE_DR+gb.PERIOD_NET_DR)-NVL(kk.Opening_DR,0) YTD_DEBIT,
sum(gb.PROJECT_TO_DATE_CR+gb.PERIOD_NET_CR)-NVL(kk.Opening_CR,0) YTD_CREDIT,
sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR) ENDING_BALANCE
from gl_balances gb, gl_code_combinations gcc,
(select gcc.segment4 Acc,sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR) OPENING_BALANCE_YTD
from gl_balances gb,
gl_code_combinations gcc,
FND_FLEX_VALUES_VL FND
where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
AND FND.FLEX_VALUE = GCC.SEGMENT4
and ledger_id = '&Ledger_id'
and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
and gb.CURRENCY_CODE = 'PKR'
and gb.ACTUAL_FLAG = 'A'
and gb.PERIOD_NUM = 1
and gb.PERIOD_YEAR = '&Fiscal_Year'
group by gcc.segment4) hh,
(select gb.PERIOD_NAME,gcc.SEGMENT4,
sum(gb.PROJECT_TO_DATE_DR) Opening_DR ,sum(gb.PROJECT_TO_DATE_CR) Opening_CR
from gl_balances gb, gl_code_combinations gcc, gl_periods gp
where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
and gb.PERIOD_YEAR=gp.PERIOD_YEAR
and gb.PERIOD_NUM=gp.PERIOD_NUM
and gb.ledger_id = '&Ledger_id'
and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
and gb.CURRENCY_CODE = 'PKR'
and gb.ACTUAL_FLAG = 'A'
and gp.PERIOD_NUM = (select min(Period_num) from gl_periods where period_year = '&Fiscal_Year') --and gp.ENTERED_PERIOD_NAME= 'JUL-OP' --(First Period)
and gp.PERIOD_YEAR ='&Fiscal_Year'
group by gb.PERIOD_NAME,gcc.SEGMENT4 ) kk
where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
and kk.segment4(+)=gcc.SEGMENT4
and hh.Acc(+)=gcc.segment4
--and gb.PERIOD_NAME=yy.period_name
and gb.ledger_id = '&Ledger_id'
and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
and gb.CURRENCY_CODE = 'PKR'
and gb.ACTUAL_FLAG = 'A'
and gb.PERIOD_YEAR = '&Fiscal_Year'
and gb.PERIOD_NAME = NVL('&Period_Name',gb.PERIOD_NAME)
group by gcc.SEGMENT4,gb.PERIOD_NAME, gb.PERIOD_year,gb.PERIOD_NUM, hh.OPENING_BALANCE_YTD,kk.Opening_DR, kk.Opening_CR
order by gb.PERIOD_YEAR, gb.PERIOD_NUM asc
--------------------------------------------------------------------------------------------------------------------------
Currency Type = Entered
select gcc.SEGMENT4,
gb.PERIOD_NAME,
gb.PERIOD_YEAR,
gb.PERIOD_NUM,
Decode(gb.CURRENCY_CODE,
'PKR',
sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq),
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)) Opening_Balance_PTD,
Decode(gb.CURRENCY_CODE,
'PKR',
sum(gb.PERIOD_NET_DR_beq),
sum(gb.PERIOD_NET_DR)) PTD_DEBIT,
Decode(gb.CURRENCY_CODE,
'PKR',
sum(gb.PERIOD_NET_CR_beq),
sum(gb.PERIOD_NET_CR)) PTD_CREDIT,
NVL(hh.OPENING_BALANCE_YTD, 0) OPENING_BALANCE_YTD,
/*sum(gb.PROJECT_TO_DATE_DR_beq+gb.PERIOD_NET_DR_beq),
sum(gb.PROJECT_TO_DATE_CR_beq+gb.PERIOD_NET_CR_beq),
kk.Opening_DR,kk.Opening_CR,*/
Decode(gb.CURRENCY_CODE,
'PKR',
sum(gb.PROJECT_TO_DATE_DR_beq + gb.PERIOD_NET_DR_beq) -
NVL(kk.Opening_DR, 0),
sum(gb.PROJECT_TO_DATE_DR + gb.PERIOD_NET_DR) -
NVL(kk.Opening_DR, 0)) YTD_DEBIT,
Decode(gb.CURRENCY_CODE,
'PKR',
sum(gb.PROJECT_TO_DATE_CR_beq + gb.PERIOD_NET_CR_beq) -
NVL(kk.Opening_CR, 0),
sum(gb.PROJECT_TO_DATE_CR + gb.PERIOD_NET_CR) -
NVL(kk.Opening_CR, 0)) YTD_CREDIT,
Decode(gb.CURRENCY_CODE,
'PKR',
sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq),
sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)
) ENDING_BALANCE
from gl_balances gb,
gl_code_combinations gcc,
(select gcc.segment4 Acc,
Decode(gb.CURRENCY_CODE,
'PKR',
sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq),
sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)) OPENING_BALANCE_YTD
from gl_balances gb,
gl_code_combinations gcc,
FND_FLEX_VALUES_VL FND
where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
AND FND.FLEX_VALUE = GCC.SEGMENT4
and ledger_id = '&Ledger_id'
and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
and gb.CURRENCY_CODE = NVL('&Currency', gb.CURRENCY_CODE)
and gb.ACTUAL_FLAG = 'A'
and gb.PERIOD_NUM = 1
and gb.PERIOD_YEAR = '&Fiscal_Year'
group by gcc.segment4, gb.CURRENCY_CODE) hh,
(select gb.PERIOD_NAME,
gcc.SEGMENT4,
Decode(gb.CURRENCY_CODE,
'PKR',
sum(gb.PROJECT_TO_DATE_DR_beq),
sum(gb.PROJECT_TO_DATE_DR)) Opening_DR,
Decode(gb.CURRENCY_CODE,
'PKR',
sum(gb.PROJECT_TO_DATE_CR_beq),
sum(gb.PROJECT_TO_DATE_CR)) Opening_CR
from gl_balances gb, gl_code_combinations gcc, gl_periods gp
where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
and gb.PERIOD_YEAR = gp.PERIOD_YEAR
and gb.PERIOD_NUM = gp.PERIOD_NUM
and gb.ledger_id = '&Ledger_id'
and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
and gb.CURRENCY_CODE = NVL('&Currency', gb.CURRENCY_CODE)
and gb.ACTUAL_FLAG = 'A'
and gp.PERIOD_NUM = (select min(Period_num) from gl_periods where period_year = '&Fiscal_Year') --and gp.ENTERED_PERIOD_NAME= 'JUL-OP' --(First Period)
and gp.PERIOD_YEAR = '&Fiscal_Year'
group by gb.PERIOD_NAME, gcc.SEGMENT4, gb.CURRENCY_CODE) kk
where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
and kk.segment4(+) = gcc.SEGMENT4
and hh.Acc(+) = gcc.segment4
--and gb.PERIOD_NAME=yy.period_name
and gb.ledger_id = '&Ledger_id'
and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
and gb.CURRENCY_CODE = NVL('&Currency', gb.CURRENCY_CODE)
and gb.ACTUAL_FLAG = 'A'
and gb.PERIOD_YEAR = '&Fiscal_Year'
and gb.PERIOD_NAME = NVL('&Period_Name', gb.PERIOD_NAME)
group by gcc.SEGMENT4,
gb.PERIOD_NAME,
gb.PERIOD_year,
gb.PERIOD_NUM,
gb.CURRENCY_CODE,
hh.OPENING_BALANCE_YTD,
kk.Opening_DR,
kk.Opening_CR
order by gb.PERIOD_YEAR, gb.PERIOD_NUM asc
--------------------------------------------------------------------------------------------------------------------------
P_Currency_Type = Entered/Total (Parameter)
select gcc.SEGMENT4,
gb.PERIOD_NAME,
gb.PERIOD_YEAR,
gb.PERIOD_NUM,
Decode('&Currency',
'&Ledger_Currency',
Decode('&P_Currency_type',
'Entered',
sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq),
'Total',
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)),
Decode('&P_Currency_type',
'Entered',
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
'Total',
sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq))) Opening_Balance_PTD,
Decode('&Currency',
'&Ledger_Currency',
Decode('&P_Currency_type',
'Entered',
sum(gb.PERIOD_NET_DR_beq),
'Total',
sum(gb.PERIOD_NET_DR),
sum(gb.PERIOD_NET_DR)),
Decode('&P_Currency_type',
'Entered',
sum(gb.PERIOD_NET_DR),
'Total',
sum(gb.PERIOD_NET_DR_beq))) PTD_DEBIT,
Decode('&Currency',
'&Ledger_Currency',
Decode('&P_Currency_type',
'Entered',
sum(gb.PERIOD_NET_CR_beq),
'Total',
sum(gb.PERIOD_NET_CR),
sum(gb.PERIOD_NET_CR)),
Decode('&P_Currency_type',
'Entered',
sum(gb.PERIOD_NET_CR),
'Total',
sum(gb.PERIOD_NET_CR_beq))) PTD_CREDIT,
NVL(hh.OPENING_BALANCE_YTD, 0) OPENING_BALANCE_YTD,
/*sum(gb.PROJECT_TO_DATE_DR_beq+gb.PERIOD_NET_DR_beq),
sum(gb.PROJECT_TO_DATE_CR_beq+gb.PERIOD_NET_CR_beq),
kk.Opening_DR,kk.Opening_CR,*/
Decode('&Currency',
'&Ledger_Currency',
Decode('&P_Currency_type',
'Entered',
sum(gb.PROJECT_TO_DATE_DR_beq + gb.PERIOD_NET_DR_beq) -
NVL(kk.Opening_DR, 0),
'Total',
sum(gb.PROJECT_TO_DATE_DR + gb.PERIOD_NET_DR) -
NVL(kk.Opening_DR, 0),
sum(gb.PROJECT_TO_DATE_DR + gb.PERIOD_NET_DR) -
NVL(kk.Opening_DR, 0)),
Decode('&P_Currency_type',
'Entered',
sum(gb.PROJECT_TO_DATE_DR + gb.PERIOD_NET_DR) -
NVL(kk.Opening_DR, 0),
'Total',
sum(gb.PROJECT_TO_DATE_DR_beq + gb.PERIOD_NET_DR_beq) -
NVL(kk.Opening_DR, 0))) YTD_DEBIT,
Decode('&Currency',
'&Ledger_Currency',
Decode('&P_Currency_type',
'Entered',
sum(gb.PROJECT_TO_DATE_CR_beq + gb.PERIOD_NET_CR_beq) -
NVL(kk.Opening_CR, 0),
'Total',
sum(gb.PROJECT_TO_DATE_CR + gb.PERIOD_NET_CR) -
NVL(kk.Opening_CR, 0),
sum(gb.PROJECT_TO_DATE_CR + gb.PERIOD_NET_CR) -
NVL(kk.Opening_CR, 0)),
Decode('&P_Currency_type',
'Entered',
sum(gb.PROJECT_TO_DATE_CR + gb.PERIOD_NET_CR) -
NVL(kk.Opening_CR, 0),
'Total',
sum(gb.PROJECT_TO_DATE_CR_beq + gb.PERIOD_NET_CR_beq) -
NVL(kk.Opening_CR, 0))) YTD_CREDIT,
Decode('&Currency',
'&Ledger_Currency',
Decode('&P_Currency_type',
'Entered',
sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq),
'Total',
sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)),
Decode('&P_Currency_type',
'Entered',
sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
'Total',
sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq))) ENDING_BALANCE
from gl_balances gb,
gl_code_combinations gcc,
(select gcc.segment4 Acc,
Decode('&Currency',
'&Ledger_Currency',
Decode('&P_Currency_type',
'Entered',
sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
sum(gb.BEGIN_BALANCE_DR_beq -
gb.BEGIN_BALANCE_CR_beq),
'Total',
sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)),
Decode('&P_Currency_type',
'Entered',
sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
'Total',
sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
sum(gb.BEGIN_BALANCE_DR_beq -
gb.BEGIN_BALANCE_CR_beq))) OPENING_BALANCE_YTD
from gl_balances gb,
gl_code_combinations gcc,
FND_FLEX_VALUES_VL FND
where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
AND FND.FLEX_VALUE = GCC.SEGMENT4
and ledger_id = '&Ledger_id'
and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
and gb.CURRENCY_CODE = NVL('&Currency', gb.CURRENCY_CODE)
and gb.ACTUAL_FLAG = 'A'
and gb.PERIOD_NUM = 1
and gb.PERIOD_YEAR = '&Fiscal_Year'
group by gcc.segment4, gb.CURRENCY_CODE) hh,
(select gb.PERIOD_NAME,
gcc.SEGMENT4,
Decode('&Currency',
'&Ledger_Currency',
Decode('&P_Currency_type',
'Entered',
sum(gb.PROJECT_TO_DATE_DR_beq),
'Total',
sum(gb.PROJECT_TO_DATE_DR),
sum(gb.PROJECT_TO_DATE_DR)),
Decode('&P_Currency_type',
'Entered',
sum(gb.PROJECT_TO_DATE_DR),
'Total',
sum(gb.PROJECT_TO_DATE_DR_beq))) Opening_DR,
Decode('&Currency',
'&Ledger_Currency',
Decode('&P_Currency_type',
'Entered',
sum(gb.PROJECT_TO_DATE_CR_beq),
'Total',
sum(gb.PROJECT_TO_DATE_CR),
sum(gb.PROJECT_TO_DATE_CR)),
Decode('&P_Currency_type',
'Entered',
sum(gb.PROJECT_TO_DATE_CR),
'Total',
sum(gb.PROJECT_TO_DATE_CR_beq))) Opening_CR
from gl_balances gb, gl_code_combinations gcc, gl_periods gp
where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
and gb.PERIOD_YEAR = gp.PERIOD_YEAR
and gb.PERIOD_NUM = gp.PERIOD_NUM
and gb.ledger_id = '&Ledger_id'
and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
and gb.CURRENCY_CODE = NVL('&Currency', gb.CURRENCY_CODE)
and gb.ACTUAL_FLAG = 'A'
and gp.PERIOD_NUM = (select min(Period_num) from gl_periods where period_year = '&Fiscal_Year') --and gp.ENTERED_PERIOD_NAME= 'JUL-OP' --(First Period)
and gp.PERIOD_YEAR = '&Fiscal_Year'
group by gb.PERIOD_NAME, gcc.SEGMENT4, gb.CURRENCY_CODE) kk
where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
and kk.segment4(+) = gcc.SEGMENT4
and hh.Acc(+) = gcc.segment4
--and gb.PERIOD_NAME=yy.period_name
and gb.ledger_id = '&Ledger_id'
and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
and gb.CURRENCY_CODE = NVL('&Currency', gb.CURRENCY_CODE)
and gb.ACTUAL_FLAG = 'A'
and gb.PERIOD_YEAR = '&Fiscal_Year'
and gb.PERIOD_NAME = NVL('&Period_Name', gb.PERIOD_NAME)
group by gcc.SEGMENT4,
gb.PERIOD_NAME,
gb.PERIOD_year,
gb.PERIOD_NUM,
gb.CURRENCY_CODE,
hh.OPENING_BALANCE_YTD,
kk.Opening_DR,
kk.Opening_CR
order by gb.PERIOD_YEAR, gb.PERIOD_NUM asc
--------------------------------------------------------------------------------------------------------------------------
Currency Type = E/T/C (Parameter)
select gcc.SEGMENT4,
gb.PERIOD_NAME,
gb.PERIOD_YEAR,
gb.PERIOD_NUM,
Decode(ga.CURRENCY_CODE,'&Currency',
Decode('&Currency_type',
'E',
sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq),
'T',
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)),
Decode('&Currency_type',
'E',
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
'C',
sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq))) Opening_Balance_PTD,
Decode(ga.CURRENCY_CODE,'&Currency',
Decode('&Currency_type',
'E',
sum(gb.PERIOD_NET_DR_beq),
'T',
sum(gb.PERIOD_NET_DR),
sum(gb.PERIOD_NET_DR)),
Decode('&Currency_type',
'E',
sum(gb.PERIOD_NET_DR),
'C',
sum(gb.PERIOD_NET_DR_beq))) PTD_DEBIT,
Decode(ga.CURRENCY_CODE,'&Currency',
Decode('&Currency_type',
'E',
sum(gb.PERIOD_NET_CR_beq),
'T',
sum(gb.PERIOD_NET_CR),
sum(gb.PERIOD_NET_CR)),
Decode('&Currency_type',
'E',
sum(gb.PERIOD_NET_CR),
'C',
sum(gb.PERIOD_NET_CR_beq))) PTD_CREDIT,
NVL(hh.OPENING_BALANCE_YTD, 0) OPENING_BALANCE_YTD,
/*sum(gb.PROJECT_TO_DATE_DR_beq+gb.PERIOD_NET_DR_beq),
sum(gb.PROJECT_TO_DATE_CR_beq+gb.PERIOD_NET_CR_beq),
kk.Opening_DR,kk.Opening_CR,*/
Decode(ga.CURRENCY_CODE,'&Currency',
Decode('&Currency_type',
'E',
sum(gb.PROJECT_TO_DATE_DR_beq + gb.PERIOD_NET_DR_beq) -
NVL(kk.Opening_DR, 0),
'T',
sum(gb.PROJECT_TO_DATE_DR + gb.PERIOD_NET_DR) -
NVL(kk.Opening_DR, 0),
sum(gb.PROJECT_TO_DATE_DR + gb.PERIOD_NET_DR) -
NVL(kk.Opening_DR, 0)),
Decode('&Currency_type',
'E',
sum(gb.PROJECT_TO_DATE_DR + gb.PERIOD_NET_DR) -
NVL(kk.Opening_DR, 0),
'C',
sum(gb.PROJECT_TO_DATE_DR_beq + gb.PERIOD_NET_DR_beq) -
NVL(kk.Opening_DR, 0))) YTD_DEBIT,
Decode(ga.CURRENCY_CODE,'&Currency',
Decode('&Currency_type',
'E',
sum(gb.PROJECT_TO_DATE_CR_beq + gb.PERIOD_NET_CR_beq) -
NVL(kk.Opening_CR, 0),
'T',
sum(gb.PROJECT_TO_DATE_CR + gb.PERIOD_NET_CR) -
NVL(kk.Opening_CR, 0),
sum(gb.PROJECT_TO_DATE_CR + gb.PERIOD_NET_CR) -
NVL(kk.Opening_CR, 0)),
Decode('&Currency_type',
'E',
sum(gb.PROJECT_TO_DATE_CR + gb.PERIOD_NET_CR) -
NVL(kk.Opening_CR, 0),
'C',
sum(gb.PROJECT_TO_DATE_CR_beq + gb.PERIOD_NET_CR_beq) -
NVL(kk.Opening_CR, 0))) YTD_CREDIT,
Decode(ga.CURRENCY_CODE,'&Currency',
Decode('&Currency_type',
'E',
sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq),
'T',
sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)),
Decode('&Currency_type',
'E',
sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
'C',
sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
sum(gb.BEGIN_BALANCE_DR_beq - gb.BEGIN_BALANCE_CR_beq))) ENDING_BALANCE
from gl_balances gb,
gl_code_combinations gcc,
gl_ledgers ga,
(select gcc.segment4 Acc,
Decode(ga.CURRENCY_CODE,'&Currency',
Decode('&Currency_type',
'E',
sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
sum(gb.BEGIN_BALANCE_DR_beq -
gb.BEGIN_BALANCE_CR_beq),
'T',
sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR)),
Decode('&Currency_type',
'E',
sum(gb.PERIOD_NET_DR - gb.PERIOD_NET_CR) +
sum(gb.BEGIN_BALANCE_DR - gb.BEGIN_BALANCE_CR),
'C',
sum(gb.PERIOD_NET_DR_beq - gb.PERIOD_NET_CR_beq) +
sum(gb.BEGIN_BALANCE_DR_beq -
gb.BEGIN_BALANCE_CR_beq))) OPENING_BALANCE_YTD
from gl_balances gb,
gl_code_combinations gcc,
FND_FLEX_VALUES_VL FND,
gl_ledgers ga
where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
AND FND.FLEX_VALUE = GCC.SEGMENT4
and ga.LEDGER_ID=gb.LEDGER_ID
and gcc.CHART_OF_ACCOUNTS_ID=ga.CHART_OF_ACCOUNTS_ID
and gb.ledger_id = '&Ledger_id'
and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
and gb.CURRENCY_CODE = '&Currency'
and gb.ACTUAL_FLAG = 'A'
and gb.PERIOD_NUM = 1
and gb.PERIOD_YEAR = '&Fiscal_Year'
group by gcc.segment4, ga.CURRENCY_CODE) hh,
(select gb.PERIOD_NAME,
gcc.SEGMENT4,
Decode(ga.CURRENCY_CODE,'&Currency',
Decode('&Currency_type',
'E',
sum(gb.PROJECT_TO_DATE_DR_beq),
'T',
sum(gb.PROJECT_TO_DATE_DR),
sum(gb.PROJECT_TO_DATE_DR)),
Decode('&Currency_type',
'E',
sum(gb.PROJECT_TO_DATE_DR),
'C',
sum(gb.PROJECT_TO_DATE_DR_beq))) Opening_DR,
Decode(ga.CURRENCY_CODE,'&Currency',
Decode('&Currency_type',
'E',
sum(gb.PROJECT_TO_DATE_CR_beq),
'T',
sum(gb.PROJECT_TO_DATE_CR),
sum(gb.PROJECT_TO_DATE_CR)),
Decode('&Currency_type',
'E',
sum(gb.PROJECT_TO_DATE_CR),
'C',
sum(gb.PROJECT_TO_DATE_CR_beq))) Opening_CR
from gl_balances gb, gl_code_combinations gcc, gl_periods gp, gl_ledgers ga
where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
and gb.PERIOD_YEAR = gp.PERIOD_YEAR
and gb.PERIOD_NUM = gp.PERIOD_NUM
and ga.LEDGER_ID=gb.LEDGER_ID
and gcc.CHART_OF_ACCOUNTS_ID=ga.CHART_OF_ACCOUNTS_ID
and gb.ledger_id = '&Ledger_id'
and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
and gb.CURRENCY_CODE = '&Currency'
and gb.ACTUAL_FLAG = 'A'
and gp.PERIOD_NUM = (select min(Period_num) from gl_periods where period_year = '&Fiscal_Year') --and gp.ENTERED_PERIOD_NAME= 'JUL-OP' --(First Period)
and gp.PERIOD_YEAR = '&Fiscal_Year'
group by gb.PERIOD_NAME, gcc.SEGMENT4, ga.CURRENCY_CODE) kk
where gb.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
and kk.segment4(+) = gcc.SEGMENT4
and hh.Acc(+) = gcc.segment4
and ga.LEDGER_ID=gb.LEDGER_ID
and gcc.CHART_OF_ACCOUNTS_ID=ga.CHART_OF_ACCOUNTS_ID
--and gb.PERIOD_NAME=yy.period_name
and gb.ledger_id = '&Ledger_id'
and gcc.SEGMENT2 = NVL('&Location', gcc.SEGMENT2)
and gcc.SEGMENT4 = NVL('&Account', gcc.SEGMENT4)
and gb.CURRENCY_CODE = '&Currency'
and gb.ACTUAL_FLAG = 'A'
and gb.PERIOD_YEAR = '&Fiscal_Year'
and gb.PERIOD_NAME = NVL('&Period_Name', gb.PERIOD_NAME)
group by gcc.SEGMENT4,
gb.PERIOD_NAME,
gb.PERIOD_year,
gb.PERIOD_NUM,
ga.CURRENCY_CODE,
hh.OPENING_BALANCE_YTD,
kk.Opening_DR,
kk.Opening_CR
order by gb.PERIOD_YEAR, gb.PERIOD_NUM asc