2013-05-19 246 views
1

表1:SQL:避免重複數據

Person_ID Name Salary_Revisions 
1   Test1 100 
1   Test1 200 
2   Test2 300 
2   Test2 400 

表2:

Person ID     Department 
-------------------------- ---------------- 
1       Physics 
1       Chemistry 
2       Maths 

我希望得到的結果一樣:實際

Person_ID    Name    Salary_Revisions  Department 
--------------------- ------------------ ---------------------- -------------- 
1      Test1    100     Physics 
1      Test1    200     Chemistry 
2      Test2    300     Maths 
2      Test2    400 

Person ID   Name  Salary Revisions  Department 
------------------ --------- --------------------- ---------------- 
1     Test1  100     Physics 
1     Test1  200     Physics 
1     Test1  100     Chemistry 
1     Test1  200     Chemistry 
2     Test2  300     Maths 
2     Test2  400     Maths 

您能否幫我實施預期的結果?

在執行此操作時,我使用person id將表1與Table 2左連接來編寫存儲過程。通過在數據庫中執行查詢它返回像實際結果。

SQL查詢:

SELECT table1.person_ID, table1.name, table1.salary_revisions, table2.department 
from table1 
left outer join table2 on table1.person_id=table2.person_id 
+3

「Salary_Revisions = 400」的行來自哪裏?似乎沒有在基表中......你需要**解釋你的邏輯** - 從你在這裏給我們顯示的那些數據來看,這並不清楚...... –

+0

你如何確定人1在物理系有100個工資修改,而不是在化學系? – Andomar

+0

對不起。我錯過了salary_revisions = 400。我已更正了這些數據。 – user2395176

回答

1

鑑於你的評價是,是修訂和部門之間沒有關係的話很有道理使用列表。逗號分隔看起來不錯。就像這樣:

PERSON_ID NAME SALARY REVISION LIST DEPARTMENT LIST 
---------- ------ --------------------- ------------------------ 
1   Test1 100, 200    Physics, Chemistry 
2   Test2 300, 400    Maths 

下面是該查詢:

SELECT DISTINCT 
     Person_ID, 
     Name, 
     STUFF((SELECT ', ' + CAST(S.Salary_Revisions AS VARCHAR(50)) 
       FROM Table1 S 
       WHERE S.Person_ID = P.Person_ID 
       FOR XML PATH ('')),1,2,'') AS [Salary Revision List], 
     STUFF((SELECT ', ' + D.Department 
       FROM Table2 D 
       WHERE D.Person_ID = P.Person_ID 
       FOR XML PATH ('')),1,2,'') AS [Department List] 
FROM Table1 P 

這裏是小提琴: http://sqlfiddle.com/#!3/27c07/3/0


原來的答案

嗯,這很容易做到但是有一部分商業規則沒有道理。爲什麼這一行沒有一個部門?

Person_ID    Name    Salary_Revisions  Department 
--------------------- ------------------ ---------------------- -------------- 
2      Test2    400 

有關於表2什麼,意味着用戶2之間的300和用戶2400的差異。

在您的系統的數據意味着您展示的結果是正確的 - 無法結果也是:

Person_ID    Name    Salary_Revisions  Department 
--------------------- ------------------ ---------------------- -------------- 
1      Test1    100     Physics 
1      Test1    200     Chemistry 
2      Test2    300      
2      Test2    400     Maths 

Person_ID    Name    Salary_Revisions  Department 
--------------------- ------------------ ---------------------- -------------- 
1      Test1    100     Physics 
1      Test1    200     Chemistry 
2      Test2    300     Maths 
2      Test2    400     Maths 

如果你沒有數據邏輯上選擇正確的,沒有辦法執行此查詢。

也許行的順序很重要(這對SQL來說很奇怪)。也許有一些數據沒有放入您需要的數據模型中?

+0

我想得到一個表示。我的意思是像Person_ID 1有兩個薪水修訂和兩個部門。 Person_ID 2有兩個薪金修訂和一個部門。我知道這裏沒有辦法從邏輯上獲取數據。但我想獲得一個代表。謝謝 – user2395176

+0

@ user2395176 - 好的,查看更新以選擇列表。 – Hogan

1

我不太明白使用情況,但我認爲這是你想要的。

兩個CTE的獲得,每人每列的所有唯一值,和FULL OUTER JOIN結合他們逐行。

WITH salary_revision AS (
    SELECT person_id, name, salary_revisions sr, 
    ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY person_id) row 
    FROM table1 
), department AS (
    SELECT person_id, department, 
    ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY person_id) row 
    FROM table2 
) 
SELECT sr.person_id,sr.name, sr.sr, d.department 
FROM salary_revision sr 
FULL OUTER JOIN department d 
    ON sr.person_id = d.person_id 
AND sr.row = d.row 

An SQLfiddle to test with

+0

嗨Joachim,我想從表2中的所有部門,但不重複,不是最大(部門)。謝謝 – user2395176

+0

@ user2395176請參閱更新。 –

+0

嗨Joachim,我想加入後得到所有person_ids。如果我的表2包含3個person_ID 1部門,那麼我該如何繼續?請.... – user2395176

-1
WITH salary_revision AS (
    SELECT person_id, name, salary_revisions sr, 
    ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY person_id) row 
    FROM table1 
    ), department AS (
    SELECT person_id, department, 
    ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY person_id) row 
    FROM table2 
    ) 

SELECT COALESCE(sr.person_id,d.person_id),sr.name,sr.sr,d.department FROM salary_revision SR FULL OUTER JOIN部門d ON sr.person_id = d.person_id AND sr.row = d.row

在給予COALESCE它考慮第一個非空值。所以我們在每一行都得到了peson_id。