Multiple sums in one query
I have a table 'table1' with fields, 'key' and 'number' There is another table 'table2' with fields 'key1' and 'key2' 'table2.key1' corresponds to 'table1.key'. Now, I need to find, in one query, the sum of all 'table1.number' fields for a given 'table2.key2' AND the highest sum of 'table1.number' with same key, within the 'table2.key2' So I need something like SELECT sum(table1.number) as highest FROM table1, table2 WHERE table1.key=table2.key1 AND table2.key2=? GROUP BY table1.key ORDER BY highest DESC LIMIT 1 and SELECT sum(table1.number) as total FROM table1, table2 WHERE table1.key=table2.key2 AND table2.key2=? GROUP BY table2.key2 in one query. Is this even possible? I would greatly appreciate any help, thanks Aleksander Wang-Hansen _ MSN Messenger http://www.msn.no/messenger - Den korteste veien mellom deg og dine venner -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: Multiple sums in one query
Are you wanting to do this in one query? You could put an order by on your second query. The first row returned by it should be what you are after. -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: Multiple sums in one query
Sorry, I didn't look closely enough at your two queries. I don't believe that you will be able to get your desired results from one query. Besides it is usually better to execute 2 queries that are simplier than one that is very complex. Robert Rowe --- Aleksander Wang-Hansen [EMAIL PROTECTED] wrote: Yes, I want to do it in one query. And the second query I made should only return one row anyway. But I want both the value I select in my first query, and the value I select in my second query, from one query. Getting it from two isn't a problem, but I can't seem to figure out how to get it from only one query. Thanks Original Message Follows From: robert_rowe [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: Re: Multiple sums in one query Date: Wed, 7 Jan 2004 13:46:02 -0500 Are you wanting to do this in one query? You could put an order by on your second query. The first row returned by it should be what you are after. -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED] _ Last ned nye MSN Messenger 6.1 gratis http://www.msn.no/computing/messenger - Den korteste veien mellom deg og dine venner -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]