Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 821399FC0DE for ; Thu, 8 Mar 2007 21:30:38 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 71171-06 for ; Thu, 8 Mar 2007 21:30:28 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.175]) by postgresql.org (Postfix) with ESMTP id CBED39FC151 for ; Thu, 8 Mar 2007 21:30:28 -0400 (AST) Received: by ug-out-1314.google.com with SMTP id k3so1085938ugf for ; Thu, 08 Mar 2007 17:30:27 -0800 (PST) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=MGDUaBAObiI2TUHz+sTHsdGfvhm8ChW/hJVGkcBlilXtd6dmc22LxTVXL0b9Ph1QQYiUHahTOFNDB7+G26+4M1znOxnmR2AulPPqlPYoELLlGQ4xiVroXxLfzS5+qLl59WI5hvSrmZHXpm0r6vhJ0CCkvXFdAylIH7aqumy1A+E= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=bj6ihSjWlcLw+8d2/DJYf1leU/7pBxq0n678jZxIlQymeXz8oboeBLghTf8hJOboRyErcJ3/8cvWE5ScwL198LmtYC9Gg/Z6DoyPjP1xlvM/WdkHoAe2+PPg1z7wc9KrC5LmKN1pXceVtP5/fEejW9JY8LrUrHssHRzrRSN2/ug= Received: by 10.115.58.1 with SMTP id l1mr330295wak.1173403826228; Thu, 08 Mar 2007 17:30:26 -0800 (PST) Received: by 10.114.166.8 with HTTP; Thu, 8 Mar 2007 17:30:26 -0800 (PST) Message-ID: Date: Thu, 8 Mar 2007 20:30:26 -0500 From: "=?ISO-8859-1?Q?Rodrigo_De_Le=F3n?=" To: pgsql-sql@postgresql.org Subject: Re: Select when table have missing data In-Reply-To: <1172827053.569467.79430@p10g2000cwp.googlegroups.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <1172827053.569467.79430@p10g2000cwp.googlegroups.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200703/94 X-Sequence-Number: 27732 On 2 Mar 2007 01:17:33 -0800, Lars Gregersen wrote: > 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 generate_series() is your friend: ------------------------------ create table t( date_time timestamp , xrate real ); insert into t values ('2007-3-7',0.23); insert into t values ('2007-3-8',0.1); insert into t values ('2007-3-9',0.2); -- no '2007-3-10' -- no '2007-3-11' insert into t values ('2007-3-12',0.3); insert into t values ('2007-3-13',0.4); -- no '2007-3-14' insert into t values ('2007-3-15',0.99); -- no '2007-3-16' select d.*, ( select xrate from t where date_time = ( select max(date_time) from t where date_time <= d.ddate ) ) as xrate from ( select ('2007-3-7'::date+s.x)::timestamp as ddate from generate_series(0,9) s(x) ) d ------------------------------ Extrapolate for case 2. Regards.