SQL 2008

Hello,
I'm trying to find the zoneid, Price of a postal code within a range of 2 columns.
My Table is:
ZoneID              Price                    PostalCodeFrom                        PostalCodeTo
481                      $5.oo                    L4A0A6                                  L5B9Z9
551                      $12.00                   L2X1B2                                  L3X1W3
214                      $7.50                     L3P0A0                                 L6S9Z9

is I want to find the zoneid,Price of postal codes,
example: L4B2E2,  L2X3X5, L5R2B4

Any Help is appreciated.
Thanks,
W.E.BAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan McCauleyEnterprise Analytics ManagerCommented:
You're looking for the BETWEEN operator:

DECLARE @PostalCode VARCHAR(6)
   SET @PostalCode = 'L4B2E2'

SELECT ZoneID, Price
  From YourTable
 WHERE @PostalCode BETWEEN PostalCodeFrom AND PostalCodeTo

Open in new window


BETWEEN finds values that are >= the first and <= the second, which it sounds like is what you want to do.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark WillsTopic AdvisorCommented:
@ryanmccauley - very close, but I think there is more to it than that - or - the sample data might be a bit out.

Alphabetically there are overlapping postalcodes, so, the query might return more than 1 row. The first example "L4B2E2" would return zones 481 and 214

So, the query needs a bit more qualification like TOP 1 based on price, zone or something else... What is the defining uniqueness that will give us a single answer ?

Then we also need to consider how it is to be used. How is the price lookup needed ? Is it a straight query, or maybe a subquery (or procedure or function) returning the value for some other data source ?

I think the best way to answer this is to first have a couple of the above questions answered first.
Ryan McCauleyEnterprise Analytics ManagerCommented:
I'd hope that the ranges are exclusive, but I suppose you're right - given the data above, you can't really tell. Hopefully the OP is back soon to clear things up.
Mark WillsTopic AdvisorCommented:
Agree...

And here is some sample code just to demonstrate some of the questions I raise above...

-- OK so, first lets create some test data 
-- For this we will use TEMPDB database and some (hopefully) *unique* table names

use tempdb
GO

-- first check for our test tables and remove so we can start afresh

if object_id('tmp_postal_prices','U') is not null drop table tmp_postal_prices
if object_id('tmp_things_to_post','U') is not null drop table tmp_things_to_post
GO

create table tmp_postal_prices (zoneid int, price money, PostalCodeFrom varchar(20), PostalCodeTo varchar(20))
GO

-- we really should create an index depending on performance and volumes

create index idx_tmp_postal_prices_postalcodes on tmp_postal_prices (PostalCodeFrom, PostalCodeTo)
GO

-- now load some data

insert tmp_postal_prices
select 481,5.oo,'L4A0A6','L5B9Z9' union all
select 551,12.00,'L2X1B2','L3X1W3' union all
select 214,7.50,'L3P0A0','L6S9Z9'
GO

-- Now we can do a select with the "between" using one of the examples from: L4B2E2,  L2X3X5, L5R2B4

select Zoneid, Price 
from tmp_postal_prices 
where 'L4B2E2' between PostalCodeFrom and PostalCodeTo
GO
/*-- returns two rows - which maybe OK - but sounds like we want a (one) price.
481	5.00
214	7.50
*/

-- So, now we do a select but this time restricting the row selection
-- (and this is just one example, there can be other methods)

select top 1 Zoneid, Price 
from tmp_postal_prices 
where 'L4B2E2' between PostalCodeFrom and PostalCodeTo
Order by Price desc 
GO
/*-- returns a single row
214	7.50
*/

-- To deomnstrate the second question, ie "how to use" 
-- This time we will build another table containing information we want to price.

create table tmp_things_to_post (TID int, Item varchar(60), destination varchar (20))
GO

insert tmp_things_to_post 
select 1,'A big box','L4B2E2' union all
select 2,'A big brown paper bag','L5R2B4' union all
select 2,'A thing with 2x and 3x','L2X3X5' 
GO

-- now lets try to "price" our postal data...

Select t.*, p.zoneid, p.price
from tmp_things_to_post T
outer apply (select top 1 Zoneid, Price 
             from tmp_postal_prices 
             where t.destination between PostalCodeFrom and PostalCodeTo
             Order by Price desc ) P

-- and the above is only one method from various other methods available.
-- we could do an inline query
-- we could create a function
-- we could bundle it all in a Stored Procedure
-- we do need to consider indexes in all of this too 
--    (otherwise a possible scan everytime - ouch)

-- and now we can cleanup

if object_id('tmp_postal_prices','U') is not null drop table tmp_postal_prices
if object_id('tmp_things_to_post','U') is not null drop table tmp_things_to_post
GO

Open in new window

W.E.BAuthor Commented:
Hello,
using the Select --  Between, will return few hundred results per search, (this is what I was trying to avoid), What I did is, used the Select -- between , and I put some case if into my script, this way I can narrow the results,

I'm getting 3  results right now (which is not bad) vs few hundreds.

Thanks for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.