CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Oracle >  开发

请教一个查询

楼主StartDay()2005-05-12 14:36:33 在 Oracle / 开发 提问

操作表是T1,   该表的Key是f1,   f2,   f3,fMonth,   其中fMonth存的是月份。  
  现在把前月的所有数据找出来,对找出来的每一条纪录:  
  以该纪录的f1,   f2,   f3的值为条件检索当月的数据,如果检索不到数据,  
  就把该条纪录的fMonth改为当月后,插入数据库。  
   
  对于上面的要求,用pl/sql怎么实现好?  
   
  我现在的思路是检索前月数据然后一条条判断是不是要插入当月,  
  感觉是最慢的一种,所以希望各位大侠指点指点,先谢了。  
   
   
   
   
  问题点数:20、回复次数:4Top

1 楼zsfww1205(努力学习oracle)回复于 2005-05-12 18:02:04 得分 10

update   table     set   fmonth   =   Add_months(fmonth,   '1')    
  where   rowid   in                                      
  (  
  select   t1.rowid   from    
    (select   *   from   table   where   to_char(a,'mm')   =   to_char(sysdate,'mm')-   1)   t1,  
    (select   *   from   table   where   to_char(a,'mm')   =   to_char(sysdate,'mm')   )   t2  
  where   t1.f1   !=   t2.f1   and   t1.f2   !=   t2.f2     and   t1.f3   !=   t2.f3  
  )Top

2 楼StartDay()回复于 2005-05-13 14:49:03 得分 0

谢谢!  
  楼上的写法很直观,一看就懂了,  
  不过我总有种感觉,会不会还有技巧性更  
  强的写法呢?  
  Top

3 楼xiandaishihou(xiandaishitou)回复于 2005-05-13 15:26:32 得分 0

dingTop

4 楼duanzilin(寻)回复于 2005-05-13 16:42:15 得分 10

这里月份是number型  
  update   T1   a   set   fmonth   =   fmonth   +   1    
  where   exists   (  
          select   *   from(  
                  select   f1,f2,f3,fmonth,count(*)   over(partition   by   f1,f2,f3)   num    
                  from   T1  
                  where   extract(month   from   sysdate)   -   fmonth   <=   1)   b    
          where   b.num   =   1   and   b.f1=   a.f1   and   b.f2=   a.f2   and   b.f3=   a.f3)  
  and   a.fmonth   =   extract(month   from   sysdate)   -   1Top

相关问题

  • 查询
  • 查询?
  • 查询
  • 查询
  • 查询
  • 查询
  • 查询
  • 查询
  • 查询
  • 查询

关键词

  • 数据
  • fmonth
  • 纪录
  • sysdate
  • mm
  • where
  • table
  • char
  • select

得分解答快速导航

  • 帖主:StartDay
  • zsfww1205
  • duanzilin

相关链接

  • Oracle类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo