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 2005 Forums
 Transact-SQL (2005)
 Sort by set value first then asc on the rest

Author  Topic 

creichard
Starting Member

1 Post

Posted - 2009-01-15 : 11:57:30
I have a query which returns the businesses within a list of zip codes generated by the user. The interface is a simple proceedure where the user enters a zip code and a radius. The code then generates a list of zip codes within that radius. Pretty basic.

Here's my SQL question: I need to sort the query results by zip code with the first zip code being the zip code the user entered, then the remaining results are sorted asc by zip code. How do I define the sort statement to list the users zip code first then all other zip codes?

thanks all

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2009-01-15 : 12:26:41
I think you may have to do a UNION. Select the user-inputted zip code by itself and then union the list of other zip codes sorted by ascending order.
Go to Top of Page

tosscrosby
Aged Yak Warrior

676 Posts

Posted - 2009-01-15 : 12:31:45
skorch is right. They is no way to do a dual sort on a single column within a single select statement.

Terry

-- Procrastinate now!
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2009-01-15 : 13:39:31
quote:
Originally posted by creichard

I have a query which returns the businesses within a list of zip codes generated by the user. The interface is a simple proceedure where the user enters a zip code and a radius. The code then generates a list of zip codes within that radius. Pretty basic.

Here's my SQL question: I need to sort the query results by zip code with the first zip code being the zip code the user entered, then the remaining results are sorted asc by zip code. How do I define the sort statement to list the users zip code first then all other zip codes?

thanks all



and how does this query work? I'm getting curious. Zip code cannot be a single co-ordinate, how do you find zip codes within fixed a distance from one spot?
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2009-01-15 : 13:53:51
quote:
Originally posted by tosscrosby
...They is no way to do a dual sort on a single column within a single select statement...

Never say never:

order by
case
when ZipCode = @UserZipCode
then 0
else 1
end,
ZipCode





CODO ERGO SUM
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2009-01-15 : 14:04:12
quote:
Originally posted by rohitkumar

quote:
Originally posted by creichard

I have a query which returns the businesses within a list of zip codes generated by the user. The interface is a simple proceedure where the user enters a zip code and a radius. The code then generates a list of zip codes within that radius. Pretty basic.

Here's my SQL question: I need to sort the query results by zip code with the first zip code being the zip code the user entered, then the remaining results are sorted asc by zip code. How do I define the sort statement to list the users zip code first then all other zip codes?

thanks all



and how does this query work? I'm getting curious. Zip code cannot be a single co-ordinate, how do you find zip codes within fixed a distance from one spot?



Most commercial Zip Code files have the latitude and longitude of the post office included, so it is fairly easy to do a search based on that. This link below has plenty of discussion about how to do the search.

Great Circle Distance Function - Haversine Formula
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81360

CODO ERGO SUM
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2009-01-15 : 14:21:46
but then zip code is for an area and its not just 2 co-ordinates

A-------D----------------------------------------------B

so here if you have co-ordinates for two zip codes A (which may be Sears tower standing on a small patch of land and having its own zip code) and B how do you find if business D lies in zip code A or zip code B? you have to know the border too right?
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2009-01-15 : 14:33:48
quote:
Originally posted by rohitkumar

but then zip code is for an area and its not just 2 co-ordinates

A-------D----------------------------------------------B

so here if you have co-ordinates for two zip codes A (which may be Sears tower standing on a small patch of land and having its own zip code) and B how do you find if business D lies in zip code A or zip code B? you have to know the border too right?


That falls under the category of "good enough".

Most of these searches are just looking for something like "show me the stores close to where I live", so the fact that you are really two miles away from the post office doesn't matter that much.




CODO ERGO SUM
Go to Top of Page

tosscrosby
Aged Yak Warrior

676 Posts

Posted - 2009-01-16 : 08:23:03
quote:
Originally posted by Michael Valentine Jones

quote:
Originally posted by tosscrosby
...They is no way to do a dual sort on a single column within a single select statement...

Never say never:

order by
case
when ZipCode = @UserZipCode
then 0
else 1
end,
ZipCode




CODO ERGO SUM



MVJ - Gonna have to file this one in my scripts folder for future use. Thanks!

Terry

-- Procrastinate now!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-16 : 08:26:37
I think this is good too...
ORDER BY	NULLIF(ZipCode, @UserZipCode)
One thing to think of is that a NULL value in zipcode column may be sorted differently within other nulls (such as wanted zipcode), but still in right order.
You can solve that with
ORDER BY	NULLIF(ZipCode, @UserZipCode), ZipCode DESC


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -