Re: help in sql - postgresql
| From: | Chris | Date: | Tue, 02 May 2006 07:20:36 +0000 |
| Subject: | Re: help in sql - postgresql | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-41799@lists.php.net to get a copy of this message | ||
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 -----+-------+--------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 | 0 150 | 150 | 0 175 | 150 | 25
25 | 0 | 25 150 | 150 | 0 175 | 150 | 25(3 rows) http://www.postgresql.org/docs/8.1/interactive/functions-conditional.html -- Postgresql & php tutorials http://www.designmagick.com/