Sql 用oracle中的上一个表达式替换表值

Sql 用oracle中的上一个表达式替换表值,sql,oracle,join,subquery,dml,Sql,Oracle,Join,Subquery,Dml,好的,我有下面的场景 我有一张名为employees的表格,在以下情况下,我必须替换其中一些人的姓氏: 1-姓氏只能替换为在牛津工作的员工。 2-他们的新姓氏将是员工编号为-1的人员的姓氏(例如,员工#173现在应改为员工#172姓氏) 我是这样开始查询的: select last_name,num_emp from employees where num_emp in( select e.num_emp from employees join departments using(nu

好的,我有下面的场景

我有一张名为employees的表格,在以下情况下,我必须替换其中一些人的姓氏: 1-姓氏只能替换为在牛津工作的员工。 2-他们的新姓氏将是员工编号为-1的人员的姓氏(例如,员工#173现在应改为员工#172姓氏)

我是这样开始查询的:

select last_name,num_emp

from employees

where num_emp in(

select e.num_emp

from employees

join departments using(num_dept)

join office using(id_office)

where city='Oxford')
我做了第二个查询以确定哪些值将替换哪些值

Select last_name,num_emp

from employees

where num_emp in(

select (e.num_emp-1)

from employees

join departments using(num_dept)

join office using(id_office)

where city='Oxford')
现在我想我可以做到这一点,使代码工作。。。但它没有:

update employees

set last_name=(select last_name

from employees

where num_emp in(

select (e.num_emp-1)
from employees

join departments using(num_dept)

join office using(id_office)

where city='Oxford')
说SQL命令意外结束时出错

所以我考虑做一个改变,因为我认为集合中有太多的值不是重点,下面是我上次是如何做的:

update employees

set last_name=(select last_name
from employees)

where  num_emp =(

select (e.num_emp-1)
from employees

join departments using(num_dept)

join office using(id_office)

where city='Oxford')

得到一个错误,说是缺少右括号,我知道它不能表达问题是什么。我知道我遗漏了一些东西,sintaxis的一部分是错误的,我可能需要创建另一个表并添加这些值,以便将它们保存在那里,我可以将它们与原始值进行比较,但此时我完全被阻止,无法发现我所犯的错误。请帮帮我,我真的很感激

您对更新内容和语句中的更新内容感到困惑

下面是要更新的内容。我在从句中使用IN来表达清楚。EXISTS条款也适用

update employees
set last_name = ...
where num_dept in
(
  select num_dept
  from departments
  where id_office in
  (
    select id_office
    from office
    where city = 'Oxford'
  )
);
以下是要更新的内容:

set last_name =
(
  select last_name
  from employees prev_employee
  where prev_employee.num_emp = employee.num_emp - 1
)

您应该使用分析
lag
功能,然后,如果员工172不存在,您还需要在173中填入员工171的姓名,那么您也可以填补空白

你的选择应该是这样的

with new_emp as
   (select last_name,lag(last_name, 1, 0) over (order by num_emp) as new_last_name, num_emp
    from employees)
select *
from new_emp
where num_emp in(
   select e.num_emp
   from employees e
   join departments using(num_dept)
   join office using(id_office)
   where city='Oxford');
此选择将为您提供原始姓氏、新姓氏和员工编号

然后,您的更新应如下所示:

update employees x
set last_name = (with new_emp as
                  (select last_name,lag(last_name, 1, 0) over (order by num_emp) as new_last_name, num_emp
                   from employees)
                select new_last_name
                from new_emp ne
                where ne.num_emp = x.num_emp)
where x.num_emp in(
   select e.num_emp
   from employees e
   join departments using(num_dept)
   join office using(id_office)
   where city='Oxford');

托尔斯滕·凯特纳!!!非常感谢这正是我想要的!!!是的,我完全被搞糊涂了,我在这方面做了太多的改变,以至于我没有意识到我只需要保持简单就可以了!!:D通知它:我只是去一个错误,说不能udpate(table.name…)为null,里面似乎有一个值是null,我怎么能改变它使其工作?我看到这个错误发生的唯一原因是第一条记录需要更新,当然之前没有记录。因此,不能满足要求,语句失败是正确的。要避免这种情况,您可以:1)使姓氏为空,或2)更改规则(例如,当之前没有记录时,请保留该名称),并相应地更改语句。好的,太好了!!我可以通过更改姓氏nullable:)来解决这个问题,再次感谢!