如何保留以前记录的值

How to keep the value from previous records

我原来的table'T1'是这样的:

     ID  Date        Order_ind   Var2   Var3
      1  1/1/2015            1  .....  .....
      1  1/5/2015            1  .....  .....
      1  1/5/2015            2  .....  .....
      2  1/10/2015           1  .....  .....
      2  1/20/2015           1  .....  .....
      2  1/20/2015           2  .....  .....
      2  1/20/2015           3  .....  .....

我要创建的最终 table 是根据某些条件添加一个附加变量 'new_var'。您可能会注意到,有些记录具有相同的日期,并且这些条件仅适用于第一条记录 (order_ind=1)。对于其余日期相同的记录,例如order_ind=2,或3,new_var值应与order_ind=1记录相同。

     ID  Date        order_ind   Var1   Var2    new_var   
      1  1/1/2015            1  .....  .....    1
      1  1/5/2015            1  .....  .....    0
      1  1/5/2015            2  .....  .....    0
      2  1/10/2015           1  .....  .....    0
      2  1/20/2015           1  .....  .....    1
      2  1/20/2015           2  .....  .....    1
      2  1/20/2015           3  .....  .....    1

我写的SQL代码是这样的:

     SELECT *,
            CASE
            WHEN order_ind=1 and (criteria1....) THEN '1'
            WHEN order_ind=1 and (criteria2....) THEN '0' 
            WHEN order_ind<>1 .......(please advise how to code this) 
            END AS new_var
     FROM T1
     ;

知道如何为 order_ind<>1 的记录编写代码吗?

我会分几步完成。首先,创建一个 ind_1_new_var 列,其中仅包含 order_ind = 1 条记录的值。

select
  *
  ,case
    when order_ind = 1 and (criteria1...) then 1
    when order_ind = 1 and (criteria2...) then 0
    else null
  end ind_1_new_var
from
  t1;

然后构建您的 new_var 引用此专栏。

select
  *
  ,case
    when order_ind = 1 and (criteria1...) then 1
    when order_ind = 1 and (criteria2...) then 0
    else null
  end ind_1_new_var
  ,max(ind_1_new_var) over (
    partition by id, date
  ) new_var
from
  t1;

我不知道你的 criteria1,但这是我的 nz 数据库中的一个工作示例,其中包含你提供的数据。

TEST_DB(ADMIN)=> select * from t1 order by 1,2,3;
 ID |  T1_DATE   | ORDER_IND | VAR1 | VAR2
----+------------+-----------+------+------
  1 | 2015-01-01 |         1 |    0 |    0
  1 | 2015-01-05 |         1 |    0 |    0
  1 | 2015-01-05 |         2 |    0 |    0
  2 | 2015-01-10 |         1 |    0 |    0
  2 | 2015-01-20 |         1 |    0 |    0
  2 | 2015-01-20 |         2 |    0 |    0
  2 | 2015-01-20 |         3 |    0 |    0
(7 rows)

TEST_DB(ADMIN)=> select
TEST_DB(ADMIN)->   *
TEST_DB(ADMIN)->   ,case
TEST_DB(ADMIN)->     when order_ind = 1 and (
TEST_DB(ADMIN)(>       (id = 1 and t1_date = '2015-01-01')
TEST_DB(ADMIN)(>       or (id = 2 and t1_date = '2015-01-20')
TEST_DB(ADMIN)(>     ) then 1
TEST_DB(ADMIN)->     when order_ind = 1 and (
TEST_DB(ADMIN)(>       (id = 1 and t1_date = '2015-01-05')
TEST_DB(ADMIN)(>       or (id = 2 and t1_date = '2015-01-10')
TEST_DB(ADMIN)(>     ) then 0
TEST_DB(ADMIN)->     else null
TEST_DB(ADMIN)->   end ind_1_new_var
TEST_DB(ADMIN)->   ,max(ind_1_new_var) over (
TEST_DB(ADMIN)(>     partition by id, t1_date
TEST_DB(ADMIN)(>   ) new_var
TEST_DB(ADMIN)-> from
TEST_DB(ADMIN)->   t1
TEST_DB(ADMIN)-> order by 1,2,3;
 ID |  T1_DATE   | ORDER_IND | VAR1 | VAR2 | IND_1_NEW_VAR | NEW_VAR
----+------------+-----------+------+------+---------------+---------
  1 | 2015-01-01 |         1 |    0 |    0 |             1 |       1
  1 | 2015-01-05 |         1 |    0 |    0 |             0 |       0
  1 | 2015-01-05 |         2 |    0 |    0 |               |       0
  2 | 2015-01-10 |         1 |    0 |    0 |             0 |       0
  2 | 2015-01-20 |         1 |    0 |    0 |             1 |       1
  2 | 2015-01-20 |         2 |    0 |    0 |               |       1
  2 | 2015-01-20 |         3 |    0 |    0 |               |       1
(7 rows)