So I have this MySQL 5.0.24 table
select state, county, population from state_pops order by state, county;
yields
Arizona Yapavai 4
Arizona Pueblo 5
Arizona Pinal 8
Arizona Maricopa 13
Michigan Lawson 3
Michigan Maple 4
Michigan Appleton 8
Texas Richmond 5
Texas Dupree 7
Texas Brighton 10
I need to determine which is the most heavily-populated county in each state.
select state, county, max( population) from state_pops group by state order by state;
yields
Arizona Maricopa 13
Michigan Appleton 8
Texas Brighton 10
Easy. But now I need to somehow flag the most-populous county of each state while
listing all counties of all states, like so
Arizona Yapavai 4 NO
Arizona Pueblo 5 NO
Arizona Pinal 8 NO
Arizona Maricopa 13 YES
Michigan Lawson 3 NO
Michigan Maple 4 NO
Michigan Appleton 8 YES
Texas Richmond 5 NO
Texas Dupree 7 NO
Texas Brighton 10 YES
So I need to derive a column somehow, perhaps some form of CASE..WHEN Any ideas?
TIA,
Still-learning Stev
You can use this solution:
See a SQL-Fiddle Demo