Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 3DD01B5DCEB for ; Tue, 25 Oct 2011 09:42:54 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 09943-02 for ; Tue, 25 Oct 2011 12:42:48 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 01C77B5DC2D for ; Tue, 25 Oct 2011 09:42:47 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1RIgL8-0003lP-MI for pgsql-sql@postgresql.org; Tue, 25 Oct 2011 14:42:46 +0200 Received: from 212.126.222.122 ([212.126.222.122]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 25 Oct 2011 14:42:46 +0200 Received: from hari.fuchs by 212.126.222.122 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 25 Oct 2011 14:42:46 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Harald Fuchs Subject: Re: CTE or Subselect and outer joins not behaving as expected? Date: Tue, 25 Oct 2011 14:42:34 +0200 Lines: 21 Message-ID: <86zkgpi6o5.fsf@protecting.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 212.126.222.122 X-Archive: encrypt User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:zJ07LGrQSkhQCr6HIvCQTNZvJVA= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.229 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_NUMERIC_HELO=1.164, RP_MATCHES_RCVD=-0.504, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201110/76 X-Sequence-Number: 35960 In article , Joel Stevenson writes: > 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 = met.month ) > where met.series = 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. Your WHERE clause turns the OUTER into an INNER JOIN. Try 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 left join my_existing_table as met on tally_mon = met.month and met.series = 1;