Complex Query Help

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2007
Posts: 9
Reputation: MetalHobin is an unknown quantity at this point 
Solved Threads: 0
MetalHobin MetalHobin is offline Offline
Newbie Poster

Complex Query Help

 
0
  #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:
  1. 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:
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Complex Query Help

 
0
  #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 6:41 am.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 9
Reputation: MetalHobin is an unknown quantity at this point 
Solved Threads: 0
MetalHobin MetalHobin is offline Offline
Newbie Poster

Re: Complex Query Help

 
0
  #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 Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Complex Query Help

 
0
  #4
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 9
Reputation: MetalHobin is an unknown quantity at this point 
Solved Threads: 0
MetalHobin MetalHobin is offline Offline
Newbie Poster

Re: Complex Query Help

 
0
  #5
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 Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Complex Query Help

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 9
Reputation: MetalHobin is an unknown quantity at this point 
Solved Threads: 0
MetalHobin MetalHobin is offline Offline
Newbie Poster

Re: Complex Query Help

 
0
  #7
Jun 25th, 2007
That did it! It was the GROUP BY SUBSTRING() Combination, thank you.
Marked as Solved
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Complex Query Help

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 9
Reputation: MetalHobin is an unknown quantity at this point 
Solved Threads: 0
MetalHobin MetalHobin is offline Offline
Newbie Poster

Re: Complex Query Help

 
0
  #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 Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC