相当新的Sql Server,我希望在匹配两列时加入两个查询。例如。如果列TimeByDay&查询中的ResourceName容量匹配查询分配中的列TimeByDay和ResourceName,然后拉取容量中的值。欢迎任何其他建议。如果我不清楚,请告诉我。感谢。
QueryAllocation
ProjectName TaskName ResourceName ResourceManager AssignmentWork TimeBYDay
Project1 Task1 ResourceA ManagerA 8 2015-1-1
Project1 Task2 ResourceB ManagerB 7.5 2015-1-2
Project2 Task3 ResourceA ManagerA 4 2015-1-5
Project3 Task4 ResourceA ManagerA 1 2015-1-5
Project3 Task5 ResourceB ManagerB 2 2015-1-5
Project4 Task6 ResourceC ManagerB 2 2015-1-5
查询
SELECT
P.ProjectName,
T.TaskName,
R.ResourceName,
RUV.[Resource Manager],
ABD.AssignmentWork,
TBD.TimeByDay
FROM dbo.MSP_TimeByDay TBD
LEFT JOIN dbo.MSP_EpmAssignmentByDay ABD
ON TBD.TimeByday = ABD.TimeByDay
LEFT JOIN dbo.MSP_EpmAssignment A
ON ABD.AssignmentUID = A.AssignmentUID
LEFT JOIN dbo.MSP_EpmResource R
ON A.ResourceUID = R.ResourceUID
LEFT JOIN dbo.MSP_EpmResource_UserView RUV
ON R.ResourceUID = RUV.ResourceUID
LEFT JOIN dbo.MSP_EpmTask T
ON A.TaskUID = T.TaskUID
LEFT JOIN dbo.MSP_EpmProject P
ON T.ProjectUID = P.ProjectUID
WHERE NOT (ABD.AssignmentUID IS NULL)
QueryCapacity
ResourceName Capacity TimeByDay
ResourceA 6 2015-1-1
ResourceB 4 2015-1-2
ResourceA 8 2015-1-5
ResourceB 7 2015-1-5
ResourceC 7.5 2015-1-5
查询
SELECT
R2.ResourceName,
RBD2.Capacity,
TBD2.TimeByDay
FROM dbo.MSP_TimeByDay TBD2
LEFT JOIN dbo.MSP_EpmResourceByDay RBD2
ON TBD2.TimeByDay = RBD2.TimeByDay
LEFT JOIN dbo.MSP_EpmResource R2
ON RBD2.ResourceUID = R2.ResourceUID
WHERE NOT (RBD2.ResourceUID IS NULL)
期望的结果
ProjectName TaskName ResourceName ResourceManager AssignmentWork TimeBYDay Capacity
Project1 Task1 ResourceA ManagerA 8 2015-1-1 6
Project1 Task2 ResourceB ManagerB 7.5 2015-1-2 4
Project2 Task3 ResourceA ManagerA 4 2015-1-5 8
Project3 Task4 ResourceA ManagerA 1 2015-1-5 8
Project3 Task5 ResourceB ManagerB 2 2015-1-5 7
Project4 Task6 ResourceC ManagerB 2 2015-1-5 7.5
答案 0 :(得分:1)
感谢SimplyInk,我不知道可以在SSMS中打开查询设计器。我现在得到了预期的结果。
SELECT
dbo.MSP_EpmAssignment.AssignmentUID, dbo.MSP_EpmAssignmentByDay.AssignmentWork, dbo.MSP_EpmAssignmentByDay.TimeByDay,
dbo.MSP_EpmResourceByDay.Capacity, dbo.MSP_EpmTask.TaskName, dbo.MSP_EpmProject.ProjectName, dbo.MSP_EpmResource.ResourceName,
dbo.MSP_EpmResource_UserView.[Resource Manager], dbo.MSP_EpmResource_UserView.[Resource Departments],
dbo.MSP_EpmResource_UserView.[Resource Role], dbo.MSP_EpmResource_UserView.ResourceEarliestAvailableFrom,
dbo.MSP_EpmResource_UserView.ResourceLatestAvailableTo, dbo.MSP_TimeByDay.TimeMonthOfTheYear, dbo.MSP_TimeByDay.TimeDayOfTheMonth,
dbo.MSP_TimeByDay.TimeYear
FROM
dbo.MSP_EpmAssignment
INNER JOIN dbo.MSP_EpmAssignmentByDay ON dbo.MSP_EpmAssignment.AssignmentUID = dbo.MSP_EpmAssignmentByDay.AssignmentUID
INNER JOIN dbo.MSP_EpmResourceByDay ON dbo.MSP_EpmAssignment.ResourceUID = dbo.MSP_EpmResourceByDay.ResourceUID AND
dbo.MSP_EpmAssignmentByDay.TimeByDay = dbo.MSP_EpmResourceByDay.TimeByDay
INNER JOIN dbo.MSP_EpmTask ON dbo.MSP_EpmAssignment.ProjectUID = dbo.MSP_EpmTask.ProjectUID AND
dbo.MSP_EpmAssignment.TaskUID = dbo.MSP_EpmTask.TaskUID AND dbo.MSP_EpmAssignmentByDay.ProjectUID = dbo.MSP_EpmTask.ProjectUID AND
dbo.MSP_EpmAssignmentByDay.TaskUID = dbo.MSP_EpmTask.TaskUID
INNER JOIN dbo.MSP_EpmProject ON dbo.MSP_EpmAssignment.ProjectUID = dbo.MSP_EpmProject.ProjectUID AND
dbo.MSP_EpmAssignmentByDay.ProjectUID = dbo.MSP_EpmProject.ProjectUID AND dbo.MSP_EpmTask.ProjectUID = dbo.MSP_EpmProject.ProjectUID
INNER JOIN dbo.MSP_EpmResource ON dbo.MSP_EpmAssignment.ResourceOwnerUID = dbo.MSP_EpmResource.ResourceUID AND
dbo.MSP_EpmAssignment.ResourceUID = dbo.MSP_EpmResource.ResourceUID AND
dbo.MSP_EpmResourceByDay.ResourceUID = dbo.MSP_EpmResource.ResourceUID
INNER JOIN dbo.MSP_EpmResource_UserView ON dbo.MSP_EpmResourceByDay.ResourceUID = dbo.MSP_EpmResource_UserView.ResourceUID
INNER JOIN dbo.MSP_TimeByDay ON dbo.MSP_EpmAssignmentByDay.TimeByDay = dbo.MSP_TimeByDay.TimeByDay
我需要以下加入
INNER JOIN dbo.MSP_EpmResourceByDay ON dbo.MSP_EpmAssignment.ResourceUID = dbo.MSP_EpmResourceByDay.ResourceUID AND
dbo.MSP_EpmAssignmentByDay.TimeByDay = dbo.MSP_EpmResourceByDay.TimeByDay
答案 1 :(得分:0)
我不确定你的牌桌之间的架构和关系是什么,所以我在黑暗中拍摄。
请检查此查询是否以您需要的格式提供结果。
由于LEFT JOIN的使用,您需要检查连接条件是否不会造成不必要的重复。
SELECT
P.ProjectName
, T.TaskName
, R.ResourceName
, RUV.[Resource Manager]
, ABD.AssignmentWork
, TBD.TimeByDay
, RBD.Capacity
FROM dbo.MSP_TimeByDay TBD
-- Query Allocation
LEFT JOIN dbo.MSP_EpmAssignmentByDay ABD ON TBD.TimeByday = ABD.TimeByDay
LEFT JOIN dbo.MSP_EpmAssignment A ON ABD.AssignmentUID = A.AssignmentUID
LEFT JOIN dbo.MSP_EpmResource R ON A.ResourceUID = R.ResourceUID
LEFT JOIN dbo.MSP_EpmResource_UserView RUV ON R.ResourceUID = RUV.ResourceUID
LEFT JOIN dbo.MSP_EpmTask T ON A.TaskUID = T.TaskUID
LEFT JOIN dbo.MSP_EpmProject P ON T.ProjectUID = P.ProjectUID
-- Query Capacity
LEFT JOIN dbo.MSP_EpmResourceByDay RBD ON TBD.TimeByDay = RBD.TimeByDay
WHERE NOT (ABD.AssignmentUID IS NULL) AND NOT (RBD.ResourceUID IS NULL)