使用 Microsoft SQL 服务器从另一个 table 更新 table
Updating table from another table based of column using Microsoft SQL Server
嘿,我有两个 table。我需要将数据 (gID) 的等级 table 传输给员工 table (GradeID)。
员工
EmployeeID | Name | mgr | Job | Salary| Comm | HireDate | dID| GradeID
1001 | Susan Adams | 1004| Locksmith| 60000| 20000| 2005-04-12| 1 | NULL
1002 | Ricky Jones | 1004| Writer| 85000 | 15000| 2020-01-18| 1 | NULL
1003 | Beatrice | 1006| Editor| 89000 | NULL | 2002-03-07| 2 | NULL
GradeID| MinSal| MaxSal| Holiday
A | NULL | 60000 | 40
B | 60000 | 80000 | 40
C | 90000 | 100000| 40
D | 200000| 120000| 45
E | 320000| NULL | 55
这是我目前所拥有的
UPDATE EMPLOYEE
SET GradeID = 'A'
WHERE Salary between (SELECT COALESCE(MAX(MinSAL),0) From GRADE WHere gID = 'A') and (SELECT Max (MaxSal) From GRADE where gID = 'A')
UPDATE EMPLOYEE
SET GradeID = 'B'
WHERE Salary between (SELECT MAX(MinSal) From GRADE WHere gID = 'B') and (SELECT Max (MaxSal) From GRADE where gID = 'B')
UPDATE EMPLOYEE
SET GradeID = 'C'
WHERE Salary between (SELECT MAX(MinSal) From GRADE WHere gID = 'C') and (SELECT Max (MaxSal) From GRADE where gID = 'C')
UPDATE EMPLOYEE
SET GradeID = 'D'
WHERE Salary between (SELECT MAX(MinSal) From GRADE WHere gID = 'D') and (SELECT Max (MaxSal) From GRADE where gID = 'D')
UPDATE EMPLOYEE
SET GradeID = 'E'
WHERE Salary between (SELECT MAX(MinSal) From GRADE WHere gID = 'E') and (SELECT COALESCE(MAX(MaxSAL),1000000000000000) From GRADE WHere gID = 'E')
但我需要一个不那么笨拙的单一查询,它可以根据他们的薪水在员工 table 中填充正确的等级。感谢您的帮助。
尝试使用更新连接:
UPDATE e
SET e.GradeID = g.gID
FROM Employee e
INNER JOIN Grade g
ON (e.Salary > g.MinSal OR g.MinSal IS NULL) AND
(e.Salary <= g.MaxSal OR g.MaxSal IS NULL);
嘿,我有两个 table。我需要将数据 (gID) 的等级 table 传输给员工 table (GradeID)。
员工
EmployeeID | Name | mgr | Job | Salary| Comm | HireDate | dID| GradeID
1001 | Susan Adams | 1004| Locksmith| 60000| 20000| 2005-04-12| 1 | NULL
1002 | Ricky Jones | 1004| Writer| 85000 | 15000| 2020-01-18| 1 | NULL
1003 | Beatrice | 1006| Editor| 89000 | NULL | 2002-03-07| 2 | NULL
GradeID| MinSal| MaxSal| Holiday
A | NULL | 60000 | 40
B | 60000 | 80000 | 40
C | 90000 | 100000| 40
D | 200000| 120000| 45
E | 320000| NULL | 55
这是我目前所拥有的
UPDATE EMPLOYEE
SET GradeID = 'A'
WHERE Salary between (SELECT COALESCE(MAX(MinSAL),0) From GRADE WHere gID = 'A') and (SELECT Max (MaxSal) From GRADE where gID = 'A')
UPDATE EMPLOYEE
SET GradeID = 'B'
WHERE Salary between (SELECT MAX(MinSal) From GRADE WHere gID = 'B') and (SELECT Max (MaxSal) From GRADE where gID = 'B')
UPDATE EMPLOYEE
SET GradeID = 'C'
WHERE Salary between (SELECT MAX(MinSal) From GRADE WHere gID = 'C') and (SELECT Max (MaxSal) From GRADE where gID = 'C')
UPDATE EMPLOYEE
SET GradeID = 'D'
WHERE Salary between (SELECT MAX(MinSal) From GRADE WHere gID = 'D') and (SELECT Max (MaxSal) From GRADE where gID = 'D')
UPDATE EMPLOYEE
SET GradeID = 'E'
WHERE Salary between (SELECT MAX(MinSal) From GRADE WHere gID = 'E') and (SELECT COALESCE(MAX(MaxSAL),1000000000000000) From GRADE WHere gID = 'E')
但我需要一个不那么笨拙的单一查询,它可以根据他们的薪水在员工 table 中填充正确的等级。感谢您的帮助。
尝试使用更新连接:
UPDATE e
SET e.GradeID = g.gID
FROM Employee e
INNER JOIN Grade g
ON (e.Salary > g.MinSal OR g.MinSal IS NULL) AND
(e.Salary <= g.MaxSal OR g.MaxSal IS NULL);