SQL Question, MAX records that max column

TechBoyJK

Lifer
Oct 17, 2002
16,701
60
91
Hi Guys,

I'm querying a zip code database to find where records match a city name.

For instance, if I query the database for 'Saint Louis' I will get a bunch of matches for St. Louis, MO (one for every zip code), and a few matches for smaller towns in other states, that just happen to also be named Saint Louis.

SELECT city, state, zipcode

FROM zipcodes

WHERE city = 'saint louis'

I need to find which set of results has the most zip code matches for a particular state.

So with the above query, I'd get about 71 records returned, with all but 3 being from the Saint Louis in Missouri.

How could I figure out which CityName has the zip code most matches for a state? I'm trying to figure which city has the most zips.
 

dfnkt

Senior member
May 3, 2006
435
0
76
SELECT COUNT(zipcode) AS ZipCode, state
FROM zipcodes
WHERE city = 'saint louis'

I'll do some checking, but try that

EDIT:
--Working code
SELECT COUNT(zipcode) AS ZipCode, state
FROM zipcode
WHERE city = 'saint louis'
Group by state
 
Last edited:

TechBoyJK

Lifer
Oct 17, 2002
16,701
60
91
SELECT COUNT(zipcode) AS ZipCode, state
FROM zipcodes
WHERE city = 'saint louis'

I'll do some checking, but try that

EDIT:
--Working code
SELECT COUNT(zipcode) AS ZipCode, state
FROM zipcode
WHERE city = 'saint louis'
Group by state

Ok, so that counts em.. What's the best way to grab which state has the most records? Just the one at top?? Sort asc, then maxrow=1?


ps! thanks! im at work so I can't fiddle/experiment with the code right now.. so i try and figure out what I need to do before I have a chance so I can tear through more work at home.
 

dfnkt

Senior member
May 3, 2006
435
0
76
SELECT COUNT(zipcode) AS ZipCode, state
FROM zipcode
WHERE city = 'saint louis'
Group by state
Order By zipcode DESC

At first I thought that the state with the most would automatically be on top because in my test database I defined Missouri with 7 zip codes for Saint Louis and then Delaware with 8. Delaware came up on top, I then realized it was alphabetized because I added 9 entries for Wyoming (Wa) and it was listed on the bottom.

This was quickly fixed by adding Order by zipcode DESC
 

TechBoyJK

Lifer
Oct 17, 2002
16,701
60
91
This is what I had figured out but i'm getting a syntax error on line 2 at count(zipcode)

select top 1 state, zipcode

count(zipcode) as zips

from zipcodes

where city = '#city_match#'

group by state

order by zips desc
 

dfnkt

Senior member
May 3, 2006
435
0
76
Try ordering by zipcode instead of "Zips", perhaps your line 2 syntax error is an issue because of that line.
 

Chapbass

Diamond Member
May 31, 2004
3,148
89
91
Looks like you're missing a comma after zipcode on line 2.


EDIT: Sorry, line one, at the very end of the line. if you were to take out the whitespace, it would be

select top 1 state, zipcode count(zipcode) as zips

but you need

select top 1 state, zipcode, count(zipcode) as zips
 
Last edited:

Evadman

Administrator Emeritus<br>Elite Member
Feb 18, 2001
30,990
5
81
SELECT TOP 1 state FROM zipcodes WHERE city = 'saint louis' GROUP BY state ORDER BY Count(*) DESC
 

clamum

Lifer
Feb 13, 2003
26,255
403
126
SELECT TOP 1 state FROM zipcodes WHERE city = 'saint louis' GROUP BY state ORDER BY Count(*) DESC
I was going to post a slightly different query with a sub-query but I think this works and is more efficient.
 

TechBoyJK

Lifer
Oct 17, 2002
16,701
60
91
SELECT TOP 1 state FROM zipcodes WHERE city = 'saint louis' GROUP BY state ORDER BY Count(*) DESC

What's the advantage to TOP 1 in the SQL vs leaving it out but declaring "maxrows=1" in the cfquery tag (im using cfml)
 

Evadman

Administrator Emeritus<br>Elite Member
Feb 18, 2001
30,990
5
81
maxrows=1 only works in your tool. The query I posted will work in any MS SQL system in case you upgrade. In addition, the maxrows=1 will be hidden away from where expected in case anyone else needs to figure out what you were doing.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |