•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 402,907 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,101 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums
Views: 1476 | Replies: 8 | Solved
![]() |
| |
•
•
Join Date: Jan 2007
Posts: 9
Reputation:
Rep Power: 0
Solved Threads: 0
I need to write a query that pulls out Zip codes by the first three characters, so I started with this:
But I would also like to query out the zips by those substrings I pulled out, so that would be like this:
And I need the result to be unique, and also I would like to sum all of the values that are in a column called [AvailNums] which belong to zip codes that have similar beginnings.
In other words if there are four zip codes like so:
Zip AvailNums
97301 425
97305 345
12302 1236
12305 445
I would like the resultset to look like this:
Zip AvailNums
973 770
123 1681
SELECT SUBSTRING(Zip, 1, 3) FROM AvailHomeZips AS tZip
But I would also like to query out the zips by those substrings I pulled out, so that would be like this:
SELECT Zip From AvailHomeZips WHERE Zip LIKE @Zip + "%"
And I need the result to be unique, and also I would like to sum all of the values that are in a column called [AvailNums] which belong to zip codes that have similar beginnings.
In other words if there are four zip codes like so:
Zip AvailNums
97301 425
97305 345
12302 1236
12305 445
I would like the resultset to look like this:
Zip AvailNums
973 770
123 1681
I don't know how exactly MS SQL behaves, but here is an equivalen in MySQL. Try to modify this to your needs:
sql Syntax (Toggle Plain Text)
SELECT substring(zip, 1, 3) AS ZIP, sum(AvailNums) AS SUM FROM AvailHomeZips GROUP BY ZIP;
Last edited by ~s.o.s~ : Jun 23rd, 2007 at 5:41 am.
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
I had tried the query pasted by me in my previous post on MySQL so I don't know what went wrong when the same was ported to MS SQL.
Try this:
I guess MS SQL doesn't take into consideration the case hence the results. Post if it works or not.
Try this:
sql Syntax (Toggle Plain Text)
SELECT substring(zip, 1, 3) AS Z, sum(AvailNums) AS SUM FROM AvailHomeZips GROUP BY Z;
I guess MS SQL doesn't take into consideration the case hence the results. Post if it works or not.
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
If what you say is true, then try out this one:
sql Syntax (Toggle Plain Text)
SELECT substring(zip, 1, 3), sum(AvailNums) FROM AvailHomeZips GROUP BY substring(zip, 1, 3);
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Glad it worked out for you, though I must go out to say that MS SQL sucks if it can't even allow a alias to be used with the group-by clause which is so common a thing with present databases. So much for the Enterprise level database... ;-)
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
![]() |
•
•
•
•
•
•
•
•
DaniWeb MS SQL Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the MS SQL Forum
- Previous Thread: insert multiple rows in database
- Next Thread: Stored procedure with Dynamic columns?



Hybrid Mode