|
|
发表于 2007/3/6 10:19:02
|
显示全部楼层
发一个sql自己先慢慢体会
select DENSE_RANK() OVER (PARTITION BY period_name ORDER BY sequence) col,
period_name,sequence,
description 项目,本期余额,本期累计余额,上期同期累计余额
from(
select DENSE_RANK() OVER (PARTITION BY period_name ORDER BY sequence) col,b.period_name,b.sequence,b.description,
sum(decode(b.operator,'+',1,'-',-1,1)*decode(b.sign,'+',1,'-',-1,1)*nvl(c.本期余额,0)) 本期余额,
sum(decode(b.operator,'+',1,'-',-1,1)*decode(b.sign,'+',1,'-',-1,1)*nvl(c.本期累计余额,0)) 本期累计余额,
sum(decode(b.operator,'+',1,'-',-1,1)*decode(b.sign,'+',1,'-',-1,1)*nvl(c.上期同期累计余额,0)) 上期同期累计余额
from
(select peri.period_name,a1.sequence,a1.description,decode(a1.sign,null,'+',a1.sign) sign,a1.operator,a1.segment3_low,a1.segment3_high
from
org_acct_periods peri,
(select '' PERIOD_NAME,rga.sequence,decode(rga.name,null,rga.description,rga.name) description,rgr.sign||a.sign sign,
a.operator operator,rgr.segment3_low||a.segment3_low segment3_low,rgr.segment3_high||a.segment3_high segment3_high
from RG_REPORT_AXES_V rga,
RG_REPORT_AXIS_CONTENTS rgr,
(SELECT RGC.AXIS_SEQ sequence,RGC.operator,RGC.AXIS_SET_ID,A.sign,A.segment3_low,A.segment3_high
FROM (select rgc.axis_set_id,
rgc.axis_seq, decode(sign(rgc1.axis_seq_low), 1,rgc1.operator,rgc.operator) operator,
decode(sign(rgc1.axis_seq_low),1,rgc1.axis_seq_low,rgc.axis_seq_low) axis_seq_low,
decode(sign(rgc1.axis_seq_low),1,rgc1.axis_seq_high,rgc.axis_seq_high) axis_seq_high
from RG_REPORT_CALCULATIONS rgc,
(select rgc.axis_seq,rgc.operator, rgc.axis_seq_low, rgc.axis_seq_high
from RG_REPORT_CALCULATIONS rgc
where axis_set_id = 1001 and rgc.APPLICATION_ID = 101) rgc1
where rgc.axis_set_id = 1001 and rgc.APPLICATION_ID = 101 and
rgc1.axis_seq(+) = rgc.axis_seq_high) RGC,
(select rga.axis_set_id,rga.sequence,decode(rga.name,null,rga.description,rga.name) description,
rgr.sign,rgr.segment3_low,rgr.segment3_high
from RG_REPORT_AXES_V rga,
RG_REPORT_AXIS_CONTENTS rgr
where rga.axis_set_id = 1001
and rga.display_flag = 'Y'
and rga.axis_set_id=rgr.axis_set_id(+)
and rga.sequence=rgr.axis_seq(+)) A
WHERE
(RGC.AXIS_SET_ID = 1001) AND
A.sequence BETWEEN RGC.AXIS_SEQ_LOW AND RGC.AXIS_SEQ_HIGH AND
A.AXIS_SET_ID=RGC.AXIS_SET_ID) a
where rga.axis_set_id = 1001
and rga.display_flag = 'Y'
and rga.axis_set_id=rgr.axis_set_id(+)
and rga.sequence=rgr.axis_seq(+)
and A.sequence(+)=rga.sequence
order by sequence) a1
where peri.period_name=nvl(a1.PERIOD_NAME,peri.period_name) and peri.organization_id=104)b,
(select
gb.period_name 期间
,gb.period_year 年
,gcck.segment3 科目编码
,sum(nvl(gb.period_net_dr,0)) 本期借方发生
,sum(nvl(gb.period_net_cr,0)) 本期贷方发生
,sum(nvl(gb.period_net_dr, 0) - nvl(gb.period_net_cr, 0)) 本期余额
,sum(nvl(gb.begin_balance_dr, 0) + nvl(gb.period_net_dr, 0) -
nvl(gb.begin_balance_cr, 0) - nvl(gb.period_net_cr, 0)) 本期累计余额
,(select sum(nvl(gb1.begin_balance_dr, 0) + nvl(gb1.period_net_dr, 0) -
nvl(gb1.begin_balance_cr, 0) - nvl(gb1.period_net_cr, 0)) 本期累计余额
from gl_balances gb1,
gl_code_combinations gcck1
where gb1.code_combination_id = gcck1.code_combination_id
and gb1.actual_flag = 'A'
and gcck1.TEMPLATE_ID is NULL
and gb1.translated_flag is null
and gb1.period_year=gb.period_year-1
and gb1.period_num=gb.period_num
and gcck1.segment3=gcck.segment3
group by
gcck1.segment3,
gb1.period_year,
gb1.period_num,
gb1.period_name ) 上期同期累计余额
from gl.gl_balances gb,
gl_code_combinations gcck
where gb.code_combination_id = gcck.code_combination_id
and gb.actual_flag = 'A'
and gcck.TEMPLATE_ID is NULL
and gb.translated_flag is null
group by
gcck.segment3,
gb.period_year,
gb.period_num,
gb.period_name) c
where c.科目编码(+) between b.segment3_low and b.segment3_high
and b.period_name=c.期间(+)
group by b.period_name,b.sequence,b.description) |
|