Greenplum数据库窗口使用(FIRST_VALUE)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xfg0218/article/details/82910776

目录

1、创建目标表,也就是需要合并写入的目标表

2、中间表,也就是用户只管插入的中间表表

3、向临时表中插入数据

4、窗口合并,按唯一值约束,仅提取一条(可能存在窗口内合并的需求,例如按时间取最新,比如以最后一条为准,又或者以有值,且最新的为准)

5、保存以上查询出来的结果到临时表

6、合并写入,将窗口提取的结果,合并写入目标表,INNER JOIN,覆盖旧记录,同时补齐旧的字段(以NULL为判断条件。如果新的记录没有值,则取旧记录的值。)提取。

7、查看目标表的数据


1、创建目标表,也就是需要合并写入的目标表

create table t( id int, c1 int , c2 int, c3 int, c4 int, c5 int, crt_time timestamp);

2、中间表,也就是用户只管插入的中间表表

create table t_tmp(like t);

like关键字是复制t表的结构

3、向临时表中插入数据

insert into t_tmp values(1,1,2,3,null,null,now());    
insert into t_tmp values(1,1,2,4,null,null,now());    
insert into t_tmp values(1,1,2,3,null,7,now());    
insert into t_tmp values(1,1,null,3,5,6,now());
insert into t_tmp values(2,2,34,1,1,4,now());
insert into t_tmp values(2,33,null,32,232,2,now());

4、窗口合并,按唯一值约束,仅提取一条(可能存在窗口内合并的需求,例如按时间取最新,比如以最后一条为准,又或者以有值,且最新的为准)

select distinct on (id)     
  id,
  first_value(c1) over (partition by id order by c1 desc nulls last) as c1,    
  first_value(c2) over (partition by id order by c2 desc nulls last) as c2,    
  first_value(c3) over (partition by id order by c3 desc nulls last) as c3,    
  first_value(c4) over (partition by id order by c4 desc nulls last) as c4,    
  first_value(c5) over (partition by id order by c5 desc nulls last) as c5,    
  first_value(crt_time) over (partition by id order by crt_time desc) as crt_time    
  from t_tmp ; 

5、保存以上查询出来的结果到临时表

create table t_tmp1 (like t) ;

insert into t_tmp1
select distinct on (id)     
  id,
  first_value(c1) over (partition by id order by c1 desc nulls last) as c1,    
  first_value(c2) over (partition by id order by c2 desc nulls last) as c2,    
  first_value(c3) over (partition by id order by c3 desc nulls last) as c3,    
  first_value(c4) over (partition by id order by c4 desc nulls last) as c4,    
  first_value(c5) over (partition by id order by c5 desc nulls last) as c5,    
  first_value(crt_time) over (partition by id order by crt_time desc) as crt_time    
  from t_tmp ; 

6、合并写入,将窗口提取的结果,合并写入目标表,INNER JOIN,覆盖旧记录,同时补齐旧的字段(以NULL为判断条件。如果新的记录没有值,则取旧记录的值。)提取。

insert into t_tmp
select
tt.id,     
coalesce(tt.c1, t.c1) as c1,     
coalesce(tt.c2, t.c2) as c2,     
coalesce(tt.c3, t.c3) as c3,     
coalesce(tt.c4, t.c4) as c4 ,     
coalesce(tt.c5, t.c5) as c5,     
coalesce(tt.crt_time, t.crt_time)  as  crt_time   
from
t_tmp1 as t   
inner join     
t_tmp tt
USING(id);

7、查看目标表的数据

select * from t;

猜你喜欢

转载自blog.csdn.net/xfg0218/article/details/82910776
今日推荐