Page 1 of 1

SELECT with many SUMs

Posted: Wed Nov 30, 2005 2:07 am
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???

Posted: Wed Nov 30, 2005 3:56 am
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.

Posted: Wed Nov 30, 2005 7:33 pm
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!!!