User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jan 2007
Posts: 9
Reputation: MetalHobin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MetalHobin MetalHobin is offline Offline
Newbie Poster

Question Complex Query Help

  #1  
Jun 22nd, 2007
I need to write a query that pulls out Zip codes by the first three characters, so I started with this:
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,816
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Complex Query Help

  #2  
Jun 23rd, 2007
I don't know how exactly MS SQL behaves, but here is an equivalen in MySQL. Try to modify this to your needs:

  1. SELECT substring(zip, 1, 3) AS ZIP, sum(AvailNums) AS SUM
  2. 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."
Reply With Quote  
Join Date: Jan 2007
Posts: 9
Reputation: MetalHobin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MetalHobin MetalHobin is offline Offline
Newbie Poster

Re: Complex Query Help

  #3  
Jun 25th, 2007
That worked, except for the Zip column, I am still getting redundant zips. For all of the zips with the same first three letters, I would like to group those together.
I can't find a syntactically correct way of incorporating DISTINCT into this query.
Reply With Quote  
Join Date: Jan 2007
Posts: 9
Reputation: MetalHobin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MetalHobin MetalHobin is offline Offline
Newbie Poster

Re: Complex Query Help

  #4  
Jun 25th, 2007
No, this didn't work, because when I use the AS keyword in MS SQL, I can't use the alias in a GROUP BY clause, it give me a syntax error.

I just got a SQL 2005 Programming book, but I appreciate the help, I will keep looking and post If I find a satisfactory solution.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,816
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Complex Query Help

  #5  
Jun 25th, 2007
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:
  1. 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."
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,816
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Complex Query Help

  #6  
Jun 25th, 2007
If what you say is true, then try out this one:

  1. 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."
Reply With Quote  
Join Date: Jan 2007
Posts: 9
Reputation: MetalHobin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MetalHobin MetalHobin is offline Offline
Newbie Poster

Re: Complex Query Help

  #7  
Jun 25th, 2007
That did it! It was the GROUP BY SUBSTRING() Combination, thank you.
Marked as Solved
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,816
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Complex Query Help

  #8  
Jun 25th, 2007
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."
Reply With Quote  
Join Date: Jan 2007
Posts: 9
Reputation: MetalHobin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MetalHobin MetalHobin is offline Offline
Newbie Poster

Re: Complex Query Help

  #9  
Jun 25th, 2007
I know, I should just use MySQL, the only thing is that the MySQL team hasn't released the MySQL-Visual Studio integration tools yet.
Until then, Adios
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MS SQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 3:37 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC