473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

top x of group

I want to create a query that is limited to the top x (according to one field) of records that have the same data in another field.

For example, let's say I want to list the top 5 cities by population in each state. I have a database that covers thousands of U.S. cities and has these fields: State_Name, City_Name, Population. I make a query with these 3 fields and sort it in ascending order by State_Name and in descending order by Population. How do I make it show _only_ the 5 highest-population cities of each state?

This is probably very simple, but I thank anyone who can suggest an answer.
Mar 5 '07 #1
15 1803
MMcCarthy
14,534 Expert Mod 8TB
I want to create a query that is limited to the top x (according to one field) of records that have the same data in another field.

For example, let's say I want to list the top 5 cities by population in each state. I have a database that covers thousands of U.S. cities and has these fields: State_Name, City_Name, Population. I make a query with these 3 fields and sort it in ascending order by State_Name and in descending order by Population. How do I make it show _only_ the 5 highest-population cities of each state?

This is probably very simple, but I thank anyone who can suggest an answer.
Don't know if this will work but it's worth a try.

Expand|Select|Wrap|Line Numbers
  1. SELECT State_Name,
  2. (SELECT Top 5 City_Name 
  3. FROM TableName As T1 INNER JOIN T2 
  4. ON T1.State_Name=T2.StateName
  5. ORDER BY Population DESC)
  6. Population
  7. FROM TableName As T2;
  8.  
Mary
Mar 6 '07 #2
NeoPa
32,556 Expert Mod 16PB
I want to create a query that is limited to the top x (according to one field) of records that have the same data in another field.

For example, let's say I want to list the top 5 cities by population in each state. I have a database that covers thousands of U.S. cities and has these fields: State_Name, City_Name, Population. I make a query with these 3 fields and sort it in ascending order by State_Name and in descending order by Population. How do I make it show _only_ the 5 highest-population cities of each state?

This is probably very simple, but I thank anyone who can suggest an answer.
It's not so much simple as theoretically impossible.
Maybe I overstate the case, as I often find that people can come up with 'out-of-the-box' solutions that seem to break the established rules, but I can see no possible way of implementing this I'm afraid.
Mar 8 '07 #3
ADezii
8,834 Expert 8TB
It's not so much simple as theoretically impossible.
Maybe I overstate the case, as I often find that people can come up with 'out-of-the-box' solutions that seem to break the established rules, but I can see no possible way of implementing this I'm afraid.
I think I may be able to attain a solution via VBA code with the end results (5 highest cities population wise by State) being written to a Table. Do you think it is worth the effort, or just a waste of time? I'm truly interested in your opinion because I've played with it myself and could not arrive at a solution via the SQL Route.
Mar 8 '07 #4
NeoPa
32,556 Expert Mod 16PB
I expect you can ADezii (knowing how familiar you are with the coding side of things).
I should have been clearer in my earlier comment though :
"This is (I think) impossible to do in a query (SQL) alone."
Remember though, any TOP 5 query executed within this code must be executed for each TOP 5 set required. It will not work if generic SQL is used and simply processed through again and again.
Mar 8 '07 #5
ADezii
8,834 Expert 8TB
I expect you can ADezii (knowing how familiar you are with the coding side of things).
I should have been clearer in my earlier comment though :
"This is (I think) impossible to do in a query (SQL) alone."
Remember though, any TOP 5 query executed within this code must be executed for each TOP 5 set required. It will not work if generic SQL is used and simply processed through again and again.
Thanks NeoPa:
Don't think it will be worth the effort to generate the code simply to see if it 'can be done'. It's not like anyone will actually use it.
Mar 8 '07 #6
NeoPa
32,556 Expert Mod 16PB
The OP's gone AWOL you mean. Guess it wasn't such a pressing need ;)
You can always do it if they come back and respond with a request any time. I wouldn't waste your time otherwise though.
Mar 9 '07 #7
ADezii
8,834 Expert 8TB
The OP's gone AWOL you mean. Guess it wasn't such a pressing need ;)
You can always do it if they come back and respond with a request any time. I wouldn't waste your time otherwise though.
Advice well taken
Mar 9 '07 #8
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. SELECT Temp.State_Name, Temp.City_Name, Temp.Population
  2. FROM Table1 AS Temp
  3. WHERE City_Name In(SELECT TOP 5 City_Name FROM Table1 WHERE State_Name = Temp.State_Name ORDER BY Population DESC;);
Just replace Table1 with the name of your table.
Mar 9 '07 #9
NeoPa
32,556 Expert Mod 16PB
Lucky I added the (I think) bit in there.
Nice one Rabbit. I thought of this thread when I saw your post in the other thread (Adding a Rank column to a report).
This is the solution I wish I'd come up with ;)
Mar 9 '07 #10
Rabbit
12,516 Expert Mod 8TB
That's exactly where I got it from too. I had to play around with it a bit putting it in different places in the SQL till I found one that worked
Mar 9 '07 #11
MMcCarthy
14,534 Expert Mod 8TB
Lucky I added the (I think) bit in there.
Nice one Rabbit. I thought of this thread when I saw your post in the other thread (Adding a Rank column to a report).
This is the solution I wish I'd come up with ;)
Me too!

Great work Rabbit.

Mary
Mar 9 '07 #12
ADezii
8,834 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. SELECT Temp.State_Name, Temp.City_Name, Temp.Population
  2. FROM Table1 AS Temp
  3. WHERE City_Name In(SELECT TOP 5 City_Name FROM Table1 WHERE State_Name = Temp.State_Name ORDER BY Population DESC;);
Just replace Table1 with the name of your table.
You rascally Rabbit! Nice job.
Mar 9 '07 #13
Rabbit
12,516 Expert Mod 8TB
Thanks. Quite proud of it myself lol.
Mar 9 '07 #14
Hi, I'm the OP. Thanks for the helpful ideas, everyone!
Apr 18 '07 #15
Rabbit
12,516 Expert Mod 8TB
Not a problem. Good to hear from you again.
Apr 18 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
2
by: Tom Loach | last post by:
Our system administrator set up an NT server group in order to allow our users to login to our application via https to our sql server. The group appears as a User in SQL Server when you look at...
4
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the...
2
by: BillD | last post by:
I'm trying to derive a schema from a base schema. I want to redefine a "group" from the base schema in my derived schema in order to add more options to the "choice" aggregate (see schema1.xsd...
16
by: michael | last post by:
Is it possible to get all href URLs contained in a unordered list and place them in an array? Or in fact two different arrays, differently named one for each <ul> group? <ul> <li><a...
7
by: Darin | last post by:
I have a report that sub-totals on a group, then grand-totals at the report footer. If there's only one group, the sub-total and grand total are redundant, so I only want to show one of them. I...
1
by: David Horowitz | last post by:
Hi folks. I need to create a report that has a Group Header that pulls certain data from the Detail section. It's something like this: +--Report---------------------------------------- |...
7
by: Sameh Ahmed | last post by:
Hello there IsInrole gives ya the means to check if the current or impersonated user belongs to a specific windows role or group. is there a way to do the same without using ADSI to check if...
2
by: jon|k | last post by:
hi all-- i need to do a transformation that removes duplicates (among other things). to accomplish that, i'm trying to use for-each-group, but it doesn't work. i need to select for duplicates by...
3
by: Sebastian | last post by:
Hello all I have a report where I have two nested groups. I know there are only three standard options for running sum: None, Over Group and Over All. I have a MyTextBox in detail section where...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.