making a combined sum of two columns

Posted by bsandrabr on Stack Overflow See other posts from Stack Overflow or by bsandrabr
Published on 2010-03-12T21:29:31Z Indexed on 2010/03/12 21:37 UTC
Read the original article Hit count: 227

Filed under:
|
|

I have a table (apples) containing:

cid  date_am date_pm 
----------------------
1      1       1
2      2       1
3      1       3  
1      1       2

I asked a question earlier (badly) about how I would rank the customers in order of the number of ones(1) they had. The solution was (based on one column):

SELECT cid, sum( date_pm ) AS No_of_ones
FROM apples
WHERE date_am =1
GROUP BY cid
ORDER BY no_of_ones DESC 

This works great for one column but how would I do the same for the sum of the two columns. ie.

SELECT cid, sum( date_pm ) AS No_of_ones
FROM apples
WHERE date_am =1
add to
SELECT cid, sum( date_am ) AS No_of_ones
FROM apples
WHERE date_pm =1
GROUP by cid
ORDER by no_of_ones(added)

hope I've managed to make that clear enough for you to help -thanks

© Stack Overflow or respective owner

Related posts about sql

Related posts about sum