X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id CEDC2D1B54D for ; Thu, 9 Oct 2003 16:59:45 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 70785-03 for ; Thu, 9 Oct 2003 13:58:56 -0300 (ADT) Received: from mail.gis.priv (unknown [62.157.206.2]) by svr1.postgresql.org (Postfix) with ESMTP id EF4A9D1B4F6 for ; Thu, 9 Oct 2003 13:58:43 -0300 (ADT) Received: from yahoo.de (ip-58.gis.priv [192.168.1.58]) by mail.gis.priv (8.11.6/8.11.6) with ESMTP id h99GwaE05530; Thu, 9 Oct 2003 18:58:36 +0200 Message-ID: <3F8593C2.4080809@yahoo.de> Date: Thu, 09 Oct 2003 18:58:42 +0200 From: Janko Richter User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5b) Gecko/20030903 Thunderbird/0.2 X-Accept-Language: en-us, en MIME-Version: 1.0 To: colorob@tin.it, pgsql-sql@postgresql.org Subject: Re: select References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/155 X-Sequence-Number: 15555 roberto wrote: > Dear friends, > I have this table > > table work{ > day date, > hour integer, > } > > select * from work; > > date | text > ----------- > 1-1-2003 1 > 1-1-2003 1 > 2-1-2003 5 > 3-1-2003 10 > 5-1-2003 15 > > how can i obtain this? > > date | text > ----------- > 1-1-2003 2 > 2-1-2003 5 > 3-1-2003 10 > 4-1-2003 null > 5-1-2003 15 > 6-1-2003 null > > First , you need a sequence of days. Just create a function like this: CREATE OR REPLACE FUNCTION dateseq ( DATE , DATE ) RETURNS SETOF DATE LANGUAGE 'plpgsql' AS ' DECLARE bdate ALIAS FOR $1 ; edate ALIAS FOR $2 ; cdate DATE ; BEGIN cdate := bdate; WHILE cdate <= edate LOOP RETURN NEXT cdate ; cdate := CAST ( cdate + interval ''1 day'' AS date ); END LOOP; RETURN; END; '; The function is like a table/view , where the fist function argument is the start date , the second argument is the end date. Now try : SELECT ds.day, sum(w.hour) FROM dateseq( '2003-1-1', '2003-1-6' ) AS ds (day) LEFT JOIN work w ON ds.day=w.day GROUP BY ds.day; Regards, Janko -- Janko Richter