如何向表中添加状态

时间:2016-04-12 17:56:38

标签: sql oracle plsql

我有下表,其中是从我的数据库剪辑。我有两种类型的合同。

我:客户支付前6个月60美元,接下来6个月120美元(111个客户)

II:客户支付前6个月60美元,但如果仍然需要支付60美元,合同将在6个月延长,整个合同是18个月。 (321仍然支付的客户)

ID_Client | Amount | Amount_charge | Lenght | Date_from   | Date_to    | Reverse
--------------------------------------------------------------------------------
111         60         60            12       2015-01-01   2015-01-31   12
111         60         60            12       2015-02-01   2015-02-28   11
111         60         60            12       2015-03-01   2015-03-31   10
111         60         60            12       2015-04-01   2015-04-30   9
111         60         60            12       2015-05-01   2015-05-31   8
111         60         60            12       2015-06-01   2015-06-30   7
111         120        60            12       2015-07-01   2015-07-31   6
111         120        60            12       2015-08-01   2015-08-31   5
111         120        60            12       2015-09-01   2015-09-30   4
111         120        60            12       2015-10-01   2015-10-31   3
111         120        60            12       2015-11-01   2015-11-30   2
111         120        60            12       2015-12-01   2015-12-31   1
111         120        60            12       2016-01-01   2015-01-31   0
111         120        60            12       2016-02-01   2015-02-29   0
321         60         60            12       2015-01-01   2015-01-31   12
321         60         60            12       2015-02-01   2015-02-28   11
321         60         60            12       2015-03-01   2015-03-31   10
321         60         60            12       2015-04-01   2015-04-30   9
321         60         60            12       2015-05-01   2015-05-31   8
321         60         60            12       2015-06-01   2015-06-30   7
321         60         60            12       2015-07-01   2015-07-31   6
321         60         60            12       2015-08-01   2015-08-31   5
321         60         60            12       2015-09-01   2015-09-30   4
321         60         60            12       2015-10-01   2015-10-31   3
321         60         60            12       2015-11-01   2015-11-30   2
321         60         60            12       2015-12-01   2015-12-31   1
321         60         60            12       2016-01-01   2016-01-30   0
321         60         60            12       2016-02-01   2016-02-31   0
321         60         60            12       2016-03-01   2016-03-30   0
321         60         60            12       2016-04-01   2016-04-31   0

我需要添加状态列。

A - 正常的协议期

D - 协议在6个月后加倍但在12个月之后是E(nd of agreemnt)

E - 合同完成

L - 延长6个月后的合同,在18个月后,状态将为E型

对于321客户在12个月后,合同的长度从12更新到18

我有很多客户,所以我认为所有客户都会使用循环更好吗?

 ID_Client | Amount | Amount_charge | Lenght | Date_from   | Date_to    | Reverse | Status
-----------------------------------------------------------------------------------------
111         60         60            12       2015-01-01   2015-01-31   12          A
111         60         60            12       2015-02-01   2015-02-28   11          A 
111         60         60            12       2015-03-01   2015-03-31   10          A
111         60         60            12       2015-04-01   2015-04-30   9           A
111         60         60            12       2015-05-01   2015-05-31   8           A
111         60         60            12       2015-06-01   2015-06-30   7           A
111         120        60            12       2015-07-01   2015-07-31   6           D
111         120        60            12       2015-08-01   2015-08-31   5           D
111         120        60            12       2015-09-01   2015-09-30   4           D
111         120        60            12       2015-10-01   2015-10-31   3           D
111         120        60            12       2015-11-01   2015-11-30   2           D
111         120        60            12       2015-12-01   2015-12-31   1           D
111         120        60            12       2016-01-01   2015-01-31   0           E
111         120        60            12       2016-02-01   2015-02-29   0           E
321         60         60            12       2015-01-01   2015-01-31   12          A
321         60         60            12       2015-02-01   2015-02-28   11          A
321         60         60            12       2015-03-01   2015-03-31   10          A
321         60         60            12       2015-04-01   2015-04-30   9           A
321         60         60            12       2015-05-01   2015-05-31   8           A
321         60         60            12       2015-06-01   2015-06-30   7           A
321         60         60            12       2015-07-01   2015-07-31   6           L
321         60         60            12       2015-08-01   2015-08-31   5           L
321         60         60            12       2015-09-01   2015-09-30   4           L
321         60         60            12       2015-10-01   2015-10-31   3           L
321         60         60            12       2015-11-01   2015-11-30   2           L
321         60         60            12       2015-12-01   2015-12-31   1           L
321         60         60            18       2016-01-01   2016-01-30   0           L
321         60         60            18       2016-02-01   2016-02-31   0           L
321         60         60            18       2016-03-01   2016-03-30   0           L
321         60         60            18       2016-04-01   2016-04-31   0           L

1 个答案:

答案 0 :(得分:0)

如果反向列是我的想法:

var bar = [];
function myFunction2() {
    bar.push('pubs');
    document.getElementById("demo").innerHTML = bar; //Prints on website 
}

更好的是计算总和。每月的金额来自首次付款。像这样:

update table1 a
set "Status"=
  CASE
      WHEN A."Reverse" > 6 THEN
         'A'
      WHEN A."Reverse" > 0 THEN
         DECODE (A."Amount", A."Amount_charge", 'L', 'D')
      ELSE
         CASE
            WHEN A."Amount" <> A."Amount_charge" THEN
               'E'
            ELSE
               CASE WHEN ADD_MONTHS ( (SELECT b."Date_from" FROM table1 b WHERE a."ID_Client" = b."ID_Client" AND b."Reverse" = 1),6) > a."Date_from" THEN 'L'
                  ELSE
                     'E'
               END
         END
   END