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

 

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

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

样例SQL如下。

 1   select rownum  序号 ,
 2        ld.comcode 分公司,
 3        'B2B' 来源,
 4        ed.tranno 银行交易号,
 5        (select bb.newcontno
 6           from b2b_contno bb
 7          where trim(bb.oldcontno) = trim(lc.contno)) 保单号新,
 8        lc.contno 保单号旧,
 9        lc.appntname 投保人,
10        ms.pol_status 保单状态,
11        ed.makedate 客户申请日期,
12        ms.UPD_DATE 保全受理日期,
13        lead(to_char(ed.fingetamt),(select count(*)-1 from EdorAPDetail),null)  over (partition by  lc.contno order by lc.contno) as 购买总金额,
14        ea.accountcode 基金代码,
15        (select  b.CODE_DES from  bankandinsurercodemapping b  where  b.COMCode = 'ICBC'
16    and b.CODETYPE = 'accounttype'  and trim(b.BANK_CODE) = trim(ea.accountcode)) 基金名称, 
17        ea.accountpercent 购买比例,
18        '现金' 支付方式,
19        ed.bankcode 银行代码,
20        ed.accnumber 账号,
21        ed.bak1 户名,
22        ld.name 营业单位,
23        la.agentcom 代理人编号,
24        la.name 代理人姓名,
25        ed.appntphone 投保人电话
26        from lccont              lc, --日志表
27        lacom               la, --银行代理机构表
28        edoraddinsure ed, --保全业务表
29        ldcom               ld, --保险分公司表
30        B2B_ITF_T_POL_MST   ms, --ODS推数表
31        EdorAPDetail        ea
32 
33        where ed.funcflag in (1020)
34     and lc.agentcom = la.agentcom
35    and ld.comcode = la.managecom
36    and ms.comp_code=ld.comcode
37    
38    and trim(ed.bankcode) = trim(lc.newbankcode)
39    and ed.bankcode='0011'
40  
41    and trim(lc.contno) = trim(ed.contno)
42    and trim(ms.pol_no) = trim(ed.contno)
43    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置空。并按某某顺序。

 

 

 

posted @ 2013-11-26 22:12  程序员张越  阅读(1804)  评论(0编辑  收藏  举报