pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joel Stevenson <jstevenson@bepress.com>
To: pgsql-sql@postgresql.org
Subject: CTE or Subselect and outer joins not behaving as expected?
Date: Mon, 24 Oct 2011 17:46:42 -0700
Message-ID: <A3B9626E-9093-4E7C-9986-BA6597665F3C@bepress.com> (raw)

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  
------------
 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 = 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.

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 );




view thread (4+ messages)  latest in thread

Message-ID: <A3B9626E-9093-4E7C-9986-BA6597665F3C@bepress.com>
Permalink:  ../A3B9626E-9093-4E7C-9986-BA6597665F3C@bepress.com/
Also on:    postgresql.org/message-id/A3B9626E-9093-4E7C-9986-BA6597665F3C@bepress.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jstevenson@bepress.com
  Subject: Re: CTE or Subselect and outer joins not behaving as expected?
  In-Reply-To: <A3B9626E-9093-4E7C-9986-BA6597665F3C@bepress.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox