4

I have a function that takes 2 parameters: @iEmployeeID and @dDate.

It's purpose is to find a budget rate for the given parameters. In other words, it should find the largest date being smaller or equal to the @dDate argument, and return the rate that corresponds.

Budget rates are:

Start        Rate
-------      -----
01-01-2008   600
01-01-2009   800
01-01-2010   700
DECLARE @result decimal(38,20)

SELECT @result = decRate
FROM BudgetRates BR
WHERE BR.iRefEmployeeID = @iEmployeeID
GROUP BY decRate
HAVING MAX(BR.dStart) <= @dDate

RETURN @result

  • When supplied the argument 06-06-2008, it correctly returns 600.
  • When supplied the argument 03-03-2009, it correctly returns 800
  • When supplied the argument 02-02-2010, it should return 700. The function actually returns 800.

Where is the bug?

bug hunting: If I tweak around with the figures, it seems to pick the largest rate if it has 2 values to pick from.

2
  • bughunting: Now if I "ruin" my data and edit the 700 rate to 810. It correctly picks the that rate when the question is 02-02-2010. Commented Jul 14, 2010 at 13:58
  • Ahh there was something. Solution giving the correct answer: DECLARE @result decimal(38,20) SELECT TOP 1 @result = decRateWeekday FROM tbl_eCon_Target_BudgetRates BR WHERE BR.iRefEmployeeID = @iEmployeeID GROUP BY decRateWeekday, dStart HAVING MAX(BR.dStart) <= @dDate ORDER BY dStart DESC RETURN @result thank you so much for the help, I really appreciate it. Commented Jul 14, 2010 at 14:02

4 Answers 4

4

Surely your code shouldn't be grouping at all?

SELECT TOP 1 @result = decRate
FROM BudgetRates BR
WHERE BR.iRefEmployeeID = @iEmployeeID
AND BR.dStart <= @dDate
ORDER BY BR.dStart DESC

RETURN @result
Sign up to request clarification or add additional context in comments.

3 Comments

When I leave out the GROUP BY I get: Column 'BudgetRates.decRate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. And Top 1 picks the rate 600.
@DoStuffz - You can't have left out the GROUP BY if you get that message.
@DoStuffz: If TOP 1 is picking a rate of 600, that sounds as if you left off the DESC on the ORDER BY.
1

You should select top 1 using TOP 1 get the appropriate one with ORDER BY

DECLARE @result decimal(38,20)

SELECT TOP 1 @result = decRate
FROM BudgetRates BR
WHERE BR.iRefEmployeeID = @iEmployeeID
ORDER BY decRate DESC

RETURN @result

2 Comments

When I do this I get. "Invalid use of a side-effecting operator 'SET ROW COUNT' within a function."
oh.. i forgot you can't do that in a function.. correcting for Top 1
1

Seems like you are supposed to use ranking functions here.

DECLARE @result decimal(38,20)

SELECT @result = decRate 
(
  SELECT decRate, ROW_NUMBER() OVER (ORDER BY BR.dStart DESC) rownum
  FROM BudgetRates BR
  WHERE BR.iRefEmployeeID = @iEmployeeID
  AND BR.dStart <= @dDate
) sub
WHERE rownum = 1

RETURN @result

Comments

0

try this mate:

select @result = decRate
    from BudgetRates
      inner join (
       select max(BR.dStart) as MaxDate
       from BudgetRates
      where BR.dStart <= @dDate
        and BR.iRefEmployeeID = @iEmployeeID
                 ) temp on tmep.MaxDate = BudgetRates.dStart
                   and BudgetRates.iRefEmployeeID = @iEmployeeID

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.