Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language 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 November 15th, 2009, 01:24 PM
Authorized User
 
Join Date: Jul 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Max value of a groupby query

Hi,

I have this table named Sales with this data


Table: Sales
------------------
Salesman Amount
--------------------
Ben 1
Steve 2
Ben 3

then i do this query

select Salesman, sum(amount) as Total
from Sales
group by Salesman

the results are

---------------------
Salesman Amount
---------------------

Ben 4
Steve 2


The query i want is one that gives me the maximum amount of sales (4).

I have tried this, but it is wrong


select max(Total) from
(select Salesman, sum(amount) as Total
from Sales
group by Salesman)


I am using microsoft SQL Server 2005

thanks in advance!
 
Old November 17th, 2009, 03:02 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Try this:

select query1.* from
(SELECT Salesman, Sum(Amount) AS total_amt
FROM Sales
GROUP BY salesman) query1,

(select max(query2.total_amt) as highest_amt
from (SELECT Salesman, Sum(Amount) AS total_amt
FROM Sales
GROUP BY Salesman) query2) query3
where query1.total_amt = query3.highest_amt;
 
Old November 17th, 2009, 09:26 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Ummm...why not simply:
Code:
SELECT TOP 1 Salesman, sum(amount) as Total
from Sales
group by Salesman
ORDER BY sum(amount) DESC
?????
Why work harder than that?
 
Old November 17th, 2009, 10:48 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

That was my first thought. But what happens when the the amount is equal for two different salesmen?
 
Old November 18th, 2009, 05:00 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

He's using SQL Server, so he could get all of them via
Code:
SELECT TOP 1 WITH TIES Salesman, sum(amount) as Total
from Sales
group by Salesman
ORDER BY sum(amount) DESC
http://msdn.microsoft.com/en-us/libr...3(SQL.90).aspx
 
Old November 18th, 2009, 05:04 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Yes. Excellent!

Must be the water up there.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Groupby Reports in Data Report - Urgent Kaustav VB How-To 0 October 26th, 2005 06:33 AM
MAX Query zaeem Classic ASP Databases 1 April 12th, 2005 03:16 AM
Problem using query to obtain max mth hayley Classic ASP Databases 2 January 24th, 2005 10:27 PM
Query String Too Long....Max Length Error Message phungleon Classic ASP Databases 14 May 28th, 2004 12:25 PM
how to select Max hosefo81 PHP Databases 0 November 21st, 2003 12:58 AM





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