Re: help in sql - postgresql

From: Date: Tue, 02 May 2006 08:46:33 +0000
Subject: Re: help in sql - postgresql
References: 1 2  Groups: php.db 
Request: Send a blank email to php-db+get-41800@lists.php.net to get a copy of this message
Hi, Thanks a lot, That exactly wat I need :) Suad Chris wrote:
suad wrote:
Hi, I need some help in sql - postgresql:
<snip> Yay a postgres question! :D hee hee
*The question is* : how can I force that the result of the col payed to be zerro "0" insted of nothing (NULL) and the order will be in way that the zerro's values comes first. and the result will be: sum | payed | to_pay -----+-------+--------
 25 |     0 |      0
150 |   150 |      0
175 |   150 |     25
COALESCE will do it for you: SELECT SUM(c_price) as sum,(SELECT COALESCE(SUM(d_price), 0) FROM d WHERE a_id=t1.a_id ) AS payed, SUM(c_price)-(SELECT COALESCE(SUM(d_price), 0) FROM d WHERE a_id=t1.a_id ) AS to_pay FROM c AS t1 group by a_id order by payed; sum | payed | to_pay -----+-------+--------
  25 |     0 |     25
 150 |   150 |      0
 175 |   150 |     25
(3 rows) http://www.postgresql.org/docs/8.1/interactive/functions-conditional.html


« previous php.db (#41800) next »