To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Part 10 of the Visual Basic .NET 2010 Express Tutorial Complete!
How to Use the Visual Studio Code Analysis Tool FxCop
Article :: Interview with Andrei Alexandrescu (Part 3 of 3)
Introducing Visual Studio LightSwitch
Visual Studio LightSwitch Beta 1 is Available



Go Back   VBForums > Visual Basic > Database Development

Reply Post New Thread
 
Thread Tools Display Modes
Old May 18th, 2004, 07:36 AM   #1
BenFinkel
Hyperactive Member
 
Join Date: Jun 01
Location: Buffalo, NY
Posts: 296
BenFinkel is an unknown quantity at this point (<10)
SQL:Using TOP in conjunction with GROUP BY

Hey Guys,

I've got a question about some SQL I'm trying to write. What I've got is a table of Contacts, each of whom has a Sales figure associated with them, along with a territory.

I'd like an SQL statement to select the the top 5 sales figures for each territory.

In other words, I'd like to see the following result:

Code:
GEORGIA:Joe:5000
GEORGIA:Sam:4000
GEORGIA:Alex:3800
GEORGIA:Tim:3700
GEORGIA:Beth:3200
NEW YORK:Mabel:5100
NEW YORK:Terry:4850
NEW YORK:Jimbo:4100
NEW YORK:Leann:3600
NEW YORK:Mary:3500
IOWA:Jerry:4610
IOWA:Frank:4500
IOWA:Sally:3900
IOWA:Nancy:3650
IOWA:Greg:3110
...
From a table that is structured exactly the same, but has 150 contacts in each territory.

Any ideas?

Thanks!
BenFinkel is offline   Reply With Quote
Old May 18th, 2004, 08:38 AM   #2
Ecniv
Don't Panic!
 
Ecniv's Avatar
 
Join Date: Nov 00
Location: Excel Hell!
Posts: 4,756
Ecniv has a spectacular aura about (150+)Ecniv has a spectacular aura about (150+)
Code:
SELECT qryTerSales.SumOfSales, qryTerSales.TerritoryID, qryTerSales.TerNameID, tblTerritories.Territory, tblTerName.TerName

FROM (
(SELECT top 5 tblTerSales.TerritoryID, tblTerSales.TerNameID, Sum(tblTerSales.Sales) AS SumOfSales
FROM tblTerSales
GROUP BY tblTerSales.TerritoryID, tblTerSales.TerNameID
order by Sum(tblTerSales.Sales)
) AS qryTerSales 
LEFT JOIN tblTerName ON qryTerSales.TerNameID = tblTerName.TerNameID) 
LEFT JOIN tblTerritories ON qryTerSales.TerritoryID = tblTerritories.TerritoryID

GROUP BY qryTerSales.SumOfSales, qryTerSales.TerritoryID, qryTerSales.TerNameID, tblTerritories.Territory, tblTerName.TerName
ORDER BY tblTerritories.Territory;
Something like that (made up of relative tables...


Vince
__________________

BOFH Now, BOFH Past, Information on duplicates

Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
Ecniv is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Database Development


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:15 AM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.