Actual Balance vs Budget Balance SQL Query

select
actual.period_name,
cc.segment1 company_code,
vs1.description company_desc,
cc.segment2 location_code,
vs2.description location_desc,
cc.segment3 cost_center_code,
vs3.description cost_center_desc,
cc.segment4 account_code,
vs4.description account_desc,
cc.segment5 resp_code,
vs5.description resp_desc,
cc.segment6 future_code,
vs6.description future_desc,
actual.ptd ptd_actual,
actual.ytd ytd_actual,
budget.ptd ptd_budget,
budget.ytd ytd_budget

from
gl_code_combinations cc,
(select period_name,code_combination_id,sum(period_net_dr-period_net_cr) ptd,sum((begin_balance_dr-begin_balance_cr+period_net_dr-period_net_cr)) ytd
from gl_balances where actual_flag='A' and currency_code='JOD' group by period_name,code_combination_id) actual,
(select period_name,code_combination_id,sum(period_net_dr-period_net_cr) ptd,sum((begin_balance_dr-begin_balance_cr+period_net_dr-period_net_cr)) ytd
from gl_balances where actual_flag='B' and currency_code='JOD' group by period_name,code_combination_id) budget,
fnd_flex_values_vl vs1,
fnd_flex_values_vl vs2,
fnd_flex_values_vl vs3,
fnd_flex_values_vl vs4,
fnd_flex_values_vl vs5,
fnd_flex_values_vl vs6

where
cc.code_combination_id=actual.code_combination_id
and cc.segment1=vs1.flex_value
and cc.segment2=vs2.flex_value
and cc.segment3=vs3.flex_value
and cc.segment4=vs4.flex_value
and cc.segment5=vs5.flex_value
and cc.segment6=vs6.flex_value
and actual.period_name=budget.period_name (+)
and actual.code_combination_id=budget.code_combination_id (+)
and vs1.flex_value_set_id=1007829
and vs2.flex_value_set_id=1007833
and vs3.flex_value_set_id=1007831
and vs4.flex_value_set_id=1007830
and vs5.flex_value_set_id=1007834
and vs6.flex_value_set_id=1007832
and actual.period_name=:p_period_name

No comments:

Post a Comment