Question

Ok so I am having the following scenario

I have a table called employees and have to replaced the last names for some of the people there under the following conditions: 1-The last name must be replaced only to those employees who work on Oxford. 2-Their new last name is going to be the last name of the person that has their employee number -1 ( for instance employee#173 should have now employee#172 last name instead)

This is how I started the query:

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')

And I did a second query to make sure which values were going to replace which

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')

Now I thought I could do this and make the code work... but it didn't:

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')

Got error saying unexpected end of SQL command...

So I thought on making a change because I believed having too many values on the set was not the point and here is how I did it for last time:

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')

Got an error that says is missing right parenthesis, which I know it does not express whaat the issue is. I know I am missing something and part of the sintaxis is wrong as well as I may need to créate another table and add those values so that they get saved there and I can compare them with the original ones, but at this point I am totally blocked and can't discover what is the mistake I am doing. Please help me I'd really apprecciate it!

Was it helpful?

Solution

You are getting confused with what to update and what to update with in your statements.

Here is what to update. I use IN clauses to make it plain. An EXISTS clause would also be appropriate.

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'
  )
);

And here is what to update with:

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

OTHER TIPS

You should use the analytical lag function, then you also fill in gaps if for example employee 172 doesn't exist and you have to put the name of employee 171 in 173.

Your select should be something like this

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');

This select will give you the original last name, new last name, employee number.

Then afterwards your update should be this:

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');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top