T2terBKK
T2terBKK

Reputation: 319

How can i SELECT sum() many columns in 1 query?

Just Sample Table : Cus_fi.

enter image description here

Can i

SELECT SUM(dollar,euro,pound) FROM cus_fi ; in 1 query.

And Show Result by age range like this.

   20 - 25   1500    410     166
   26 - 30   5300    2000    584

Thank for help student really want to known.

Upvotes: 0

Views: 74

Answers (2)

Kuzgun
Kuzgun

Reputation: 4737

Try this one:

SELECT age, SUM(dollar) AS Total_Dollar, SUM(Euro) AS Total_Euro, 
SUM(Pound) AS Total_Pound 
    FROM cus_fi GROUP BY age

EDIT:

SELECT CONCAT(2*floor(age/2), '-', 2*floor(age/2) + 2) as `age_range`, SUM(dollar) AS Total_Dollar, SUM(Euro) AS Total_Euro, 
SUM(Pound) AS Total_Pound 
    FROM cus_fi GROUP BY 1 ORDER BY age

Upvotes: 1

MarcinJuraszek
MarcinJuraszek

Reputation: 125630

select
    round(age / 5) * 5 - 4,
    round(age / 5) * 5,
    SUM(dollar),
    SUM(euro)
FROM TEST
GROUP BY round(age / 5)

You can extend it with more columns. See DEMO: http://sqlfiddle.com/#!2/160bb/15

Upvotes: 3

Related Questions