您的位置:首页 > 数据库 > Oracle

Oracle中本行记录和上一行记录进行比较lead over 函数处理

2013-11-26 22:12 465 查看
遇到问题:多表关联查询,有一个要求是,同一保单号,对应多个投资产品Code.以及投资比例,每一个保单号有一个总的投资金额。要求同一保单号那一行,只有第一个总金额有值,剩下的code对应的总金额置空。

简述问题:如果本行记录和上一行记录,某字段值相同,则怎么处理,另外一列的值。

样例SQL如下。

select rownum  序号 ,
ld.comcode 分公司,
'B2B' 来源,
ed.tranno 银行交易号,
(select bb.newcontno
from b2b_contno bb
where trim(bb.oldcontno) = trim(lc.contno)) 保单号新,
lc.contno 保单号旧,
lc.appntname 投保人,
ms.pol_status 保单状态,
ed.makedate 客户申请日期,
ms.UPD_DATE 保全受理日期,
lead(to_char(ed.fingetamt),(select count(*)-1 from EdorAPDetail),null)  over (partition by  lc.contno order by lc.contno) as 购买总金额,
ea.accountcode 基金代码,
(select  b.CODE_DES from  bankandinsurercodemapping b  where  b.COMCode = 'ICBC'
and b.CODETYPE = 'accounttype'  and trim(b.BANK_CODE) = trim(ea.accountcode)) 基金名称,
ea.accountpercent 购买比例,
'现金' 支付方式,
ed.bankcode 银行代码,
ed.accnumber 账号,
ed.bak1 户名,
ld.name 营业单位,
la.agentcom 代理人编号,
la.name 代理人姓名,
ed.appntphone 投保人电话
from lccont              lc, --日志表
lacom               la, --银行代理机构表
edoraddinsure ed, --保全业务表
ldcom               ld, --保险分公司表
B2B_ITF_T_POL_MST   ms, --ODS推数表
EdorAPDetail        ea

where ed.funcflag in (1020)
and lc.agentcom = la.agentcom
and ld.comcode = la.managecom
and ms.comp_code=ld.comcode

and trim(ed.bankcode) = trim(lc.newbankcode)
and ed.bankcode='0011'

and trim(lc.contno) = trim(ed.contno)
and trim(ms.pol_no) = trim(ed.contno)
and trim(ea.contno) = trim(ed.contno)


核心处理要点:

lead(to_char(ed.fingetamt),(select count(*)-1 from EdorAPDetail),null) over (partition by lc.contno order by lc.contno) as 购买总金额,

心得:Oracle中lead (mm,1,null) over(partition by bb order by tt)函数。本行记录和上一行记录比较,partition by lc.contno相同的话,就给ed.fingetamt置空。并按某某顺序。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐