Re: SQL query

From: David Goodenough <david(dot)goodenough(at)btconnect(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: SQL query
Date: 2005-02-11 16:39:05
Message-ID: 200502111639.06071.david.goodenough@btconnect.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Friday 11 February 2005 11:31, Matt K wrote:
> David Goodenough wrote:
> >I could do this by doing a select * from addresses where customer = ?
> >and type = 'billing', looking to see if there is a result row and if not
> >repeating the query with type = 'default', but that seems inelegant to
> >me.
>
> Use NULL to indicate that the customer type is default. Then you can
> query with:
>
> select * from addresses where customer = ?
> and coalesce(type, 'billing') = 'billing'
>
> If type is NULL, the comparison will be 'billing' = 'billing' - always
> true. If there's a bunch of non-null type addresses, you'll get the
> 'billing' one.
>
> http://www.postgresql.org/docs/8.0/interactive/functions-conditional.html#A
>EN12003
>
> Matt
>
Well coalesce is not something I had come across, learn something every day.

But I can not use this as the type (with the customer) are the primary key and
therefore not null. I could do something like:-

coalesce( nullif( 'default', type), 'billing')

but I think that might be over egging it a bit. I will hope this one reserve
and remember coalesce for the future.

Thanks,

David

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David Goodenough 2005-02-11 16:39:59 Re: SQL query
Previous Message John Sidney-Woollett 2005-02-11 16:13:45 Re: vacuum confusion