Unix Technical Forum

How to look up a range

This is a discussion on How to look up a range within the DB2 forums, part of the Database Server Software category; --> Give a table with a lower bounds, upper bounds, and targets, I would like to select on a value ...


Go Back   Unix Technical Forum > Database Server Software > DB2

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 07:35 AM
Rajah
 
Posts: n/a
Default How to look up a range

Give a table with a lower bounds, upper bounds, and targets, I would
like to select on a value and supply the target. For example, think
about marginal tax rates:

AgiLo AgiHi Marginal
00 14600 10%
14601 59400 15%
59401 119950 25%
119951 182800 28%
182801 326450 33%
326451 infinity 35%

A straightforward approach would be
SELECT Marginal from marginalTaxRates where AGI >=AgiLo AND AGI <=
AgiHi

If I have to look up a lot of these Marginal tax rates, is there a more
efficient way to do this? (There might not be; I'm looking for
suggestions.)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 07:36 AM
Tonkuma
 
Posts: n/a
Default Re: How to look up a range

SELECT Marginal from marginalTaxRates where AGI BETWEEN AgiLo AND AgiHi

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 07:36 AM
Rajah
 
Posts: n/a
Default Re: How to look up a range


Tonkuma wrote:
> SELECT Marginal from marginalTaxRates where AGI BETWEEN AgiLo AND AgiHi


Has anyone used LOOKUP for this?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 07:36 AM
Brian Tkatch
 
Posts: n/a
Default Re: How to look up a range


Tonkuma wrote:
> SELECT Marginal from marginalTaxRates where AGI BETWEEN AgiLo AND AgiHi


Yeah, BETWEEN is the way to go with that SETUP.

Except "infinity" is not a number, but assuming this is for the real
world, noone will ever have the amaximum capacity of the field.

Just a note. If AgiLo is always (zero or ) one more than AgiHi, AgiHi
is inferred, and unneeded.

SELECT MAX(Marginal) from marginalTaxRates where AGI >=AgiLo

Of course >= is wasteful. Instead, it could be a field (Limit) whose
values are 0, then all the AgiHis besides "infinity".

SELECT MAX(Marginal) from marginalTaxRates where AGI > Limit

B.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-27-2008, 07:36 AM
--CELKO--
 
Posts: n/a
Default Re: How to look up a range

Change the last row to
(326451, NULL, 0.35)

NULL is now your "infinity" symbol. Use between for readability

SELECT marginal
FROM MarginalTaxRates
WHERE agi BETWEEN agi_lo
AND COALESCE (agi_hi, agi);

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-27-2008, 07:39 AM
Rajah
 
Posts: n/a
Default Re: How to look up a range


Brian Tkatch wrote:
> SELECT MAX(Marginal) from marginalTaxRates where AGI >=AgiLo


Yes, this would work if we always have a progressive system, where
Marginal rates are increasing. Unfortunately, I cannot make that
assumption with the real data.

You do have an excellent observation about the problem of
non-continguous data. After all, it is possible to have a
dollars-and-cents AGI that falls between the cracks.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:44 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com