I currently have a database table called civic_zip_code with zip, city, state, latitude, longitude columns.
I’ve been trying to get this working for a little while now and I can’t quite figure it out. Basically, since some cities have multiple zip codes, I’m trying to create a table that will allow me to associated all zip codes with that city. Since I want to have it readily available, I would like to dump it into a table, so I’m trying to come up with a SELECT INTO statement that will do it all for me.
I currently have this:
select distinct concat(city, ', ', state) as location, city, state, group_concat(zip SEPARATOR '|') as zip_codes
from civic_zip_code
group by city
What I have seems close, but what I’m seeming to have a problem is that it seems to only be using distinct on the city. I’m getting only Rochester, MN in the results, where I’m sure there should at least be a Rochester, NY as well. Also, it is combining the zip codes, so although I’m only getting Rochester, MN, it is including the Rochester, NY zip codes within the zip_codes column for Rochester, MN.
Below is exactly what I’m seeing for Rochester.
location city state zip_codes
-------------- ------------- -------------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Rochester, MN Rochester MN 55903|55904|55905|55906|48309|55902|55901|46975|48308|53167|48307|48306|62563|79544|42273|05767|14664|14683|14653|14660|14673|02770|03839|14694|14652|14651|14650|14649|14692|03868|15074|03866|03867|14647|14607|14606|14639|14642|14064|14601|14611|14608|14610|14602|14621|14622|14626|14603|14624|14609|14627|14604|14605|14623|14643|14619|14612|14625|14644|14613|14645|14646|14617|14618|14614|14616|14620|14615|14638|98579
I know at least the 146xx zip codes should be part of Rochester, NY rather than Rochester, MN.
Even if I take distinct out, it seems to be running into the same problem.
I tried a few other queries but none of them seemed to get what I was looking for.
Is there something I’m missing or an easy way to achieve what I’m looking for?
Thanks!
It sound like you want to group by both city and location. Like this: