如何将每个 'SAL' 列值增加 100(100、200、300,...等等)?
How to increase each 'SAL' column value by 100 (100, 200, 300,... and so on)?
我有一个任务需要将 'SAL' 下的每一行值增加 100。从第一个条目开始到最后一个条目。
我已经将代码写入 select 正确的条目并对它们进行排序,但无法找到增加 'SAL' 列的值的方法。
我正在使用 https://livesql.oracle.com/ 沙箱来尝试学习 SQL
SELECT ROWNUM, T1.*, T2.DNAME, T2.LOC
FROM SCOTT.EMP T1, SCOTT.DEPT T2
WHERE T1.DEPTNO = T2.DEPTNO
AND DNAME = 'SALES'
AND JOB NOT LIKE 'MANAGER'
ORDER BY HIREDATE DESC
最后一行无效
SET SAL = SAL + (ROWNUM * 100)
这个呢?
UPDATE (
SELECT ROWNUM AS R, T1.*, T2.DNAME, T2.LOC
FROM SCOTT.EMP T1
JOIN SCOTT.DEPT T2 ON T1.DEPTNO = T2.DEPTNO
WHERE DNAME = 'SALES'
AND JOB NOT LIKE 'MANAGER'
ORDER BY HIREDATE DESC)
SET SAL = SAL + (R * 100);
除了ROWNUM AS R
你也可以使用ROW_NUMBER() OVER (ORDER BY HIREDATE DESC) AS R
(这样你就不需要ORDER BY HIREDATE DESC
)
ROWNUM
和 ORDER BY
可能会产生意想不到的结果,因为它们是在 SELECT 语句的处理步骤中 ORDER BY
之前生成的。因此,更喜欢对 UPDATE
操作使用 ROW_NUMBER()
函数(通过内联视图的 UPDATE
不应包含 ROW_NUMBER()
等分析函数。因此,您不能使用当前的方法,但使用下面的方法):
UPDATE EMP e
SET e.SAL = nvl(e.SAL,0) +
(
SELECT SAL_ADD
FROM
( SELECT 100 * ROW_NUMBER() OVER (ORDER BY HIREDATE DESC) as SAL_ADD,
T1.EMPNO
FROM EMP T1
JOIN DEPT T2
ON T1.DEPTNO = T2.DEPTNO
WHERE DNAME = 'SALES'
AND JOB NOT LIKE 'MANAGER'
) tt
WHERE e.EMPNO = tt.EMPNO );
哦!这个答案被接受,但让我再考虑一下 - MERGE INTO
MERGE INTO SCOTT.EMP T11
USING (SELECT T1.EMPNO , ROW_NUMBER() OVER (ORDER BY T1.HIREDATE DESC) as RN
FROM SCOTT.EMP T1 JOIN SCOTT.DEPT T2
ON (T1.DEPTNO = T2.DEPTNO)
WHERE DNAME = 'SALES'
AND JOB NOT LIKE 'MANAGER') T22
ON (T11.EMPNO = T22.EMPNO)
WHEN MATCHED THEN
UPDATE SET T11.SAL = T11.SAL + (RN*100);
干杯!!
我有一个任务需要将 'SAL' 下的每一行值增加 100。从第一个条目开始到最后一个条目。
我已经将代码写入 select 正确的条目并对它们进行排序,但无法找到增加 'SAL' 列的值的方法。 我正在使用 https://livesql.oracle.com/ 沙箱来尝试学习 SQL
SELECT ROWNUM, T1.*, T2.DNAME, T2.LOC
FROM SCOTT.EMP T1, SCOTT.DEPT T2
WHERE T1.DEPTNO = T2.DEPTNO
AND DNAME = 'SALES'
AND JOB NOT LIKE 'MANAGER'
ORDER BY HIREDATE DESC
最后一行无效
SET SAL = SAL + (ROWNUM * 100)
这个呢?
UPDATE (
SELECT ROWNUM AS R, T1.*, T2.DNAME, T2.LOC
FROM SCOTT.EMP T1
JOIN SCOTT.DEPT T2 ON T1.DEPTNO = T2.DEPTNO
WHERE DNAME = 'SALES'
AND JOB NOT LIKE 'MANAGER'
ORDER BY HIREDATE DESC)
SET SAL = SAL + (R * 100);
除了ROWNUM AS R
你也可以使用ROW_NUMBER() OVER (ORDER BY HIREDATE DESC) AS R
(这样你就不需要ORDER BY HIREDATE DESC
)
ROWNUM
和 ORDER BY
可能会产生意想不到的结果,因为它们是在 SELECT 语句的处理步骤中 ORDER BY
之前生成的。因此,更喜欢对 UPDATE
操作使用 ROW_NUMBER()
函数(通过内联视图的 UPDATE
不应包含 ROW_NUMBER()
等分析函数。因此,您不能使用当前的方法,但使用下面的方法):
UPDATE EMP e
SET e.SAL = nvl(e.SAL,0) +
(
SELECT SAL_ADD
FROM
( SELECT 100 * ROW_NUMBER() OVER (ORDER BY HIREDATE DESC) as SAL_ADD,
T1.EMPNO
FROM EMP T1
JOIN DEPT T2
ON T1.DEPTNO = T2.DEPTNO
WHERE DNAME = 'SALES'
AND JOB NOT LIKE 'MANAGER'
) tt
WHERE e.EMPNO = tt.EMPNO );
哦!这个答案被接受,但让我再考虑一下 - MERGE INTO
MERGE INTO SCOTT.EMP T11
USING (SELECT T1.EMPNO , ROW_NUMBER() OVER (ORDER BY T1.HIREDATE DESC) as RN
FROM SCOTT.EMP T1 JOIN SCOTT.DEPT T2
ON (T1.DEPTNO = T2.DEPTNO)
WHERE DNAME = 'SALES'
AND JOB NOT LIKE 'MANAGER') T22
ON (T11.EMPNO = T22.EMPNO)
WHEN MATCHED THEN
UPDATE SET T11.SAL = T11.SAL + (RN*100);
干杯!!