Re: replace null with 0 in subselect ?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
Cc: Albrecht Berger <berger1517(at)gmx(dot)ch>, pgsql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: replace null with 0 in subselect ?
Date: 2002-10-17 03:10:17
Message-ID: 29430.1034824217@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com> writes:
> Coalesce((select max(pos)+1 from tab2), 0)
> should work.

Small comment: it's probably noticeably faster to do
(select Coalesce(max(pos), 0) +1 from tab2)

I think that the former will result in two evaluations of the sub-select
in the typical case, because COALESCE(foo, bar) is only a macro for
CASE WHEN foo IS NOT NULL THEN foo ELSE bar END
and that computes foo twice when foo isn't null.

My version isn't perfectly efficient either, because it will run two
copies of the MAX() calculation inside the sub-select; but that's still
better than two sub-selects.

Sometime we should reimplement COALESCE as a primitive instead of a
macro for a CASE expression.

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Ludwig Lim 2002-10-17 07:26:50 Re: getting the current date
Previous Message Bruce Momjian 2002-10-17 02:01:32 Re: getting the current date