Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 15th, 2004, 03:30 AM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select Maximum of Sum

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
 
Old June 15th, 2004, 04:28 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 331
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to qazi_nomi
Default

You can check it that select orderid, sum(quantity)
from [order details]
where sum (quantity) is maximum and orderid between 11000 and 11005

group by orderid
order by 2 desc



Love 4 all
 
Old June 15th, 2004, 05:53 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

qazi_nomi,

That won't work, would result in this error, as aggregate functions cannot be used in where.

An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

Alex,
You can do it that way, as mentioned in your post. Otherwise, you can store them into TempTable and pick the max from there.

Hope that helps.
Cheers!

_________________________
-Vijay G
Strive for Perfection
 
Old June 17th, 2004, 11:08 PM
Authorized User
 
Join Date: Jun 2003
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i'm not clear about what you want to obtain: the first orderid with the maximun total quantity per orders (there could be more than one order with the same total quantity)? or are you trying to obtain the maximum total quantity per one order and a random orderid?

let's say you have the following data in the [Order Details] table:

OrderId Quantity
-----------------
1 5
1 1
2 20
3 5
3 15

what do you want to obtain for the above case?





Similar Threads
Thread Thread Starter Forum Replies Last Post
SELECT SUM Top x CassyFrost Classic ASP Basics 3 October 16th, 2007 08:38 AM
XSL:Variable SELECT=sum(condictions) andreracco XSLT 3 August 15th, 2007 08:51 AM
Help: Running Sum (or Cumulative Sum) timdasa VB Databases Basics 1 August 22nd, 2006 03:12 PM
Within a SELECT do 2 Sum and show only 1 Ford SQL Server 2000 2 November 8th, 2005 01:21 AM
SELECT SUM syntax... Anubis Access VBA 8 December 15th, 2003 08:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.