请教一个查询
操作表是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




