Page 1 of 1

what is this sql query?

Posted: Wed Sep 16, 2009 3:39 am
by m2babaey
Hi
I cannot figure out this query:

Code: Select all

SELECT symbol,SUM(IF(t_type='sell',-shares,shares)) AS shares,
                                                           MAX(price) AS maxprice, MIN(price) AS minprice
                                                           FROM transactions
                                                          WHERE completed=0
                                                          GROUP BY symbol
                                                          ORDER BY shares
My problem is "-shares"; what does it mean?
how about this: IF(t_type='sell',-shares,shares)
t_type and shares are table fields
Thanks for your help

Re: what is this sql query?

Posted: Wed Sep 16, 2009 3:47 am
by onion2k
IF(t_type='sell',-shares,shares) is an if statement in SQL. It's saying "if t_type is 'sell' return the number in the column `shares` as a negative, else return it as a positive". In PHP it'd be..

Code: Select all

if ($t_type=="sell") {
  return -1*$shares;
} else {
  return $shares;
}