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.
15 1803
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. -
SELECT State_Name,
-
(SELECT Top 5 City_Name
-
FROM TableName As T1 INNER JOIN T2
-
ON T1.State_Name=T2.StateName
-
ORDER BY Population DESC)
-
Population
-
FROM TableName As T2;
-
Mary
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.
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.
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.
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.
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.
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
- SELECT Temp.State_Name, Temp.City_Name, Temp.Population
-
FROM Table1 AS Temp
-
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.
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 ;)
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
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
- SELECT Temp.State_Name, Temp.City_Name, Temp.Population
-
FROM Table1 AS Temp
-
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.
Thanks. Quite proud of it myself lol.
Hi, I'm the OP. Thanks for the helpful ideas, everyone!
Not a problem. Good to hear from you again.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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----------------------------------------
|...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
|
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...
| |