postgresql - Query Only Specific Days of Month -


i'm not sure begin on solving problem. need update record every 3rd monday of month. in postgres can query every 2nd or 3rd monday, or little more abstract every nth day of nth week?

i'm looking elegant answer postgresql. right have crude this:

select d generate_series(date_trunc('week',timestamp '2015-02-01' + interval '13 days'), timestamp '2015-02-01' + interval '1 month -1 day', interval '14 days') d; 

i use calendar table queries one.

to select third monday of every month in 2015, can query calendar table this.

select cal_date calendar year_of_date = 2015   , day_of_week = 'mon'   , day_of_week_ordinal = 3 order cal_date; 
 cal_date -- 2015-01-19 2015-02-16 2015-03-16 2015-04-20 2015-05-18 2015-06-15 2015-07-20 2015-08-17 2015-09-21 2015-10-19 2015-11-16 2015-12-21 

code create calendar table. (this how pgadminiii presents through create script menu selection.)

create table calendar (   cal_date date not null,   year_of_date integer not null,   month_of_year integer not null,   day_of_month integer not null,   day_of_week character(3) not null,   day_of_week_ordinal integer not null,   iso_year integer not null,   iso_week integer not null,   cal_quarter integer,   constraint calendar_pkey primary key (cal_date),   constraint cal_quarter_check check (cal_quarter = case     when date_part('month'::text, cal_date) >= 1::double precision , date_part('month'::text, cal_date) <= 3::double precision 1     when date_part('month'::text, cal_date) >= 4::double precision , date_part('month'::text, cal_date) <= 6::double precision 2     when date_part('month'::text, cal_date) >= 7::double precision , date_part('month'::text, cal_date) <= 9::double precision 3     when date_part('month'::text, cal_date) >= 10::double precision , date_part('month'::text, cal_date) <= 12::double precision 4     else null::integer end),   constraint cal_quarter_range check (cal_quarter >= 1 , cal_quarter <= 4),   constraint calendar_check check (year_of_date::double precision = date_part('year'::text, cal_date)),   constraint calendar_check1 check (month_of_year::double precision = date_part('month'::text, cal_date)),   constraint calendar_check2 check (day_of_month::double precision = date_part('day'::text, cal_date)),   constraint calendar_check3 check (day_of_week::text = case     when date_part('dow'::text, cal_date) = 0::double precision 'sun'::text     when date_part('dow'::text, cal_date) = 1::double precision 'mon'::text     when date_part('dow'::text, cal_date) = 2::double precision 'tue'::text     when date_part('dow'::text, cal_date) = 3::double precision 'wed'::text     when date_part('dow'::text, cal_date) = 4::double precision 'thu'::text     when date_part('dow'::text, cal_date) = 5::double precision 'fri'::text     when date_part('dow'::text, cal_date) = 6::double precision 'sat'::text     else null::text end),   constraint calendar_check4 check (day_of_week_ordinal = case     when day_of_month >= 1 , day_of_month <= 7 1     when day_of_month >= 8 , day_of_month <= 14 2     when day_of_month >= 15 , day_of_month <= 21 3     when day_of_month >= 22 , day_of_month <= 28 4     else 5 end),   constraint calendar_check5 check (iso_year::double precision = date_part('isoyear'::text, cal_date)),   constraint calendar_check6 check (iso_week::double precision = date_part('week'::text, cal_date)) ) (   oids=false ); 

you need

  • grant , revoke statments--very few people should allowed change content of kind of table, and
  • suitable create index statements.

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -