Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

ASP.NET Forum


You are currently viewing our ASP.NET Forum as a guest. Please register to participate.
Login



Reply
SQL query that counts occurances
Old 04-06-2005, 07:47 PM SQL query that counts occurances
webgrrl's Avatar
Skilled Talker

Posts: 74
Trades: 0
I have a database full of zip codes, like this:

95945
95945
48864
48864
95630
95959
etc.

I'd like to find out how many of each zip I have. Is there query that I can do that will tell me this? It would be great if there is a query that will tell me: 959459 (2), 48864 (2), 95630 (1), 95959 (1), or something like that. I don't know all of the zip codes in the database so I can't query one-by-one.

Any suggestions?


Thanks,
WebGrrl
webgrrl is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-06-2005, 09:12 PM
Minaki's Avatar
Defies a Status

Posts: 1,626
Location: Guildford, UK
Trades: 0
You can find out each separate ZIP code (i.e. no repetitions) by using:

SELECT DISTINCT ZipCode FROM MyTable

You could then iterate through them using the SQL COUNT statement. That's a bit long winded though, I'm sure there's another way to do this. Try:

Code:
SELECT COUNT(ZipCode), ZipCode
FROM MyTable
GROUP BY ZipCode
If that doesn't work (it's off the top of my head at 1am), look up on Google the syntax of COUNT and DISTINCT in SQL and you should be able to build something...
__________________
Minaki Serinde MCP
"Wow, Linux is nearly on-par with Windows ME!"

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Minaki is offline
Reply With Quote
View Public Profile Visit Minaki's homepage!
 
Old 04-07-2005, 11:45 AM
ColdFusion's Avatar
Extreme Talker

Posts: 224
Location: NY
Trades: 1
Try this:

SELECT ZipCode, COUNT(ZipCode) as Total
FROM MyTable
GROUP BY ZipCode
ORDER BY Total DESC
__________________

Please login or register to view this content. Registration is FREE
*
Please login or register to view this content. Registration is FREE
*
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
*
Please login or register to view this content. Registration is FREE
*
Please login or register to view this content. Registration is FREE
*
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
*
Please login or register to view this content. Registration is FREE
ColdFusion is offline
Reply With Quote
View Public Profile
 
Old 04-07-2005, 06:50 PM
webgrrl's Avatar
Skilled Talker

Posts: 74
Trades: 0
Cool - I will try this!

Thanks,
WebGrrl
webgrrl is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to SQL query that counts occurances
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.12923 seconds with 12 queries