Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id AE3FAB5DBEE for ; Mon, 24 Oct 2011 22:17:13 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 64442-04 for ; Tue, 25 Oct 2011 01:17:07 +0000 (UTC) X-Greylist: delayed 00:25:18.237899 by SQLgrey-1.8.0-rc2 Received: from mail-pz0-f44.google.com (mail-pz0-f44.google.com [209.85.210.44]) by mail.postgresql.org (Postfix) with ESMTP id E1D61B5DBF2 for ; Mon, 24 Oct 2011 22:17:06 -0300 (ADT) Received: by pzk4 with SMTP id 4so40940360pzk.3 for ; Mon, 24 Oct 2011 18:17:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bepress.com; s=google; h=from:content-type:content-transfer-encoding:date:subject:to :message-id:mime-version:x-mailer; bh=d9+y8/XRuVCn2Cqp4F5bm9KN/VFahqANq0IRsdfXBqI=; b=jCOBM9GdVyA5Ah+gEj+VqkXGn1FMZwidgvWmZhBuIxve1us5VIVnBzlZEk71tWLDaK G7kpQ2QFimjHgs3TdnKL/9eZXS9+Iqc/AVTO6gN7lD/Fe7EomvHiLt1XFk5dU+Y+Kwd+ +g7yEe+IPSJ8DZx+h43jHPqFQ19jMde1PGe6c= Received: by 10.68.29.101 with SMTP id j5mr51539107pbh.61.1319503604228; Mon, 24 Oct 2011 17:46:44 -0700 (PDT) Received: from [10.200.5.31] ([70.36.156.242]) by mx.google.com with ESMTPS id v8sm2627047pbf.8.2011.10.24.17.46.43 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 24 Oct 2011 17:46:43 -0700 (PDT) From: Joel Stevenson Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Date: Mon, 24 Oct 2011 17:46:42 -0700 Subject: CTE or Subselect and outer joins not behaving as expected? To: pgsql-sql@postgresql.org Message-Id: Mime-Version: 1.0 (Apple Message framework v1244.3) X-Mailer: Apple Mail (2.1244.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201110/64 X-Sequence-Number: 35948 Hi all, I'm trying to enumerate a list of months between a date in the = past and now and display a value from an existing table if there is one = for the date or NULL if there isn't. I'm using this SQL to generate the months: select distinct date_trunc( 'month', '2011-06-01'::date + tally_day = )::date as tally_mon from generate_series( 0, ( select current_date - '2011-06-01'::date ) ) = as tally_day [[ produces ]] tally_mon =20 ------------ 2011-06-01 2011-07-01 2011-08-01 2011-09-01 2011-10-01 (5 rows) and I am trying to use it as the left hand side of a left join against = my data table: select tally_table. tally_mon, met.datum from ( select distinct date_trunc( 'month', '2011-06-01'::date + tally_day = )::date as tally_mon from generate_series( 0, ( select current_date - '2011-06-01'::date ) = ) as tally_day ) as tally_table full outer join my_existing_table as met on( tally_mon = =3D met.month ) where met.series =3D 1; -- ** See SETUP below ** This produces rows only for those that exist in my_existing_table and no = left join output which I'd expect for the rest of the tally_table rows. What am I missing? Many thanks, Joel SETUP: create temp table my_existing_table ( month date not null, series int = not null, datum int not null ); insert into my_existing_table values ( '2011-08-01', 1, 5 ), ( = '2011-10-01', 1, 4 );