SELECT with many SUMs

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
joecrack
Forum Commoner
Posts: 99
Joined: Mon Oct 31, 2005 9:17 pm

SELECT with many SUMs

Post by joecrack »

Hai i need a SELECT like this:

Code: Select all

SELECT SUM(contramount) 'Bekommen' FROM sam_date_val WHERE projnr BETWEEN '1000' AND '5000'; SUM(tovalue) 'Genommen' FROM sam_date_val
Its working like this

Code: Select all

SELECT SUM(contramount) 'Bekommen' FROM sam_date_val WHERE projnr BETWEEN '1000' AND '5000'; SUM(tovalue) 'Genommen' FROM sam_date_val
and like this:

Code: Select all

SELECT SUM(contramount) 'Bekommen',SUM(tovalue) 'Genommen' FROM sam_date_val
BUT not like the first - - - so any suggestions???
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You can't return a single set of data from a two queries.

Code: Select all

SELECT 
SUM(contramount) 'Bekommen' as total
FROM sam_date_val 
WHERE projnr BETWEEN '1000' AND '5000'
UNION
SUM(tovalue) 'Genommen' as total
FROM 
sam_date_val
Use the UNION function to join them together.

Notes:

1. UNION is only available on MySQL 4.1 and above.
2. The two (or more) selects in the UNION must return exactly the same number of fields.
3. I've added " as total" after each SUM(). This aliases the result to an easy to use name.
joecrack
Forum Commoner
Posts: 99
Joined: Mon Oct 31, 2005 9:17 pm

Post by joecrack »

Thanks for the answer - but i DONT WANT a sigle data - sry i think i didnt say that.
I want to have many SUMs and then i want to put them into an Array!!!
Ithink this command cant be put in an Array!!!
Post Reply