以迭代方式更新日期列

时间:2013-06-11 11:21:16

标签: sql oracle date

我有一张表,我需要更新日期字段:EFFECTIVE_DATE和EXPIRATION_DATE。我有4行,其中AGREEMENT_NO = 212132647和OFFER_INSTANCE_ID = 506412800

AGREEMENT_NO PARAM_SEQ_NO PARAM_NAME            PARAM_VALUES   EFFECTIVE_DATE               EXPIRATION_DATE      OFFER_INSTANCE_ID
------------ ------------ --------------------- -------------- -------------------- -------------------- ---------- 
   212132647    152507704 SDG primary CTN value 3095334348     10-JUN-2013:00:00:00 10-JUN-2013:00:00:00 506412800 

   212132647    152509361 SDG primary CTN value 3095334356     10-JUN-2013:00:00:00 10-JUN-2013:00:00:00 506412800 

   212132647    152509421 SDG primary CTN value 3095334350     10-JUN-2013:00:00:00 10-JUN-2013:00:00:00 506412800 

   212132647    152509464 SDG primary CTN value 3095328533     10-JUN-2013:00:00:00 10-JUN-2013:00:00:00 506412800 

我想更新每个effective_date和到期日期,如下所示:

AGREEMENT_NO PARAM_SEQ_NO PARAM_NAME            PARAM_VALUES   EFFECTIVE_DATE               EXPIRATION_DATE      OFFER_INSTANCE_ID
------------ ------------ --------------------- -------------- -------------------- -------------------- ---------- 
   212132647    152507704 SDG primary CTN value 3095334348     10-JUN-2013:00:00:00 10-JUN-2013:00:00:01 506412800 

   212132647    152509361 SDG primary CTN value 3095334356     10-JUN-2013:00:00:01 10-JUN-2013:00:00:02 506412800 

   212132647    152509421 SDG primary CTN value 3095334350     10-JUN-2013:00:00:02 10-JUN-2013:00:00:03 506412800 

   212132647    152509464 SDG primary CTN value 3095328533     10-JUN-2013:00:00:03 10-JUN-2013:00:00:04 506412800 

有没有办法可以像

那样直接更新这些值
update table1 set expiration_date = effective_date + 1/24/3600 where <somecondition>
and for next row: effective_date = old_exp_date + 1/24/3600 , expiration_date = effective_date + 1/24/3600

表示所有行。

2 个答案:

答案 0 :(得分:0)

通过计算序列中的记录数量,这是一种相当强力的方法:

update table1
    set effective_date = effective_date +
                          (select count(*) - 1
                           from table1 t2
                           where t2.agreement_no = table1.agreement_no and
                                 t2.expiration_date = table1.expiration_date and
                                 t2.effective_date = table1.effective_date and
                                 t2.param_seq_no <= table1.param_seq_no
                          ) / (24*60*60)
       expiration_date = expiration_date +
                          (select count(*)
                           from table1 t2
                           where t2.agreement_no = table1.agreement_no and
                                 t2.expiration_date = table1.expiration_date and
                                 t2.effective_date = table1.effective_date and
                                 t2.param_seq_no <= table1.param_seq_no
                          ) / (24*60*60)

答案 1 :(得分:0)

非常简单。

你刚到达目的地。

您可以按照此link或以下是您想要的更新语句。

update table1 set expiration_date = effective_date + 1/24/3600 where <somecondition>
and for next row: effective_date = old_exp_date + (1/24/3600)+rownum , expiration_date = effective_date + (1/24/3600)+rownum