How to keep the value from previous records

时间:2015-10-30 21:47:45

标签: sql netezza aqua-data-studio

My original table 'T1' looks like this:

     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  .....  .....

The final table that I want to create is adding an additional variable 'new_var' based on some criteria. As you may notice, there are some records with the same date, and those criteria only work on the first record (order_ind=1). For the rest of the records with the same date, such as order_ind=2, or 3, the new_var value should be the same with the order_ind=1 record.

     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

The SQL codes that I wrote are like these:

     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
     ;

Any idea how to write the code for records with order_ind<>1?

1 个答案:

答案 0 :(得分: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)