Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

returning the maximum value from a SUM query

Status
Not open for further replies.

james0816

Programmer
Jan 9, 2003
295
US
should be an easy ? for someone out here (not me obviously)

current query:

select sum(b) from db where dc=1 group by ba

returns:
510
764

I would like to just capture the MAX of this query.

possible?
 
select max(sum(b)) from db where dc=1 group by ba" ???


Water is not bad as long as it remains outside human body ;-)
 
tried that...get a message saying "Invalid use of group function".
 
what about "select max(select sum(b) from db where dc=1 group by ba) from dual" ?
... where dual is a virtual table name used with Oracle... don't know if it exist in MySql

Water is not bad as long as it remains outside human body ;-)
 
not familiar with dual...but it didnt' work any way.

i have found a work around but i know there has to be an easier way. what i have currently is my query sorted on that field and then putting that data as a variable.

then...re-write the query to sort on the next field and so on.

lot of coding doing it that way...ick!
 
after having asked here, it seems that "Dual" table is Oracle only. Did you try with an existing table name instead : ""select max(select sum(b) from db where dc=1 group by ba) from db" ?

Water is not bad as long as it remains outside human body ;-)
 
yep, one of the many different variations i have tried.
 
what is the error message ?

Water is not bad as long as it remains outside human body ;-)
 
it just tells me "you have an error in your sql syntax
 
Don't you have a command line type tool to test your requests that should give you a more interseting error. mySql SQL command line tool for example ?

Water is not bad as long as it remains outside human body ;-)
 
i am using mysql control center. here is the complete line returned:

[DataBase1] ERROR 1064: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'select sum(b) from db where dc=1 group by ba) from db
 
man i hate the thought of upgrading after all the work i put into this thing. is it a relatively simple process?
 
When I did that, it was quite simple : make a dump of your DB, install new version and then reimport the dump inside the new db.
Beware that protocols and connect strings may differ from one version and other.

By the way, got to go (6PM in France : work finished).
See you tomorow morning.

Water is not bad as long as it remains outside human body ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top