目标是根据2个业务条件添加一个具有唯一ID的新列(x_event_code)。
Table : tmp_event_cd
ID Member First_Date Last_date
1 123455678 8/23/2014 8/24/2014
2 123455678 8/23/2014 8/24/2014
3 123455678 8/24/2014 8/24/2014
4 123455678 8/24/2014 8/24/2014
5 123455678 8/25/2014 8/25/2014
6 123455678 10/25/2014 10/25/2014
1 567890124 10/25/2014 10/25/2014
2 567890124 10/25/2014 10/25/2014
3 567890124 12/27/2014 12/27/2014
Psql:
Drop table if exists tmp_event_cd_1 ;
Create table tmp_event_cd_1 as
(
Select
ID,Member,First_Date,Last_date,
(Case
WHEN (ID = '1' ) then nextval('Seq_1')
WHEN (ID != '1' and First_Date::Date between
-- Member_first_Date
(FIRST_VALUE(First_date) over (partition by Member order by First_date)::Text ::date) AND
-- Member_last_date
(FIRST_VALUE(Last_date) over (partition by Member order by First_date)::Text ::date) )
then Currval('Seq_1')
/*Currval() not supported*/
/*Retain the value assigned above untill next member*/
Else nextval('Seq_1')
end ) as x_event_code
from tmp_event_cd
order by Member,id,First_Date, Last_date
)distributed randomly;
Expected final table:
ID Member First_Date Last_date x_event_code
1 123455678 8/23/2014 8/24/2014 1
2 123455678 8/23/2014 8/24/2014 1
3 123455678 8/24/2014 8/24/2014 1
4 123455678 8/24/2014 8/24/2014 1
5 123455678 8/25/2014 8/25/2014 2
6 123455678 10/25/2014 10/25/2014 3
1 567890124 10/25/2014 10/25/2014 4
2 567890124 10/25/2014 10/25/2014 4
3 567890124 12/27/2014 12/27/2014 5
“ x_event_code”应保留序列“ Seq_1”中的当前值。 我的Postgres版本不支持。 我尝试在第二种情况下使用(从seq_1中选择last_value),但是即使nextval()增加了,它也只保留了一个相同的值。
我不能使用基于变量的函数,因为该表具有数十亿条记录。
对此表示感谢。