PostgreSQL的
generate_series()函数可以创建一个包含连续日期列表的视图:
with calendar as ( select ((select min(date) from test)::date + (n || ' days')::interval)::date cal_date from generate_series(0, (select max(date) - min(date) from test)) n)select cal_datefrom calendar cleft join test t on t.date = c.cal_datewhere t.date is null;
该表达
select max(date) - min(date) from test可能偏离一。每月计数天数
识别无效月份的一种方法是创建两个视图。第一个计算每个站点每个月应产生的每日读数数量。(注意
climate.calendar转换为
climate_calendar。)秒数返回每个电台每月产生的实际每日读数。每个站每月最多可使用的天数
该视图将返回每个站点一个月中的实际天数。(例如,二月将始终为28天或29天。)
每个站每月的实际天数create view count_max_station_calendar_days as with calendar as ( select ((select min(d) from climate_calendar)::date + (n || ' days')::interval)::date cal_date from generate_series(0, (select max(d) - min(d) from climate_calendar)) n)select n, extract(year from cal_date) yr, extract(month from cal_date) mo, count(*) num_daysfrom stations cross join calendargroup by n, yr, moorder by n, yr, mo
返回的总天数将少于计数。(例如,一月将始终少于31天。)
create view count_actual_station_calendar_days asselect n, extract(year from d) yr, extract(month from d) mo, count(*) num_daysfrom climate_calendargroup by n, yr, moorder by n, yr, mo;
ORDER BY在生产中删除这些子句(它们对开发很有帮助)。比较视图
将这两个视图结合在一起,以标识需要标记的站点和月份,进入一个新视图:
create view invalid_station_months as select m.n, m.yr, m.mo, m.num_days - a.num_days num_days_missingfrom count_max_station_calendar_days minner join count_actual_station_calendar_days a on (m.n = a.n and m.yr = a.yr and m.mo = a.mo and m.num_days <> a.num_days)n yr mo num_days_missing--A 1982 1 1E 2007 3 1
该列
num_days_missing不是必需的,但很有用。
这些是需要更新的行:
更新数据库select cc.* from climate_calendar ccinner join invalid_station_months im on (cc.n = im.n and extract(year from cc.d) = im.yr and extract(month from cc.d) = im.mo)where valid = true
要更新它们,
id密钥很方便。
update climate_calendarset valid = falsewhere id in ( select id from climate_calendar cc inner join invalid_station_months im on (cc.n = im.n and extract(year from cc.d) = im.yr and extract(month from cc.d) = im.mo) where valid = true);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)