How to aggregates this data

From: John Summerfield <postgres(at)herakles(dot)homelinux(dot)org>
To: pgsql-sql(at)postgresql(dot)org
Subject: How to aggregates this data
Date: 2007-01-10 22:50:19
Message-ID: 45A56DAB.1030709@herakles.homelinux.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

<wince> Now I've figured why I don't get replies, I'm sending again.

Intro:
I have a table that contains much data like this:
SELECT distinct on (date) asx_code,date,open,high,low,close from
sales_summaries where asx_code = 'TLSCA' and date >= '2006-12-01' order
by date, time desc limit 10;
SELECT distinct on (date) asx_code,date,open,high,low,close,volume from
sales_summaries where asx_code = 'TLSCA' and date >= '2006-12-01' order
by date, time desc limit 10;
asx_code | date | open | high | low | close | volume
----------+------------+-------+-------+-------+-------+----------
TLSCA | 2006-12-01 | 2.330 | 2.340 | 2.300 | 2.300 | 29165222
TLSCA | 2006-12-04 | 2.310 | 2.320 | 2.280 | 2.300 | 14973667
TLSCA | 2006-12-05 | 2.310 | 2.320 | 2.300 | 2.320 | 23238175
TLSCA | 2006-12-06 | 2.320 | 2.400 | 2.320 | 2.380 | 58357650
TLSCA | 2006-12-07 | 2.380 | 2.450 | 2.380 | 2.450 | 64005594
TLSCA | 2006-12-08 | 2.450 | 2.520 | 2.450 | 2.510 | 82831560
TLSCA | 2006-12-11 | 2.500 | 2.550 | 2.500 | 2.540 | 38711749
TLSCA | 2006-12-12 | 2.560 | 2.620 | 2.560 | 2.610 | 61709325
TLSCA | 2006-12-13 | 2.600 | 2.600 | 2.540 | 2.590 | 39455386
TLSCA | 2006-12-14 | 2.600 | 2.650 | 2.600 | 2.620 | 30675340
(10 rows)

Some may guess (correctly) that this is price information from a stock
exchange. What I want to do is consolidate the data into weekly (for
starters) periods. Writing a Java (or other procedural language) program
to read row by row is fairly straightforward. What I want to do is to do
it entirely in SQL.

I've perused my book (Mastering SQL by Martin Gruber), the postgresql
docs (I have versions here on RHEL (Centos) 4, FC5,6, Debian Testing -
up to 8,1) and I don't see how to choose the entry for the first open
column, the last close. Max, min and average will do nicely for the
others, and converting the date to an interval then dividing by seven
seems to work nicely for creating my groups,

The results I expect from the above data are
TLSCA 2006-12-07 2.330 2.450 2.280 2.450 mumble
TLSCA 2006-12-14 2.450 2.650 2.450 2.620 mumble

The question is, "How can I do this in SQL?"

May the pedants ignore that these weeks don't run Monday to Friday:-)

fwiw I'm working on Linux; thus far everything's done in bash scripts
aided by friends such as lynx, grep awk and (Of course) psql.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Chad Wagner 2007-01-11 00:29:41 Re: How to aggregates this data
Previous Message Mark 2007-01-10 20:56:11 Requirement for PostgreSQL Database Developer