對於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
嗨SimplyInk,我感謝幫助!我試過了你的查詢,並且它繼續運行超過五分鐘,所以我停下了它。當查詢分配創建大約900,00行時,檢索約2000萬行。所以你可能是正確的創建重複,因爲我使用了左加入。我還能怎樣幫助你?再次感謝。 –
嗨保羅,對於1:1關係的表,您應該可以執行JOIN而不是LEFT JOIN來消除一些不必要的重複。 – SimplyInk
如果您有權訪問MS SQL Server Management Studio,則可以查看錶和數據之間的關係:右鍵單擊dbo.MSP_TimeByDay表,編輯前200行,在查詢上單擊「顯示圖表窗格(Ctrl + 1)」按鈕設計器工具欄。點擊同一工具欄上的「添加表格」按鈕,逐個添加其他表格。確保在添加每個表之前調整連接條件。 (這是一個查看和編輯表格數據的界面,所以一定不要更改數據或在測試數據庫上試試!) – SimplyInk