pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Time Aggregates
4+ messages / 3 participants
[nested] [flat]

* Time Aggregates
@ 2000-08-02 13:41 Itai Zukerman <zukerman@math-hat.com>
  2000-08-02 15:27 ` Re: Time Aggregates Thomas Lockhart <lockhart@alumni.caltech.edu>
  0 siblings, 1 reply; 4+ messages in thread

From: Itai Zukerman @ 2000-08-02 13:41 UTC (permalink / raw)
  To: pgsql-sql

Hi,

I'm currently doing this:

  SELECT symbol, date_trunc('minute', posted),
         min(price), max(price), avg(price)
  FROM trade
  GROUP BY symbol, date_trunc('minute', posted);

to get a list of minute-averages of trade prices.  I get the feeling
that this is bad form, that I should be doing this some other way.  Is
that the case?

-itai



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Time Aggregates
  2000-08-02 13:41 Time Aggregates Itai Zukerman <zukerman@math-hat.com>
@ 2000-08-02 15:27 ` Thomas Lockhart <lockhart@alumni.caltech.edu>
  2000-08-02 15:37   ` Re: Time Aggregates David Lloyd-Jones <david.lloyd-jones@attcanada.ca>
  2000-08-02 15:52   ` Re: Time Aggregates Itai Zukerman <zukerman@math-hat.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Thomas Lockhart @ 2000-08-02 15:27 UTC (permalink / raw)
  To: Itai Zukerman <zukerman@math-hat.com>; +Cc: pgsql-sql

> I'm currently doing this:
>   SELECT symbol, date_trunc('minute', posted),
>          min(price), max(price), avg(price)
>   FROM trade
>   GROUP BY symbol, date_trunc('minute', posted);
> to get a list of minute-averages of trade prices.  I get the feeling
> that this is bad form, that I should be doing this some other way.  Is
> that the case?

Looks OK to me. If you are doing this *a lot* (i.e. many more queries
than inserts), then you might want to set up another column which
contains date_trunc('minute',posted) to avoid the calculation. Something
like

  create table trade (
    symbol text,
    posted timestamp,
    price integer,
    mpost  timestamp
  )

then define a rule to update mpost when posted gets set (haven't done
that part).

                    - Thomas



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Time Aggregates
  2000-08-02 13:41 Time Aggregates Itai Zukerman <zukerman@math-hat.com>
  2000-08-02 15:27 ` Re: Time Aggregates Thomas Lockhart <lockhart@alumni.caltech.edu>
@ 2000-08-02 15:37   ` David Lloyd-Jones <david.lloyd-jones@attcanada.ca>
  1 sibling, 0 replies; 4+ messages in thread

From: David Lloyd-Jones @ 2000-08-02 15:37 UTC (permalink / raw)
  To: Thomas Lockhart <lockhart@alumni.caltech.edu>; Itai Zukerman <zukerman@math-hat.com>; +Cc: pgsql-sql

"Thomas Lockhart" <lockhart@alumni.caltech.edu> replied to:
"Itai Zukerman" <zukerman@math-hat.com>

> > I'm currently doing this:
> >   SELECT symbol, date_trunc('minute', posted),
> >          min(price), max(price), avg(price)
> >   FROM trade
> >   GROUP BY symbol, date_trunc('minute', posted);
> > to get a list of minute-averages of trade prices.  I get the feeling
> > that this is bad form, that I should be doing this some other way.  Is
> > that the case?
>
> Looks OK to me. If you are doing this *a lot* (i.e. many more queries
> than inserts), then you might want to set up another column which
> contains date_trunc('minute',posted) to avoid the calculation. Something
> like
>
>   create table trade (
>     symbol text,
>     posted timestamp,
>     price integer,
>     mpost  timestamp
>   )
>
> then define a rule to update mpost when posted gets set (haven't done
> that part).

Tom,

I want to have two inputs to a table -- 5 widgets at 6 dollars, nextline,7
widgets at 45 dollars, nextline 1 widget at 4...sort of thing -- and output
to screen the $30, $315, $4 with those last three numbers also going back
into the database.

Is there any way of doing this withing the SQL, or does it have to be
written in a scipt external to the database itself?

                                                  Cheers,

                                                             -dlj.








^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Time Aggregates
  2000-08-02 13:41 Time Aggregates Itai Zukerman <zukerman@math-hat.com>
  2000-08-02 15:27 ` Re: Time Aggregates Thomas Lockhart <lockhart@alumni.caltech.edu>
@ 2000-08-02 15:52   ` Itai Zukerman <zukerman@math-hat.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Itai Zukerman @ 2000-08-02 15:52 UTC (permalink / raw)
  To: pgsql-sql

> >   SELECT symbol, date_trunc('minute', posted),
> >          min(price), max(price), avg(price)
> >   FROM trade
> >   GROUP BY symbol, date_trunc('minute', posted);

Hmmm... I'm not sure how to go about doing this for, say, 5 minute
intervals.  Basically, I want a function:

  date_round( timestamp, timespan ) --> timestamp

that will round the timestamp to the "nearest" timespan interval,
counting back from, say, the current time.

  date_round( '1/1/99 15:21', '5 minutes' ) --> '1/1/99 15:20'

if the current time is, say, 12:00.  As I see it, this involves

 1. calculating the interval between now and the timestamp;
 2. rounding that interval to the nearest multiple of the supplied interval;
 3. adding the rounded interval to the current time.

It's step 2 that I can't quite figure out.  I'm thinking about using
date_part( 'epoch', ... ) and some arithmetic.  Will that be OK?  Has
anyone done this before?

-itai

PS.  I'll also be happy if the function is called 'date_trunc' :)

PPS. I expect this query to be called much less often than inserts to
the table.



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2000-08-02 15:52 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-08-02 13:41 Time Aggregates Itai Zukerman <zukerman@math-hat.com>
2000-08-02 15:27 ` Thomas Lockhart <lockhart@alumni.caltech.edu>
2000-08-02 15:37   ` David Lloyd-Jones <david.lloyd-jones@attcanada.ca>
2000-08-02 15:52   ` Itai Zukerman <zukerman@math-hat.com>

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