簡體   English   中英

以迭代方式更新日期列

[英]Update the date columns in iteratively manner

我有一個需要更新日期字段的表: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 

我想更新每個有效日期和到期日期,如下所示:

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

對於所有行。

通過計算序列中的記錄數,這是一種蠻力的方法:

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)

非常簡單。

您剛剛到達目的地。

您可以點擊此鏈接,或者下面是您想要的Update語句。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM