Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id EFC709FB553 for ; Fri, 2 Mar 2007 05:17:43 -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 96581-05 for ; Fri, 2 Mar 2007 05:17:40 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from floppy.pyrenet.fr (floppy.pyrenet.fr [194.116.145.2]) by postgresql.org (Postfix) with ESMTP id 1A99B9FB54C for ; Fri, 2 Mar 2007 05:17:43 -0400 (AST) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id 01173308E6; Fri, 2 Mar 2007 10:17:41 +0100 (MET) From: "Lars Gregersen" X-Newsgroups: pgsql.sql Subject: Select when table have missing data Date: 2 Mar 2007 01:17:33 -0800 Organization: http://groups.google.com Lines: 35 Message-ID: <1172827053.569467.79430@p10g2000cwp.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Complaints-To: groups-abuse@google.com User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322),gzip(gfe),gzip(gfe) X-HTTP-Via: 1.0 ADMPROXY Complaints-To: groups-abuse@google.com Injection-Info: p10g2000cwp.googlegroups.com; posting-host=129.142.27.194; posting-account=CSyMdA0AAAAkaCZeSz9yTDy4g-ktTFNr To: pgsql-sql@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200703/92 X-Sequence-Number: 27730 I have a table that contains historical exchange rates: date_time | timestamp xrate | real There is a maximum of one entry per day, but data are missing on weekends and holidays. For these missing dates I must use the value from the previous day (e.g. since data for a Sunday is missing I must use the value from the Friday just before the weekend). I have two questions: 1) Since historical exchange rates are not supposed to change I thought about creating a new table with data for all the missing dates (calculated using some procedural language). However, I would be much happier if there was a way to do this using SQL in a SELECT statement. Do you have any hints for this? 2) I have a number of other tables where data may be missing for different reasons. These data may be on a daily or an hourly basis. When a user selects a range of data from e.g. the 1st of January to the 1st of February I would like to be able to return a full set of data where all missing entries are returned as NULL. Is there a smart way to do this using SQL? Any hints or references you may have on the subject of handling missing data in time series data are very welcome. If there is a smarter way to set up tables for handling this type of data then please enlighten me. Thanks Lars