Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Help me speed up my query!

Author  Topic 

aclarke
Posting Yak Master

133 Posts

Posted - 2001-05-23 : 02:19:55
I have to find info from a table of Zip Codes. Problem is, not every zip code is represented, so if a zip code is not there, I have to find the info from the one that's numerically closest. I used to do a query on the exact zip code and if no records were returned, select the next one higher and lower (using select top 1 and order by) and then comparing the 2 returned Zip Codes to the one I was looking for, but that involved 3 queries. I tried to do this:

select top 1 x5C, xMC1, abs(@zipCode - dest) as diff
from Shipping
order by diff

As you can imagine, this is very slow... Can anybody think of a better way of getting this back quickly in one query?

Thanks!
- Andrew.

   

- Advertisement -