Postgres UPSERT修改冲突然后插入记录

时间:2019-02-13 16:21:56

标签: postgresql insert sql-update upsert

我正在努力构建一个UPSERT脚本,该脚本在发生冲突时(1)修改现有记录以避免冲突,然后(2)插入新记录。我正在尝试建立一个在保持空间的同时保持时间点保真度的表。从理论上讲,与捕获不变的重复数据切片相比,正确捕获数据中的更改效率更高。

我已经建立了一个示例来帮助指出我要做什么。在特定的时间段内,有三名赛车手在特定的赛道上。其中一名驾驶员决定改变赛道,而另两名则决定留在原地。在稍后的某个时间点,第四个驾驶员进入图片。

我希望能够查询表格并说“今天谁在哪个赛道上?”

希望这是有道理的。

谢谢。

----------------------Set up an example table
create table drivers(
firstName varchar(30)
,lastName varchar(30)
,raceTrack varchar(30)
,effectiveDate date 
,expirationDate date
);

insert into drivers
select 'ricky', 'bobby', 'talladega', cast('2019-01-01' as date), cast('9999-12-31' as date)
union
select 'cal', 'naughton', 'martinsville', cast('2019-01-01' as date), cast('9999-12-31' as date)
union
select 'jean', 'girard', 'daytona', cast('2019-01-01' as date), cast('9999-12-31' as date);

----------------------Expire the daytona record for Jean
----This is the UPDATE portion of the UPSERT and triggers a conflict with the UPSERT since firstname and lastname match. 
update drivers set expirationdate = cast('2019-02-10' as date)
where firstname = 'jean' and lastname = 'girard';

----------------------Insert new circuit de monaco record for Jean
----This is the INSERT portion of the UPSERT
insert into drivers
select 'jean', 'girard', 'circuit de monaco', cast('2019-02-11' as date), cast('9999-12-31' as date);

----------------------A new driver comes into the data
----This is an example of the expected behavior without a conflict
insert into drivers
select 'reese', 'bobby', 'greenville pickens', cast('2019-02-12' as date), cast('9999-12-31' as date);

----------------------Query all of the driver and track records for some day in January 2019

select * 
from drivers
where cast('2019-01-11' as date) between effectivedate and expirationdate;

----------------------Query all of the driver and track records for some day in February 2019

select * 
from drivers
where cast('2019-02-11' as date) between effectivedate and expirationdate;

select * 
from drivers
where cast('2019-02-12' as date) between effectivedate and expirationdate;

0 个答案:

没有答案