| From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> | 
|---|---|
| To: | Larry Rosenman <ler(at)lerctr(dot)org> | 
| Cc: | PostgreSQL SQL List <pgsql-sql(at)postgresql(dot)org> | 
| Subject: | Re: trying to summarize into a new table by time... | 
| Date: | 2001-06-02 14:16:12 | 
| Message-ID: | 11605.991491372@sss.pgh.pa.us | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | pgsql-sql | 
Larry Rosenman <ler(at)lerctr(dot)org> writes:
> where early >= '2001-01-01 00:00:00' and
>       early <= '2001-01-02 05:59:59'
> BUT, I'm wondering if there is an easy way to generate the obvious 
> where clauses automatically?
Assuming this is being fired from a cron job started at or just after
each interval, you could do something with date_trunc:
regression=# select now();
          now
------------------------
 2001-06-02 10:12:37-04
(1 row)
regression=# select date_trunc('hour', now());
       date_trunc
------------------------
 2001-06-02 10:00:00-04
(1 row)
regression=# select date_trunc('hour', now()) - '6 hours'::interval;
        ?column?
------------------------
 2001-06-02 04:00:00-04
(1 row)
Or, if you're running the script once a day at midnight, use
current_date (or equivalently date_trunc('day',...)) and subtract
appropriate intervals from that.
regards, tom lane
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Larry Rosenman | 2001-06-02 14:44:35 | Re: trying to summarize into a new table by time... | 
| Previous Message | Larry Rosenman | 2001-06-02 13:50:12 | Re: trying to summarize into a new table by time... |