SQL - 根据最近的位置更新员工标题

时间:2016-07-07 12:32:31

标签: sql sql-server

我有两张桌子:

表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员工职称?

以下代码没有获取最新日期,而是从Table2获得具有相同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     |
+--------------------+---------------------+

4 个答案:

答案 0 :(得分:4)

使用rowHeight确定每位员工的最新职称:

row_number()

答案 1 :(得分:3)

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的外键。

未达到预期的数据,因为初始插入到table2中会创建以下内容:

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 :(得分: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

答案 3 :(得分:2)

试试这个,希望这可以帮到你。

public int onStartCommand(Intent intent, int flags, int startId)