pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Don Drake <dondrake@gmail.com>
To: Thomas F. O'Connell <tfo@sitening.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: plpgsql functions and NULLs
Date: Mon, 31 Jan 2005 15:06:34 -0600
Message-ID: <6c21003b0501311306c5f944@mail.gmail.com> (raw)
In-Reply-To: <9719e3dd9d6201fc40efcf3a15b82442@sitening.com>
References: <6c21003b0501301141234c256b@mail.gmail.com>
	<9719e3dd9d6201fc40efcf3a15b82442@sitening.com>

My outer query to get the candidates has an outer join, that works
just fine and I get the null OMCR_ID's.

It's when I have to query the dimension table (no joins) to see if a
row exists with a (sometimes) null OMCR_ID I'm forced to write 2
queries, when I think I should only have to write one.

Thanks.

-Don


On Mon, 31 Jan 2005 14:25:03 -0600, Thomas F. O'Connell
<tfo@sitening.com> wrote:
> This sounds like a perfect candidate for a LEFT OUTER JOIN. See:
> 
> http://www.postgresql.org/docs/7.4/static/queries-table-
> expressions.html#QUERIES-FROM
> 
> Yours would looks something like:
> 
> SELECT *
> FROM ...
> LEFT JOIN candidate AS c
> ON <...>.omcr_id = c.omcr_id
> AND ...
> 
> -tfo
> 
> --
> Thomas F. O'Connell
> Co-Founder, Information Architect
> Sitening, LLC
> http://www.sitening.com/
> 110 30th Avenue North, Suite 6
> Nashville, TN 37203-6320
> 615-260-0005
> 
> On Jan 30, 2005, at 1:41 PM, Don Drake wrote:
> 
> > OK, I have a function that finds records that changed in a set of
> > tables and attempts to insert them into a data warehouse.
> >
> > There's a large outer loop of candidate rows and I inspect them to see
> > if the values really changed before inserting.
> >
> > My problem is that when I look to see if the row exists in the
> > warehouse already, based on some IDs, it fails when an ID is NULL.
> > The ID is nullable, so that's not a problem.
> >
> > But I'm forced to write an IF statement looking for the potential NULL
> > and write 2 queries:
> >
> > IF omcr_id is null
> >    select * from ....
> >    WHERE omcr_id is NULL
> >    AND ...
> > ELSE
> >    select * from ....
> >    WHERE omcr_id=candidate.omcr_id
> >    AND ....
> > END IF;
> >
> > IF FOUND
> > ...
> >
> > Is there a way to do the lookup in one statement?? This could get ugly
> > quick.  I'm using v7.4.
> >
> > Thanks.
> >
> > -Don
> >
> > --
> > Donald Drake
> > President
> > Drake Consulting
> > http://www.drakeconsult.com/
> > 312-560-1574
> >
> > ---------------------------(end of
> > broadcast)---------------------------
> > TIP 4: Don't 'kill -9' the postmaster
> 
> 


-- 
Donald Drake
President
Drake Consulting
http://www.drakeconsult.com/
312-560-1574



view thread (9+ messages)  latest in thread

Message-ID: <6c21003b0501311306c5f944@mail.gmail.com>
Permalink:  ../6c21003b0501311306c5f944@mail.gmail.com/
Also on:    postgresql.org/message-id/6c21003b0501311306c5f944@mail.gmail.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: dondrake@gmail.com, tfo@sitening.com
  Subject: Re: plpgsql functions and NULLs
  In-Reply-To: <6c21003b0501311306c5f944@mail.gmail.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