2015-06-22 57 views
2

對於Sql Server來說很新,我希望在匹配兩列時加入兩個查詢。例如。如果查詢容量中的列TimeByDay & ResourceName與查詢分配中的列TimeByDay和ResourceName相匹配,然後提取Capacity中的值。歡迎任何其他建議。如果我不清楚,請告訴我。謝謝。SQL Server - 使用兩列中的匹配值連接查詢

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

我不知道你是什麼表之間的模式和關係,所以我走在黑暗中拍攝。

請檢查此查詢是否以您需要的格式提供結果。

由於使用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) 
+0

嗨SimplyInk,我感謝幫助!我試過了你的查詢,並且它繼續運行超過五分鐘,所以我停下了它。當查詢分配創建大約900,00行時,檢索約2000萬行。所以你可能是正確的創建重複,因爲我使用了左加入。我還能怎樣幫助你?再次感謝。 –

+0

嗨保羅,對於1:1關係的表,您應該可以執行JOIN而不是LEFT JOIN來消除一些不必要的重複。 – SimplyInk

+0

如果您有權訪問MS SQL Server Management Studio,則可以查看錶和數據之間的關係:右鍵單擊dbo.MSP_TimeByDay表,編輯前200行,在查詢上單擊「顯示圖表窗格(Ctrl + 1)」按鈕設計器工具欄。點擊同一工具欄上的「添加表格」按鈕,逐個添加其他表格。確保在添加每個表之前調整連接條件。 (這是一個查看和編輯表格數據的界面,所以一定不要更改數據或在測試數據庫上試試!) – SimplyInk

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