I'm having a bit of problems with these tables...

TableCostId int,
Cost numeric,
MonthsId int


Percentt numeric,
MonthsId int

I have to calculate a new value by getting the biggest Taxes.MonthsId that do not exceed the TableCost.MonthsId (for each row).

For example, if I have the following registers:





To calculate the TaxedCost of the

first row of the TableCost -> 10*55
Second row -> 5*60
Third row ->12*65
Fourth row -> 17*65
Fifth row -> 22*70

The select must return the following fields


I've tought of declaring a table variable... storage the Taxes values on it, runs a normal select on the TableCost table and looping each of its rows calculating the TaxedCost after the select.

Any ideas of improve this query?

  • Given the data you've specified, you're going to have to run a correlated subquery. Performance is going to suck no matter what you do. Commented Feb 7, 2012 at 10:08

1 Answer 1


You can do this with a correlated sub query.

       Cost * (SELECT TOP 1 Percentt 
               FROM   Taxes t 
               WHERE  t.MonthsId <= tc.MonthsId 
               ORDER  BY t.MonthsId DESC) AS TaxedCost 
FROM   TableCost tc 

You may well get a more efficient plan though if you simply fill in the gaps in your Taxes table so you can do a straight forward join on MonthsId. At least that should allow the optimiser to consider something other than nested loops.

  • +1 - Just a question out of curiosity, could this be done with an OUTER APPLY instead?, would it make any difference?.
    – Lamak
    Commented Feb 7, 2012 at 13:20
  • @Lamak - Yes. AFAIK OUTER APPLY would likely give the same plan though in this case. Commented Feb 7, 2012 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.