Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Select Max Sum

Author  Topic 

acko
Yak Posting Veteran

52 Posts

Posted - 2004-06-15 : 04:28:52
I want to get the maximum of sum with one select statement.
The database is NorhtWind and this is the query.

select orderid, sum(quantity)
from [order details]
where orderid between 11000 and 11005
group by orderid
order by 2 desc

I know that i can use this statement:
select Top 1 orderid, sum(quantity)
from [order details]
where orderid between 11000 and 11005
group by orderid
order by 2 desc

or to use temporery tables but i am interested is there any different way to get only one row with maximum sum per orderid.
thanks
Alex

Kristen
Test

22859 Posts

Posted - 2004-06-15 : 04:44:54
Whats wrong with your SELECT TOP 1 solution?

Kristen
Go to Top of Page

LarsG
Constraint Violating Yak Guru

284 Posts

Posted - 2004-06-15 : 07:08:19
[code]
select max(q)
from (select sum(quantity) as q
from [order details]
where orderid between 11000 and 11005
group by orderid) dt
[/code]
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-06-15 : 08:27:07
I assumed, probably wrongly!, that acko wanted the "orderid" too

Kristen
Go to Top of Page

acko
Yak Posting Veteran

52 Posts

Posted - 2004-06-15 : 10:01:35
thanks LarsG
that is that i wanted
alex
Go to Top of Page
   

- Advertisement -