Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Query and Sum

Posted on 2011-09-06
3
Medium Priority
?
479 Views
Last Modified: 2012-05-12
I am working on developing a query against Microsoft Project Server database.  I want the ability to calculate the amount of hours an individual will be working within a given period of time.  I've developed the code noted below. and it works, for the most part.  

However, as you will see, if a 'resource' has multiple tasks during that week, it will return that resource multiple times.  I was to return only 1 row for each individuals.

Here's what I need:
Resource Name                     Work Sum
V                                                44
W                                                20
M                                                14
B                                                 0

Can someone assist?
SELECT     MSP_EpmAssignment.AssignmentStartDate, MSP_EpmAssignment.AssignmentFinishDate, SUM(MSP_EpmAssignmentByDay_UserView.AssignmentWork) 
                      AS WorkSum, MSP_EpmResource_UserView.ResourceName
FROM         MSP_EpmAssignmentByDay_UserView INNER JOIN
                      MSP_EpmProject_UserView ON MSP_EpmAssignmentByDay_UserView.ProjectUID = MSP_EpmProject_UserView.ProjectUID INNER JOIN
                      MSP_EpmTask_UserView ON MSP_EpmAssignmentByDay_UserView.ProjectUID = MSP_EpmTask_UserView.ProjectUID AND 
                      MSP_EpmAssignmentByDay_UserView.TaskUID = MSP_EpmTask_UserView.TaskUID INNER JOIN
                      MSP_EpmAssignment ON MSP_EpmAssignmentByDay_UserView.AssignmentUID = MSP_EpmAssignment.AssignmentUID AND 
                      MSP_EpmAssignmentByDay_UserView.ProjectUID = MSP_EpmAssignment.ProjectUID AND 
                      MSP_EpmAssignmentByDay_UserView.TaskUID = MSP_EpmAssignment.TaskUID INNER JOIN
                      MSP_EpmResource_UserView ON MSP_EpmAssignment.ResourceUID = MSP_EpmResource_UserView.ResourceUID
GROUP BY MSP_EpmAssignment.AssignmentStartDate, MSP_EpmAssignment.AssignmentFinishDate, MSP_EpmResource_UserView.ResourceName
HAVING      (MSP_EpmAssignment.AssignmentStartDate >= CONVERT(DATETIME, '2011-08-28 00:00:00', 102)) AND 
                      (MSP_EpmAssignment.AssignmentFinishDate <= CONVERT(DATETIME, '2011-09-04 00:00:00', 102)) AND 
                      (NOT (MSP_EpmResource_UserView.ResourceName IS NULL))

Open in new window

Query.jpg
0
Comment
Question by:eciabattari
  • 3
3 Comments
 
LVL 6

Expert Comment

by:markterry
ID: 36489826
Your problem is not the multiple tasks, but rather more clearly, the multiple start and finish dates.

You can build a table of weeks, and join to that table where assignment start and end date are withing that week, and group on week.
0
 
LVL 6

Expert Comment

by:markterry
ID: 36489881
group on week instead of assignment start and end date i mean.

So that might look like

TableWeeks

Week                                       Start            End
2011-09-04 to 2011-09-10      2011-09-04  2011-09-10
etc.


Select Week, Sum(Amount) from tasks
inner join TablesWeek
on tasks.assigment_start  >= Week.Start
and task.assignment_end <= week.end


However, this will be a problem when an assignment lasts longer than a week, so maybe you only want to worry about the week start date only and call it WeekStarted.
0
 
LVL 6

Accepted Solution

by:
markterry earned 2000 total points
ID: 36489933
Shoot, I should also add into that, that even if it is just weekstart, you also have to say that assignment start less than weekend, and if it is definitively in a week, then also assignment end must be > week.start.

Only care about week start:
Select Week, Sum(Amount) from tasks
inner join TablesWeek
on tasks.assigment_Start  >= Week.Start
and task.assignment_Start <= week.end

Open in new window


Must lie in a week:
Select Week, Sum(Amount) from tasks
inner join TablesWeek
on tasks.assigment_Start  >= Week.Start
and task.assignment_Start <= week.end
and tasks.assigment_End  >= Week.Start
and task.assignment_End <= week.end

Open in new window

0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question