pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stephan Szabo <sszabo@megazone.bigpanda.com>
To: Bradley Kieser <brad@sms-speedway.com>
Cc: PgSQL ADMIN <pgsql-admin@postgresql.org>
Subject: Re: OT: using column in an interval
Date: Fri, 24 Mar 2006 14:09:55 -0800 (PST)
Message-ID: <20060324140436.N95370@megazone.bigpanda.com> (raw)
In-Reply-To: <4422A13D.8060804@sms-speedway.com>
References: <4422A13D.8060804@sms-speedway.com>


On Thu, 23 Mar 2006, Bradley Kieser wrote:

> All,
>
> I know that this isn't strictly an admin question but please forgive me
> for asking.
> I am writing a (admin) job which does a select off a PG database based
> on three columns:
>
> last_backup: timestamp
> backup_unit: integer - Represents day, week, quarter, annual, etc. The
> text is stored in backup_code (e.g. 'days', 'months')
> backup_period: integer - Represents the skip factor.
>
> i.e. if backup_unit is 1 (days) and period is 3, then together they
> represent "every three days".
>
> Obviously I want to do something like:
>
> select id, client
> from backupSchedule
> where last_backup + interval backup_period backup_code <= CURRENT_DATE
>
>
> However, interval seems to only take text such as
> inverval '3 days'
>
> and I get an error even with this:
>
> select id, client
> from backupSchedule
> where last_backup + interval backup_period::text || backup_code <=
> CURRENT_DATE
>
> Can someone please point me to the right statement to use for
> column-based interval arithmetic?
> The docs all give hardcoded text strings in examples. Not found one yet
> with a proper column-based query!

Well, that's because the interval <blah> syntax is for interval literals.

CAST( backup_period || ' ' || backup_code AS interval) should give you an
interval. If the units were constant, I'd say that using integer *
interval is a better idea, but I think you'd need a function that say took
backup_unit and gave back an interval of 1 <unit> to make that work, but
that would possibly be cleaner overall.




view thread (3+ messages)  latest in thread

Message-ID: <20060324140436.N95370@megazone.bigpanda.com>
Permalink:  ../20060324140436.N95370@megazone.bigpanda.com/
Also on:    postgresql.org/message-id/20060324140436.N95370@megazone.bigpanda.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: sszabo@megazone.bigpanda.com, brad@sms-speedway.com
  Subject: Re: OT: using column in an interval
  In-Reply-To: <20060324140436.N95370@megazone.bigpanda.com>
  References: <4422A13D.8060804@sms-speedway.com> <20060324140436.N95370@megazone.bigpanda.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox