I have a MySQL table with communities and their corresponding MLS map area. Some communities are large and take up multiple map areas.
I am attempting to do a query that returns the most common communities and their map area when passed multiple map areas.
The query I am trying for map areas 601 and 606 is:
SELECT DISTINCT(community), mapArea FROM (
SELECT community, mapArea
FROM single_family
GROUP BY community
ORDER BY COUNT(community) DESC) AS query1
WHERE mapArea LIKE '601%' OR mapArea LIKE '606%' ORDER BY community
Example single_family Table Layout (actual table has over 60k rows):
community mapArea
Solera 606 - Henderson
Solera 606 - Henderson
Solera 204 - East
Solera 606 - Henderson
Solera 202 - East
Anthem 606 - Henderson
Green Valley 601 - Henderson
Green Valley 601 - Henderson
Green Valley 606 - Henderson
Seven Hills 606 - Henderson
Seven Hills 606 - Henderson
If I run a count on the table it shows:
community mapArea countCommunity
Anthem 606 - Henderson 776
Solera 606 - Henderson 58
Solera 204 - East 6
Solera 202 - East 1
Green Valley 601 - Henderson 188
Green Valley 606 - Henderson 117
Seven Hills 606 - Henderson 372
When I run the above query for map areas 601 and 606 I get the following which is correct for some communites but the community of Solera for example is not listed:
community mapArea
Anthem 606 - Henderson
Green Valley 601 - Henderson
Seven Hills 606 - Henderson
Since Solera has the most rows with the map area being 606 - Henderson, I am wondering what is wrong in the query to why it is not being included.
Any help to why this is not returning the expected results and what I have to do to get the expected results is very much appreciated.
You are grouping by
community
, which in result, might hide mapArea
lines within the grouped lines. try this instead:SELECT community, mapArea FROM (
SELECT community, mapArea
FROM single_family
WHERE mapArea LIKE '601%' OR mapArea LIKE '606%'
GROUP BY community
ORDER BY COUNT(community) DESC) AS query1
ORDER BY community
also, why using DISTINCT when you're really grouping by the same column ? the subquery will result in rows with distinct values of communities. using DISTINCT will just slow down the process
0 comments:
Post a Comment