我们对数据仓库做类似的事情。这是一个两阶段的操作。一是只添加主键字段的新记录,二是更新新记录的其他字段。数据库引擎是redbrick。redbrick 语法类似于 sql server 语法。
这是插入查询:
insert into period (date)
select
case when days_in_year((select max(date) from period))=365 -- current max year not leap year
and days_in_year(dateadd(day,1,(select max(date) from period)))=365 --new year not leap year
then dateadd(day,365,date)
else dateadd(day,366,date)end
from period
where date between
case when days_in_year(dateadd(day,1,(select max(date) from period)))=366 -- new year is leap year
or days_in_year((select max(date) from period))=366 -- current max year is leap year
then dateadd(day,-365, (select max(date) from period)) -- Dec 31 of year before current max year
else dateadd(day,-364, (select max(date) from period)) end --Jan 1 of current max year
and
case when days_in_year((select max(date) from period))=366 -- current max year is leap year
then dateadd(day,-1, (select max(date) from period))-- Dec 30 of current max year
else (select max(date) from period) end -- Dec 31 of current max year
and current_date > dateadd(month,-9,(select max(date) from period))
请注意,days_in_year 是一个红砖宏。在这种情况下,它相当于一个 sql server 用户定义的函数。相当于这个红砖代码
extract(dayofyear from date(concat(datename(year,%1),'-12-31')))
其中 %1 是传递给宏的参数。