select distinct pm.policyno,
                decode(pr.plancode, '0000', pr.riskcode, pr.plancode) as riskcode,
                pr.startdate,
                pr.endDate,
                pm.cancelind,
                pm.renewedInd,
                pm.surrenderInd,
                pm.validind,
                pm.companycode
  from GuPolicyMain pm, GuPolicyRisk pr
 where pm.policyNo = pr.policyNo
   AND (pm.coinsInd = '0')
   AND (pm.cancelInd = '0')
   AND (pm.renewedInd = '0')
   AND (pm.surrenderInd = '0')
   AND (pr.validInd = '1')
   and pm.companyCode not in
       ('010601', '0106010097', '01060101', '0106010197')
   and to_char(pr.enddate, 'yyyy-mm-dd hh:mm:dd') <=
       to_char('2017-01-31 23:59:59')
   and to_char(pr.enddate, 'yyyy-mm-dd hh:mm:dd') >=
       to_char('2017-01-01 00:00:00')
   and pm.businessType not in ('2', '3')
   and pm.standardInd is null
   and exists (select riskcode
          from ggriskconfigvalue
         where configcode = 'RENEWAL_NEWPROCESSIND'
           and validInd = '1'
           and riskcode = pr.riskcode)
   and pr.plancode = '0000'
union all
select distinct pm.policyno,
                decode(pr.plancode, '0000', pr.riskcode, pr.plancode) as riskcode,
                pr.startdate,
                pr.endDate,
                pm.cancelind,
                pm.renewedInd,
                pm.surrenderInd,
                pm.validind,
                pm.companycode
  from GuPolicyMain pm, GuPolicyRisk pr
 where pm.policyNo = pr.policyNo
   AND (pm.coinsInd = '0')
   AND (pm.cancelInd = '0')
   AND (pm.renewedInd = '0')
   AND (pm.surrenderInd = '0')
   AND (pr.validInd = '1')
   and pm.companyCode not in
       ('010601', '0106010097', '01060101', '0106010197')
   and to_char(pr.enddate, 'yyyy-mm-dd hh:mm:dd') <=
       to_char('2017-01-31 23:59:59')
   and to_char(pr.enddate, 'yyyy-mm-dd hh:mm:dd') >=
       to_char('2017-01-01 00:00:00')
   and pm.businessType not in ('2', '3')
   and pm.standardInd is null
   and pr.plancode <> '0000'
   and exists (select riskcode
          from ggriskconfigvalue
         where configcode = 'RENEWAL_NEWPROCESSIND'
           and validInd = '1'
           and riskcode = pr.planCode)
  union all
  select distinct pm.policyno,
                  decode(pr.plancode, '0000', pr.riskcode, pr.plancode) as riskcode,
                  pr.startdate,
                  pr.endDate,
                  pm.cancelind,
                  pm.renewedInd,
                  pm.surrenderInd,
                  pm.validind,
                  pm.companycode
    from GuPolicyMain pm, GuPolicyRisk pr
   where pm.policyNo = pr.policyNo
     AND (pm.coinsInd = '0')
     AND (pm.cancelInd = '0')
     AND (pm.renewedInd = '0')
     AND (pm.surrenderInd = '0')
     AND (pr.validInd = '1')
     and pm.companyCode in
         ('010601', '0106010097', '01060101', '0106010197')
     and to_char(pr.enddate, 'yyyy-mm-dd hh:mm:dd') <=
         to_char('2017-02-28 23:59:59')
     and to_char(pr.enddate, 'yyyy-mm-dd hh:mm:dd') >=
         to_char('2017-02-01 00:00:00')
     and pm.businessType not in ('2', '3')
     and pm.standardInd is null
     and exists (select riskcode
            from ggriskconfigvalue
           where configcode = 'RENEWAL_NEWPROCESSIND'
             and validInd = '1'
             and riskcode = pr.riskcode)
     and pr.plancode = '0000'
  union all
  select distinct pm.policyno,
                  decode(pr.plancode, '0000', pr.riskcode, pr.plancode) as riskcode,
                  pr.startdate,
                  pr.endDate,
                  pm.cancelind,
                  pm.renewedInd,
                  pm.surrenderInd,
                  pm.validind,
                  pm.companycode
    from GuPolicyMain pm, GuPolicyRisk pr
   where pm.policyNo = pr.policyNo
     AND (pm.coinsInd = '0')
     AND (pm.cancelInd = '0')
     AND (pm.renewedInd = '0')
     AND (pm.surrenderInd = '0')
     AND (pr.validInd = '1')
     and pm.companyCode in
         ('010601', '0106010097', '01060101', '0106010197')
     and to_char(pr.enddate, 'yyyy-mm-dd hh:mm:dd') <=
         to_char('2017-02-28 23:59:59')
     and to_char(pr.enddate, 'yyyy-mm-dd hh:mm:dd') >=
         to_char('2017-02-01 00:00:00')
     and pm.businessType not in ('2', '3')
     and pm.standardInd is null
     and pr.plancode <> '0000'
     and exists (select riskcode
            from ggriskconfigvalue
           where configcode = 'RENEWAL_NEWPROCESSIND'
             and validInd = '1'
             and riskcode = pr.planCode)
 order by companycode, riskCode, policyno

解决方案 »

  1.   

    select distinct pm.policyno,
                    decode(pr.plancode, '0000', pr.riskcode, pr.plancode) as riskcode,
                    pr.startdate,
                    pr.endDate,
                    pm.cancelind,
                    pm.renewedInd,
                    pm.surrenderInd,
                    pm.validind,
                    pm.companycode
      from GuPolicyMain pm, GuPolicyRisk pr
     where pm.policyNo = pr.policyNo
       AND (pm.coinsInd = '0')
       AND (pm.cancelInd = '0')
       AND (pm.renewedInd = '0')
       AND (pm.surrenderInd = '0')
       AND (pr.validInd = '1')
       and pm.companyCode not in
           ('010601', '0106010097', '01060101', '0106010197')
       and to_char(pr.enddate, 'yyyy-mm-dd hh:mm:dd') <=
           to_char('2017-02-28 23:59:59')
       and to_char(pr.enddate, 'yyyy-mm-dd hh:mm:dd') >=
           to_char('2017-01-01 00:00:00')
       and pm.businessType not in ('2', '3')
       and pm.standardInd is null
       and exists (select riskcode
              from ggriskconfigvalue
             where configcode = 'RENEWAL_NEWPROCESSIND'
               and validInd = '1'
               and riskcode = pr.riskcode)
    order by companycode, riskCode, policyno;看了下,先改成这样看看执行计划
      

  2.   

    你的4个union all子语句是不是只有 pr.plancode <> '0000' 和pr.plancode =  '0000'的区别,还有日期上的区别?
      

  3.   

    其实这么算可能有一点点的问题,因为原语句是先distinct后再union all,2个union all子句中查出有相同的行值,不会剔除重复。
    这个语句直接查后会distinct关键字剔除了重复。
    具体要看你的字段 decode(pr.plancode, '0000', pr.riskcode, pr.plancode) ,会不会出现0000 和 riskcode的值的情况
      

  4.   

    还有pm.companyCode 这个 业务要求根据这个不同 取不同日期区间,同时还要考虑pr.plancode 不同
      

  5.   

    pm.companyCode  4个语句的条件不都是一样的么?
      

  6.   

    就是说 当pm.companyCode in 的时候 日期要在2017年1月里;not in的时候 月份在2017年2月里,同时考虑pr的plancode,plancode有一种情况是0000.这时候取riskcode跟ggriskconfigvalue关联;若是pr.plancode<>'0000' 则取plancode跟ggriskconfigvalue做关联,仔细看肯定不一样的