sql - Can I use a modulo to specify an interval in postgres? -


i have list items have not been updated multiple of 2 years after last update. run cron job once day.

i know can ugly like:

select art_id, art_update items  art_update = now()::date - interval '2 years' or art_update = now()::date - interval '4 years' or art_update = now()::date - interval '6 years' or art_update = now()::date - interval '8 years' or art_update = now()::date - interval '10 years'; 

is there way avoid checking modulo interval? or other generalised way express this?

you can generate series of dates @ 2 year intervals going today (to 10 years ago in below) , join table:

select i.art_id, i.art_update items     inner join generate_series(2, 10, 2) s (years)         on i.art_update = now()::date - interval '1 years' * s.years; 

example on sql fiddle

n.b appears marginally faster if generate dates in series, rather numbers:

select i.art_id, i.art_update items     inner join generate_series(now() - interval '10 years',                                  now() - interval '2 years',                                  interval '2 years') d (d)         on art_update = d.d::date; 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -