X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 548379FA612 for ; Wed, 28 Jun 2006 13:19:49 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 92859-01 for ; Wed, 28 Jun 2006 13:19:43 -0300 (ADT) X-Greylist: delayed 00:18:53.18787 by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 98F939FB1BC for ; Wed, 28 Jun 2006 13:19:26 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from niflheim.msglab.com (unknown [195.245.173.24]) by svr4.postgresql.org (Postfix) with ESMTP id 8EE315AFAD2 for ; Wed, 28 Jun 2006 16:00:32 +0000 (GMT) Received: from m3ta.msglab.mine.nu (10001305514.0000043966.acesso.oni.pt [213.58.222.53]) by niflheim.msglab.com (Postfix) with ESMTP id E02222E025 for ; Wed, 28 Jun 2006 17:00:23 +0100 (WEST) Subject: generate_series with left join From: "Pedro B." Reply-To: pedro.borracha@msglab.com To: pgsql-sql@postgresql.org Content-Type: text/plain Organization: MsgLab AS Date: Wed, 28 Jun 2006 17:00:24 +0100 Message-Id: <1151510424.28638.18.camel@m3ta.msglab.mine.nu> Mime-Version: 1.0 X-Mailer: Evolution 2.6.0 Content-Transfer-Encoding: 7bit X-MsgLab-MailScanner-Information: Please contact the ISP for more information X-MsgLab-MailScanner: Found to be clean X-MsgLab-MailScanner-SpamCheck: not spam (whitelisted), SpamAssassin (score=0, required 5, autolearn=not spam) X-MsgLab-MailScanner-From: pedro.borracha@msglab.com X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/299 X-Sequence-Number: 25535 Greetings. I'm having some difficulties with my first use of the generate_series function. Situation: cause | integer date | timestamp(2) without time zone cause | date ------+------------+-----------+ 1 | 2006-03-23 15:07:53.63 | 2 | 2006-02-02 12:13:23.11 | 2 | 2006-11-12 16:43:11.45 | 1 | 2005-03-13 18:34:44.13 | 3 | 2006-01-23 11:24:41.31 | (etc) What i need to do, is to count the 'cause' column for the values '1' and '2', and group them by year, using left joins in order to also have the serialized years with empty values in the output. My needed output for a series of (2005,2007) would be: year | one | two ------+------+------ 2005 | 1 | 0 2006 | 1 | 2 2007 | 0 | 0 I have tried something like #select s, (select count(cause) from mytable where cause=1 ) as one, COUNT (cause) as two from generate_series(2006,2009) AS s(d) left JOIN mytable o ON (substr(o.date,1,4) = s.d and cause=2) GROUP BY s.d ORDER BY 1; which obviously is wrong, because of the results: s | one | two ------+------+------ 2006 | 3769 | 1658 2007 | 3769 | 0 2008 | 3769 | 0 2009 | 3769 | 0 As far as the 'two', the left join was successful, however i can not find a way to join the 'one'. The output value is correct, but the result shown should be only for the year 2006, not for all the values of the series. Maybe i've looked at it TOO much or maybe i'm completely failing to find a working logic. Any suggestions? Any and all help is humbly appreciated. \\pb -- This message has been scanned for viruses and dangerous content at MsgLab.com and is believed to be clean.