Write a stored procedure to calculate total hours employee worked in a given year without any parameters being used?

Viewed 33
select Personid, firstname, lastname, Sum(reghours), YearWeek
from TimeSheet
group by PersonId
order by FirstName, LastName

I am trying to figure out how I could write a stored procedure without any parameters and for it to calculate total hours an employee worked in a given timeframe or would I have to have some parameters. I have yearweek I can use but how could I write the query to sum up the hours for a given year and separate them in columns. I am not sure if this makes sense.

1 Answers

You could use the function datepart, but you would need a datetime field:

SELECT Personid, firstname, lastname, Sum(reghours), DATEPART(YEAR, YearWeek)
FROM TimeSheet
GROUP BY PersonId, firstname, lastname, DATEPART(YEAR, YearWeek)
ORDER BY FirstName, LastName, DATEPART(YEAR, YearWeek)
Related