Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 8A2B09FB1D9 for ; Wed, 10 Jan 2007 19:26:13 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 31585-03 for ; Wed, 10 Jan 2007 19:25:50 -0400 (AST) X-Greylist: delayed 00:32:30.907041 by SQLgrey-1.7.4 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id CE5CB9FB22E for ; Wed, 10 Jan 2007 19:26:00 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from www.herakles.homelinux.org (coco2.arach.net.au [203.34.16.107]) by svr4.postgresql.org (Postfix) with ESMTP id B057A5AF919 for ; Wed, 10 Jan 2007 22:53:27 +0000 (GMT) Received: from mail.herakles.homelinux.org (unknown [192.168.7.254]) by www.herakles.homelinux.org (Postfix) with ESMTP id E43585F5207 for ; Thu, 11 Jan 2007 07:53:10 +0900 (WST) Received: from mail2.cds.merseine.nu (unknown [192.168.9.4]) by mail.herakles.homelinux.org (Postfix) with ESMTP id CA1A51F767 for ; Thu, 11 Jan 2007 07:53:26 +0900 (WST) Received: from [192.168.9.146] (Bilby.demo.lan [192.168.9.146]) by mail2.cds.merseine.nu (Postfix) with ESMTP id 1EF30CAC001 for ; Thu, 11 Jan 2007 07:53:04 +0900 (WST) Message-ID: <45A56DAB.1030709@herakles.homelinux.org> Date: Thu, 11 Jan 2007 07:50:19 +0900 From: John Summerfield User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.7) Gecko/20061007 Red Hat/1.0.5-0.1.el4 SeaMonkey/1.0.5 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: How to aggregates this data Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.136 tagged_above=0 required=5 tests=BAYES_50, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200701/26 X-Sequence-Number: 27178 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.