Link to home
Start Free TrialLog in
Avatar of W.E.B
W.E.B

asked on

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,
ASKER CERTIFIED SOLUTION
Avatar of Ryan McCauley
Ryan McCauley
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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.
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.
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

Avatar of W.E.B
W.E.B

ASKER

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.