LittlBUGer
Programmer
Hello all. I'm working on sort of a time keeping program for the place I work. Everything is done except for a few administrative reports. One of the reports needs to grab certain information from the SQL database based on each employee's hours (worked hours) entries that they input. This report is grabbing the employee's name, the entry date, the project worked on, the comment left by employee, and the hours worked. Based on a certain selected criteria, the SQL can change slightly and thus either display information for a single employee for all projects within a given date range or for all employees for a single project for a given date range. Either way, there's a certain date range.
What I need to do is not only grab each of the hours entries, but create subtotals of the hours worked based on day and on week (a week being Sun - Sat). I have a SQL query that does all of this fine up through the day subtotal, but I can't quite figure out how to get the week subtotal.
Here's my query if it will help:
I know it looks confusing, but it's quite simple. The things that can change are the EmpID (the employee's ID) and the data range in the Where clause. The 2 UNION ALL's up above are giving the subtotal line, with the first union doing it by day, the 2nd union doing it by week (though right now it's just doing the same thing as the day union). Then later in the code I change those subtotal lines to be formatted nicer as the rest of the code is in ASP.NET (the triple 9's and Z's are just so it orders correctly).
Thus, if I'm creating a report for the month (August is shown above), then it will show each hours entry for that employee and a day subtotal for hours after each day. I then also need a subtotal of hours for each week with a week being from Sun. to Sat. Thus if the date range doesn't start on a Sun., the first week needs to be from the start date to the next Sat., and so on. If the end date isn't on a Sat., then the week subtotal needs to be from the previous Sun. to the end date. Pretty simple idea but difficult to implement, at least that's how I see it.
If anyone understands what I'm trying to do, maybe there's something I'm missing and you can help me? Thank you for your time.
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
What I need to do is not only grab each of the hours entries, but create subtotals of the hours worked based on day and on week (a week being Sun - Sat). I have a SQL query that does all of this fine up through the day subtotal, but I can't quite figure out how to get the week subtotal.
Here's my query if it will help:
Code:
Select (lastname + ', ' + firstname) as username, wdate, projectname, projectid, phasename, phaseid, taskname, taskid, clin.clin, clin.clindesc, comment, whours
From (SELECT DISTINCT clin, clindesc FROM Clin) AS Clin JOIN Hours ON Clin.Clin = Hours.Clin JOIN Task ON Hours.TaskKey = Task.TaskKey JOIN Employee ON Hours.EmpID = Employee.EmpID
Where hours.EmpID='3' and not Hours.complete='D' AND wdate BETWEEN '8/1/2006' AND '8/31/2006'
UNION ALL Select username, wdate, 'ZZY', '998', 'ZZZ', '999', 'ZZZ', '999', '', '', 'DaySubTotal', SUM(SumDayQuery.whours)
FROM (Select (lastname + ', ' + firstname) as username, wdate, projectname, projectid, phasename, phaseid, taskname, taskid, clin.clin, clin.clindesc, comment, whours
From (SELECT DISTINCT clin, clindesc FROM Clin) AS Clin JOIN Hours ON Clin.Clin = Hours.Clin JOIN Task ON Hours.TaskKey = Task.TaskKey JOIN Employee ON Hours.EmpID = Employee.EmpID
Where hours.EmpID='3' and not Hours.complete='D' AND wdate BETWEEN '8/1/2006' AND '8/31/2006' ) As SumDayQuery
Group By username, wdate
UNION ALL Select username, wdate, 'ZZZ', '999', 'ZZZ', '999', 'ZZZ', '999', '', '', 'WeekSubTotal', SUM(SumWeekQuery.whours)
FROM (Select (lastname + ', ' + firstname) as username, wdate, projectname, projectid, phasename, phaseid, taskname, taskid, clin.clin, clin.clindesc, comment, whours
From (SELECT DISTINCT clin, clindesc FROM Clin) AS Clin JOIN Hours ON Clin.Clin = Hours.Clin JOIN Task ON Hours.TaskKey = Task.TaskKey JOIN Employee ON Hours.EmpID = Employee.EmpID
Where hours.EmpID='3' and not Hours.complete='D' AND wdate BETWEEN '8/1/2006' AND '8/31/2006' ) As SumWeekQuery
Group By username, wdate
Order By 1, 2, 3, 4, 5, 6, 7, 8, 12
I know it looks confusing, but it's quite simple. The things that can change are the EmpID (the employee's ID) and the data range in the Where clause. The 2 UNION ALL's up above are giving the subtotal line, with the first union doing it by day, the 2nd union doing it by week (though right now it's just doing the same thing as the day union). Then later in the code I change those subtotal lines to be formatted nicer as the rest of the code is in ASP.NET (the triple 9's and Z's are just so it orders correctly).
Thus, if I'm creating a report for the month (August is shown above), then it will show each hours entry for that employee and a day subtotal for hours after each day. I then also need a subtotal of hours for each week with a week being from Sun. to Sat. Thus if the date range doesn't start on a Sun., the first week needs to be from the start date to the next Sat., and so on. If the end date isn't on a Sat., then the week subtotal needs to be from the previous Sun. to the end date. Pretty simple idea but difficult to implement, at least that's how I see it.
If anyone understands what I'm trying to do, maybe there's something I'm missing and you can help me? Thank you for your time.
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein