oracle with 使用

oracle查询过程中,取查询结果中某些特别的数据,可以使用with语句,避免很多嵌套的复杂查询

下面是例子:

1.取结果中第一条数据、最后一条数据、非第一条最后一条数据中状态为‘Y’的数据

with tmp as
 (select rownum rn, t.*
    from (select ad.*
            from t_srv_addresscollection ad
           where ad.collection_time >= trunc(sysdate-1 )
             and ad.collection_time < trunc(sysdate)
             and ad.driver_code = '240209'
           order by ad.collection_time) t)
select *
  from tmp
 where (tmp.rn = 1)
    or (tmp.rn <> 1 and tmp.rn <> (select max(rn) from tmp) and
       tmp.is_stop = 'Y')
    or (tmp.rn = (select max(rn) from tmp))

2.with   insert 插入语句

/**
  统计快递员 所属 经营本部,事业部,大区,小区,营业部(或者 点部)
  */
  for c in (select v.empcode, v.deptcode from v_emp_courier v) loop
    insert into t_evs_courier_info
      with t as
       (select d.deptcode, d.deptname, d.deptlevel
          from t_org_department d
         where d.active = 'Y'
         start with d.deptcode = c.deptcode
        connect by nocycle prior d.parent_org_code = d.deptcode)
      select sys_guid(),
             sysdate,
             c.empcode,
             v_product_date,
             t3.deptcode,
             t3.deptname,
             t4.deptcode,
             t4.deptname,
             t5.deptcode,
             t5.deptname,
             t6.deptcode,
             t6.deptname,
             t7.deptcode,
             t7.deptname
        from (select t.deptcode, t.deptname from t where t.deptlevel = 3) t3,
             (select t.deptcode, t.deptname from t where t.deptlevel = 4) t4,
             (select t.deptcode, t.deptname from t where t.deptlevel = 5) t5,
             (select t.deptcode, t.deptname from t where t.deptlevel = 6) t6,
             (select t.deptcode, t.deptname from t where t.deptlevel = 7) t7;
  end loop;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值