2014-08-29 112 views
-1

'我有一個數據庫,員工被分配到不同的項目。有些項目分爲不同的任務。以下數據庫結構已創建。'加入具有相同外鍵的多個表

USER: 
+------------+-------------+ 
| Person ID | Name  | 
+------------+-------------+ 
| 1   | A   | 
| 2   | B   | 
| 3   | C   | 
+------------+-------------+ 
PROJECT_ASSIGNMENT: 
+-----------------+-------------+-----------+ 
| P_Assign key | Project Key | Person ID | 
+-----------------+-------------+-----------+ 
| 1    | 1   | 1   | 
| 2    | 1   | 2   |  
| 3    | 1   | 3   |  
+------------+-------------+------+-----+---- 
TASK ASSIGNMENT: 
+-----------------+-------------+-----------+--------+ 
| T_Assign key | Project Key | Person ID |task key| 
+-----------------+-------------+-----------+--------+ 
| 1    | 2   | 1   | 1  | 
| 2    | 2   | 2   | 1  | 
| 3    | 2   | 3   | 1  | 
| 4    | 2   | 1   | 2  | 
| 5    | 2   | 2   | 2  | 
| 6    | 2   | 3   | 2  | 
+------------+-------------+------+-----+------------+; 

'What type of query would be required to get results like this?' 

+-----------------+-------------+-----------+ 
| Person ID  | Project Key | Task Key | 
+-----------------+-------------+-----------+ 
| 1    | 1   | NULL  | 
| 1    | 2   | 1   | 
| 1    | 2   | 2   | 
| 2    | 1   | NULL  | 
| 2    | 2   | 1   | 
| 2    | 2   | 2   | 
| 3    | 1   | NULL  | 
| 3    | 2   | 1   | 
| 3    | 2   | 2   | 
+------------+-------------+------+-----+---+ 

我已經嘗試了一些查詢,但我認爲有相同的外鍵防止我得到我需要的結果。

SELECT 
user.person_ID, 
task_assignment.Project Key, 
project_assignment.Project Key 
FROM user 
JOIN dbo.project_assignment 
    ON project_assignment.person_key=user.person_key 
LEFT JOIN dbo.task_assignment 
    ON dbo.project_assignment.person_key=dbo.task_assignment.person_key 

如果一個項目被分成任務,該人被分配到該任務。 如果某個項目未分解成任務,則該人被分配到該項目。

+2

你嘗試過什麼?請包括示例代碼來證明您已經付出了一些努力來解決此問題。 – nrussell 2014-08-29 18:26:45

回答

0

給出樣本數據,爲了獲得相同的確切結果,我們需要結合兩個查詢。

第一個查詢:

SELECT u.person_id 
    , p.project_key 
    , t.task_key 
    FROM user u 
    JOIN project_assignment p 
    ON p.person_id = u.person_id 
    LEFT 
    JOIN task_assignment t 
    ON t.person_id = p.person_id 
    AND t.project_key = p.project_key 
ORDER 
    BY u.person_id 
    , p.project_key 

將返回:

person_id project_key task_key 
--------- ----------- -------- 
1   1   NULL 
2   1   NULL 
3   1   NULL 

而第二個查詢:

SELECT u.person_id 
    , t.project_key 
    , t.task_key 
    FROM user u 
    JOIN task_assignment t 
    ON t.person_id = u.person_id 
ORDER 
    BY u.person_id  
    , t.project_key 
    , t.task_key 

返回結果:

person_id project_key task_key 
--------- ----------- -------- 
1   2   1 
1   2   2 
2   2   1 
2   2   2 
3   2   1 
3   2   2 

這兩個查詢的結果可以用UNION ALL集合運算符進行組合,然後加上一個ORDER BY子句,例如:

SELECT u.person_id 
    , p.project_key 
    , t.task_key 
    FROM user u 
    JOIN project_assignment p 
    ON p.person_id = u.person_id 
    LEFT 
    JOIN task_assignment t 
    ON t.person_id = p.person_id 
    AND t.project_key = p.project_key 
UNION ALL 
SELECT u.person_id 
    , t.project_key 
    , t.task_key 
    FROM user u 
    JOIN task_assignment t 
    ON t.person_id = u.person_id 
ORDER 
    BY person_id 
    , project_key 
    , task_key IS NOT NULL 
    , task_key 

有一些案件中沒有涉及的示例數據.. 。如果對於給定的用戶,在project_assignment表和task_assignment表中都出現相同的項目鍵。輸出應該是什麼樣子? (有可能是查詢中做了一些調整。)

0

即使你沒有真正展示你所做的。對於這種情況的一個例子查詢:

SELECT p.P_Assign key, p.Project Key, p.Person I FROM PROJECT_ASSIGNMENT p 
INNER JOIN TASK ASSIGNMENT t 
ON p.Person_ID=t.Person_ID 

假設person_id是project_assignment和任務分配的外鍵的主鍵。

這將加入前兩個。在你的select語句中指定你想要在結果中顯示的列。

0

我沒有實際的想法,你想要的這裏,但我想可能是你正在尋找此查詢:

SELECT PersonId, ProjectKey, TaskKey 
FROM TASK_ASSIGNMENT 
GROUP BY PersonId, ProjectKey, TaskKey; 

希望這有助於。

0

嘗試如下....

Select 
User.Person_ID, 
all_tasks.Project_key, 
all_tasks.Task_Key 
From User 
Left Join 
(
Select Person_ID, Project_Key, Null as Task_Key From Project_Assignment 
Union All 
Select Person_ID, Project_key, Task_Key From Task_Assignment 
) all_tasks On all_tasks.person_ID = User.Person_ID 
相關問題