Re: help in sql - postgresql

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux