tags:

views:

69

answers:

4

Hi,

I've got a query that returns the cost of wages for a given member of staff

SELECT     totalhours * staffbaserate AS TotalCost
FROM         newrotaRaw
WHERE     staffref = @staffref

However I need to do an additional bit of maths if the returned value is > 105. The bit of maths I need to do is that if the value is < 105 the value is returned as is, however if the value is > 105 then I need to do value * 1.128.

For example:

John Smith's Cost is 90 therefore the query should return 90

David Smith's Cost is 140 therefore the query should return 157.92

I'm sure there's some way to use replace to get it to do what I want but I've only ever used replace for exact matches, in this case the replace is conditional on the value.

Any help would be much appreciated!

+1  A: 
SELECT    totalhours * staffbaserate * (CASE WHEN (totalhours * staffbaserate) < 105 THEN 1 ELSE 1.128 END) AS TotalCost
FROM         newrotaRaw
WHERE     staffref = @staffref
Quassnoi
A: 

You're looking for the CASE statement.

hometoast
+4  A: 

Hi,

Try something like this.

SELECT     TotalCost = 
    CASE
     WHEN (totalhours * staffbaserate) < 105 THEN (totalhours * staffbaserate)
     ELSE (totalhours * staffbaserate) * 1.128
    END
FROM         newrotaRaw
WHERE     staffref = @staffref
John Sansom
technically, his requirements specified <= 105 rather than < 105, but the gist of the solution is right.
Joel Coehoorn
Thanks for that tippex but the equality component is not explicitly defined either way in the original post.
John Sansom
Ooh, you're right. I re-read it and the first time I had stopped at the first < sign to determine what comparison to use. I suspect that <= is still right, though, as this is likely calculating an overtime bonus that takes effect _after_ 105 hours.
Joel Coehoorn
Who in their right mind would work more than 105 hours ;-)
John Sansom
+3  A: 
SELECT     
    CASE WHEN totalhours * staffbaserate <= 105 THEN totalhours * staffbaserate 
         ELSE totalhours * staffbaserate * 1.128 END AS TotalCost
FROM         newrotaRaw
WHERE     staffref = @staffref
Joel Coehoorn