November 27, 2007 at 7:12 am
Hi, I'm new to sql and was wondering if it is possible to calculate overtime hours based on daily total hours for a given pay period? I need to be able to determine in what dept the overtime occurred as well.
I've attached the stored procedure hoping someone may be able to help. Thanks in advance.
I should point out that everything worked until I added the "OTimes = " case statement.
CREATE PROCEDURE _spHourlyTotals
@staffid varchar(5),
@paydate datetime
AS
declare @OTMax decimal
set @RegMax = 40.0
select Dept,
sum(TotalHours) as PayTotal,
sum(case when (Type = '10') then TotalHours else 0 end) as RegTotal,
sum(case when (Type = '4') then TotalHours else 0 end) as PTOTotal,
sum(case when (Type = '8') then TotalHours else 0 end) as SickBankTotal,
sum(case when (Type not in ('10','4','8')) then TotalHours else 0 end) as OtherTotal
OTime = case when (select sum(TotalHours)
from tadetail
where ( (StaffID = @staffid) and (PayPeriod = @paydate ))) > @RegMax
then (sum(TotalHours) - @RegMax)
end
from
tadetail
where
StaffID = @staffid and
(PayPeriod = @paydate)
Group by
Dept
November 27, 2007 at 7:33 am
What error message are you getting, it should be possible - probably a syntax probllem.
November 27, 2007 at 7:37 am
Yes - I am getting a syntax error and it is on the following part of the code.
It doesn't like the "OTime" and it doesn't like the ">" sign.
Thanks.
OTime = case when (select sum(TotalHours)
from tadetail
where ( (StaffID = @staffid) and (PayPeriod = @paydate ))) > @RegMax
then (sum(TotalHours) - @RegMax)
end
November 27, 2007 at 7:43 am
Can you post the actual error message, I'm guesssing here (because I can't see the message) but you might need to wrap a SUM( ) around your CASE.
November 27, 2007 at 7:47 am
I don't think you can put a 'SELECT' statement inside a CASE statement.
November 27, 2007 at 7:52 am
So something like
declare @OTMax decimal
set @RegMax = 40.0
select Dept,
sum(TotalHours) as PayTotal,
sum(case when (Type = '10') then TotalHours else 0 end) as RegTotal,
sum(case when (Type = '4') then TotalHours else 0 end) as PTOTotal,
sum(case when (Type = '8') then TotalHours else 0 end) as SickBankTotal,
sum(case when (Type not in ('10','4','8')) then TotalHours else 0 end) as OtherTotal
OTime = case when sum(TotalHours) > @RegMax then sum(TotalHours) - @RegMax
else 0
end
from
tadetail
where
StaffID = @staffid and
(PayPeriod = @paydate)
Group by
Dept
November 27, 2007 at 7:54 am
Here is the error message I get:
Error 170: Line 17: Incorrect syntax near 'OTime'.
Line 19: Incorrect syntax near '>'.
I've seen other code with select statments inside a case statement so I don't think that is the problem. Thanks.
November 27, 2007 at 7:57 am
I'm missing a comma after as OtherTotal
November 27, 2007 at 7:57 am
Looks like a missing comma (,) after OtherTotal.
November 27, 2007 at 8:04 am
SSC Veteran - Thanks. That got rid of the syntax errors so I will test this out with data.
Thanks again....
November 27, 2007 at 8:13 am
SSC Veteran - The reason I put the select statement in the OTime variable was to get the sum of total hrs. The current procedure only calculates total hours by department, therefore no overtime is being reported due to breakdown of departments. Hopefully with the info you have generously supplied, I should be able to figure the rest out. Thanks again.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply