pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bill Cunningham <billc@ballydev.com>
To: Bob Smith, Hammett & Edison, Inc. <bsmith@h-e.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: Odd behavior with NULL value
Date: Thu, 20 Dec 2001 21:20:56 -0800
Message-ID: <3C22C6B8.3020806@ballydev.com> (raw)
References: <000001411913091751151@h-e.com>

I apologize if this is sent twice.


The simplest solution would be to use a view over the data hiding the 
mess of the case statement so that ad-hoc users' queries would still work.

- Bill

Bob Smith, Hammett & Edison, Inc. wrote:

>On Thu, Dec 20, 2001, 18:55:18 Tom Lane wrote:
>
>>bsmith@h-e.com (Bob Smith, Hammett & Edison, Inc.) writes:
>>
>>>On a related note, does anyone know if 'current' works with DATE?
>>>
>>DATE does not have an internal representation of 'current', so the DATE
>>input parser treats it the same as 'now'.  AFAIR only timestamp (nee
>>datetime) has that concept.
>>
>>FYI, the concept of 'current' has been deemed broken and removed
>>altogether for 7.2.  See discussions in the pgsql-hackers archives
>>if you want to know why.
>>
>>			regards, tom lane
>>
>
>Here is the problem I'm trying to solve, perhaps someone can help.  For an
>invoicing system database, I have a table that defines employees.  Each has a
>begin and end date defining the employment period.  For current employees, the
>end date is "open".  How do I express that to keep queries as simple as
>possible?  The three choices I came up with are:
>
>  (1)  Use end_date = 'current'
>
>  (2)  Store end_date as TEXT and cast it to DATE in expressions, so it can
>       contain the text constant 'today' for current employees
>
>  (3)  Use end_date = NULL and have an extra expression in the queries
>
>Because 'current' doesn't work for DATE types, (1) is a bust.  (2) and (3)
>both work, but I'm not sure which is better from a performance point of view.
> For example, if I want all the employees that are current as of a particular
>date, for (2) it would be:
>
>  SELECT * FROM employee WHERE (foo >= employee.start_date) AND
>                               (foo <= employee.end_date::DATE)
>
>and for (3) it would be:
>
>  SELECT... WHERE (foo >= employee.start_date) AND
>                  ((foo <= employee.end_date) OR (employee.end_date IS NULL))
>
>(Thanks to all who posted with explanations of why (x IS NULL) should be used
>instead of (x = NULL)).  The cast has a performance penalty, but then so does
>using OR, especially in a join.  Which would be worse?
>
>I just noticed that (3) does have one advantage over (2); if the system allows
>end_date to be set into the future, (3) works for dates in the future, but (2)
>does not.  But that isn't one of my requirements so it isn't a deciding
>factor.
>
>Any opinions on which approach is better, or does anyone see a fourth
>alternative?
>
>Thanks!
>
>
>   |\      _,,,---,,_        Bob Smith
>   /,`.-'`'    -.  ;-;;,_    Hammett & Edison, Inc.
>  |,4-  ) )-,_. ,\ (  `'-'   bsmith@h-e.com
> '---''(_/--'  `-'\_)       
>
>---------------------------(end of broadcast)---------------------------
>TIP 2: you can get off all lists at once with the unregister command
>    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>





view thread (8+ messages)

Message-ID: <3C22C6B8.3020806@ballydev.com>
Permalink:  ../3C22C6B8.3020806@ballydev.com/
Also on:    postgresql.org/message-id/3C22C6B8.3020806@ballydev.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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: billc@ballydev.com, bsmith@h-e.com
  Subject: Re: Odd behavior with NULL value
  In-Reply-To: <3C22C6B8.3020806@ballydev.com>
  References: <000001411913091751151@h-e.com> <3C22C6B8.3020806@ballydev.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