X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 1985F9DC896 for ; Sat, 18 Feb 2006 03:36:32 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 83664-04 for ; Sat, 18 Feb 2006 03:36:31 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from stark.xeocode.com (stark.xeocode.com [216.58.44.227]) by postgresql.org (Postfix) with ESMTP id 5D7389DC80E for ; Sat, 18 Feb 2006 03:36:28 -0400 (AST) Received: from localhost ([127.0.0.1] helo=stark.xeocode.com) by stark.xeocode.com with smtp (Exim 3.36 #1 (Debian)) id 1FAMdm-000616-00; Sat, 18 Feb 2006 02:36:26 -0500 To: "Ding Xiangguang" Cc: Subject: Re: view of weekly data References: <543CACD7FF1C4F4AB587CC0864E94EC603B5E31D@EMHQ05B.singtel.corp.root> In-Reply-To: <543CACD7FF1C4F4AB587CC0864E94EC603B5E31D@EMHQ05B.singtel.corp.root> From: Greg Stark Organization: The Emacs Conspiracy; member since 1992 Date: 18 Feb 2006 02:36:25 -0500 Message-ID: <87hd6x15l2.fsf@stark.xeocode.com> Lines: 55 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.4 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.131 required=5 tests=[AWL=0.131] X-Spam-Score: 0.131 X-Spam-Level: X-Archive-Number: 200602/232 X-Sequence-Number: 24242 "Ding Xiangguang" writes: > Hi, friend, > > Suppose there is table of daily transaction data with 5 fields, > > time(date), open(float8), high(float8), low(float8), close(float8) > > Is it possible to create a view of weekly data, i.e. open is the first > day'open, high is the highest of the week, low is the lowest of the week, close > is the last day's close. low and high are easy, they're just min() and max(). so you would get something like: select date_trunc('week', time) as startofweek, min(low) as weeklylow, max(high) as weeklyhigh from dailydata group by date_trunc('week', time) Unfortunately showing the open and close is much much harder. To get them efficiently requires a feature set called OLAP that Postgres doesn't have and isn't likely to get soon. In Postgres 8.1 (and 8.0?) you could actually write some custom aggregate functions using RECORD data type to store the earliest and latest time found so far and the corresponding open and close to get them efficiently. Maybe someone else would be able to show how to do that, I haven't tried it yet. The only way to do it in standardish SQL would be with terribly inefficient subqueries: select date_trunc('week', time) as startofweek, min(low) as weeklylow, max(high) as weeklyhigh, (select open from dailydata as d where date_trunc('week',time)=date_trunc('week',dailydata.time) order by time asc limit 1 ) as weeklyopen, (select close from dailydata as d where date_trunc('week',time)=date_trunc('week',dailydata.time) order by time desc limit 1 ) as weeklyclose from dailydata group by date_trunc('week', time) -- greg