| От | Tom Lane |
|---|---|
| Тема | Re: replace null with 0 in subselect ? |
| Дата | |
| Msg-id | 29430.1034824217@sss.pgh.pa.us обсуждение |
| Ответ на | Re: replace null with 0 in subselect ? (Stephan Szabo <sszabo@megazone23.bigpanda.com>) |
| Список | pgsql-sql |
Stephan Szabo <sszabo@megazone23.bigpanda.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 forCASE 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
В списке pgsql-sql по дате отправления:
Сайт использует файлы cookie для корректной работы и повышения удобства. Нажимая кнопку «Принять» или продолжая пользоваться сайтом, вы соглашаетесь на их использование в соответствии с Политикой в отношении обработки cookie ООО «ППГ», в том числе на передачу данных из файлов cookie сторонним статистическим и рекламным службам. Вы можете управлять настройками cookie через параметры вашего браузера