2016-07-07 50 views
5

更新員工的標題我有2個表:SQL - 根據最近的職位

表1

CREATE TABLE #Table1(
    EMPLOYEEID [nvarchar] (100) NULL, 
    Name [nvarchar] (100) NULL, 
    TITLE [nvarchar] (100) NULL) 

    INSERT INTO #Table1 (EMPLOYEEID, Name, TITLE) VALUES ('1','Tom','System Admin'), ('2','Bill','Server Admin'), ('3','John','Senior Server Admin'), ('4', 'Bob', 'IT Staff') , ('5', 'Jill', 'Senior IT Staff') 

這給了我:

select * from #Table1 
+------------+-------+---------------------+ 
| EMPLOYEEID | Name | Title    | 
+--------------------+---------------------+ 
| 1   | Tom | System Admin  | 
| 2   | Bill | Server Admin  | 
| 3   | John | Senior Server Admin | 
| 4   | Bob | IT Staff   | 
| 5   | Jill | Senior IT Staff  | 
+--------------------+---------------------+ 

表2

CREATE TABLE #Table2(
    EMPLOYEEnumber [nvarchar] (100) NULL, 
    Name [nvarchar] (100) NULL, 
    positiontitle [nvarchar] (100) NULL, 
    datepositionstart [date] NOT NULL) 

    INSERT INTO #Table2 (EMPLOYEEnumber, Name, positiontitle, datepositionstart) VALUES ('1','Tom','System Admin', '2014-07-03'), ('1','Tom','Server Admin', '2014-08-13'), ('1','Tom','Senior Server Admin', '2016-07-23'), ('2', 'Bob', 'IT Staff', '2014-07-03') , ('2', 'Bob', 'Senior IT Staff', '2016-10-15') 

這給了我:

select * from #Table2 
    +----------------+-------+---------------------+-----------------+ 
    | EMPLOYEEnumber | Name | positiontitle  |datepositionstart| 
    +----------------+-------+--------------------+-----------------+ 
    | 1    | Tom | System Admin  | 2014-07-03  | 
    | 2    | Tom | Server Admin  | 2014-08-13  | 
    | 3    | Tom | Senior Server Admin | 2016-07-23  | 
    | 4    | Bob | IT Staff   | 2014-07-03  | 
    | 4    | Bob | Senior IT Staff  | 2016-10-15  | 
    +------------------------+---------------------+-----------------+ 

如何根據表2中最新的職位更新table1員工職稱?

下面的代碼並沒有得到最近的日期,而不是我讓員工的副本從表2與同datepositionstart。

UPDATE Table1 
    SET E.TITLE = hre.[PositionTitle] 
    FROM Table1 E 
    JOIN Table2 hre on hre.EmployeeNumber = E.EMPLOYEEID 
    WHERE 
     E.Name = hre.Name 
     AND datepositionstart = 
      (SELECT MAX(datepositionstart) 
       FROM table2 hre 
       WHERE hre.EmployeeNumber = E.EMPLOYEEID) 

預期結果:

+------------+-------+---------------------+ 
| EMPLOYEEID | Name | Title    | 
+--------------------+---------------------+ 
| 1   | Tom | Senior Server Admin | 
| 2   | Bill | Server Admin  | 
| 3   | John | Senior Server Admin | 
| 4   | Bob | Senior IT Staff  | 
| 5   | Jill | Senior IT Staff  | 
+--------------------+---------------------+ 
+0

祝所有SQL問題進行了格式化這個樣子!你爲此贏得了讚賞。 –

+0

是的,您在表格2中的數據看起來不正確... –

+1

不,不要這樣做。改爲創建一個視圖。 (或者,也許有一個計算列。)但是爲什麼要存儲兩次相同的數據,從而導致數據不一致? – jarlh

回答

4

使用row_number(),以確定每個員工最近的標題:

update 
    set title = t2.positiontitle 
from table1 t1 join 
    (select t2.*, row_number() over (partition by employeenumber order by datepositionstart desc) as seqnum 
     from table2 t2 
    ) t2 
    on t1.employeeid = t2.employeenumber and seqnum = 1; 
2

嘗試這樣的事情。

Update T1 Set T1.Title = T2.PositionTitle 
FROM Table1 T1 
JOIN 
(
    SELECT EmployeeNumber,PositionTitle, Max(datepositionstart) AS datepositionstart 
    FROM Table2 group by EmployeeNumber,PositionTitle 
) T2 
on T1.EMPLOYEEID = T2.EmployeeNumber 
2
UPDATE e 

SET E.TITLE = t.[PositionTitle] 

FROM #Table1 e 
JOIN #Table2 t ON t.EMPLOYEEnumber= e.EMPLOYEEID 
WHERE t.datepositionstart = 
      (SELECT MAX(datepositionstart) 
       FROM #table2 hre 
       WHERE hre.EmployeeNumber = E.EMPLOYEEID 
       GROUP BY hre.EMPLOYEEnumber) 

我所做的假設是table2.EMPLOYEEnumber是一個外鍵它引用table1.EMPLOYEEID

預期的數據沒有實現,因爲最初插入到表2創建如下:

EMPLOYEEnumber Name positiontitle  datepositionstart 
1    Tom  System Admin  2014-07-03 
1    Tom  Server Admin  2014-08-13 
1    Tom  Senior Server Admin 2016-07-23 
2    Bob  IT Staff   2014-07-03 
2    Bob  Senior IT Staff  2016-10-15 

我不會用名稱作爲連接表尤其是歷史表的手段,因爲人們可以做改變自己隨着時間的推移。

2

試試這個,希望這可以幫助你,

Update T1 Set T1.Title = d.PositionTitle 
    FROM #Table1 T1 
    JOIN 
    (
     select EMPLOYEEnumber,positiontitle,datepositionstart 
     ,ROW_NUMBER() over (PARTITION BY EMPLOYEEnumber order by datepositionstart desc) rn 
     from #Table2 
    ) d 
    on T1.EMPLOYEEID = d.EmployeeNumber and d.rn = 1