Group by Two Fields, Sort Field1 by a seperate Sum, then Field2

S

SEdison

Let's say I have a table….with 3 fields... State, City, Popluation

I have a query where I've group by State, then Grouped by City, then have a
value of Popluation

How can i sort the query (probably using a subquery) such that the highest
population state and it's highest population city would be listed FIRST
followed by ALL of its cities in descending populatin order, Then the state
with the second largest population's largest city followed by ALL of it's
cities, etc...

My issue has been that when sorting DESC it is sorting all lined seperately
and spreading the State throughout.

Thanks
 
K

KARL DEWEY

This uses two queries --

SEdison_StPop ---
SELECT SEdison.State, Sum(SEdison.Popluation) AS SumOfPopluation
FROM SEdison
GROUP BY SEdison.State
ORDER BY Sum(SEdison.Popluation) DESC;

SELECT SEdison_StPop.State, SEdison.City, SEdison.Popluation
FROM SEdison_StPop INNER JOIN SEdison ON SEdison_StPop.State = SEdison.State
ORDER BY SEdison_StPop.SumOfPopluation DESC , SEdison.Popluation DESC;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top