how to update table from an external table - sql

I want to update a employee table based on my external table, but I got ORA-01427 error,
single-row subquery returns more than one row
employee(emp_id, emp_name, job_history, city_code)
ext_table(emp_name, job_history, city_name)
city(city_code, city_name)
data in my ext_table as following:
Sandy, waitress, los angeles
Sandy, restaurant manager, los angeles
John, store manager, phoenix
update employee em
set (em.emp_name, em.job_history, em.city_code) =
(select t.emp_name, t.job_history, t.city_code
from (select distinct(emp_name), job_history, c.city_code from
ext_table e, city c where e.city_name=c.city_name) t)
where em.emp_name=t.emp_name;
I am greatly appreciate for any help

This is what MERGE is for:
merge into employee
using
(
select e.emp_name, e.job_history, c.city_code
from ext_table e
join city c on e.city_name=c.city_name
) t on (t.emp_name = employee.emp_name)
when matched then update
set job_history = t.job_history,
city_code = t.city_code;
Note that it's useless to update emp_name as that is the column you use to join between ext_table and employee.
The above assumes that emp_name is unique in ext_table (and employee). If this is not the case you will need to find some "key" that uniquely identifies the employee in the external table.
Also: distinct is NOT a function.
select distinct (foo), bar is absolutely identical to select distinct foo, bar. It always operates on all columns. The difference between the two is the same as the difference between select (foo),bar and select foo, bar.

Another possible option is to convert external table to regular table and update. To convert external to regular table use SQL Developer. Steps: 1.Create empty table with the same structure as your external table. 2.Export data from external to reg. table. The process is similar to exporting data from file to Excel.

you can do it by using update query with join . check following code
UPDATE employee SET job_history = B.job_history , city_code =
C.city_code FROM EMPLOYEE as A INNER JOIN ext_table AS B ON
A.emp_name = B.emp_name INNER JOIN city AS C ON B.city_name =
C.city_name

Related

Update column in table2 based on select query which contains count() from table1 using postgresql

Tables
Need to update department table's dcount column based on number of employees working in each department from employee table's dno column.
Tried using
update department set dcount=(select count() from employee INNER JOIN department ON employee.dno=department.dnumber group by dno);*
which gave an error : more than one row returned by a subquery used as an expression
Desired result is:
**dname|dnumber|dcount
Research|5|4
Admin|4|3
Headquarters|1|1**
Need help please.
Thanks in advance.
Gruheeth
Your subquery (select count() ...) returns several rows, one per employee, where postgres expect only one row from this subquery in order to update one row at a time in the department table. In this case, you case use a cte instead :
WITH list AS
(
select dno, count(*) AS dno_count
from employee
group by dno
)
update department AS d
set dcount = l. dno_count
from list AS l
where d.dnumber = l.dno ;

Microsoft SMSS - Joining tables on t2.primary key = t1.foreign_key

I need to join two tables together to create a table with columns for employee id, employee name and their boss' name.
The 'hier' table
The 'employees' table
The query I wrote is almost working, putting an employee name in the right spot, but not the right employee:
SELECT em.emp_id, em.emp_name, em.emp_name AS boss_name
FROM employees em
LEFT JOIN hier h ON (h.boss_id = em.emp_name)
Which outputs:
I need to have each person's boss to have the right name, and in the case of Big Boss, 'N/A'. Like so:
You need a self join with Employee table
SELECT em.emp_id, em.emp_name, e1.emp_name AS boss_name
FROM employees em
LEFT JOIN employees em1 ON em.boss_id = em1.emp_id

Create table as Select, then Update statement with JOIN in Oracle

I'm using Oracle HR database.
I was wondering why the following query isn't working:
create table ecopy
as select *
from employees;
create table dcopy
as select *
from departments;
UPDATE (select d.location_id, e.salary
from ecopy e inner join dcopy d
on e.department_id=d.department_id)
set salary = salary+1
where location_id = 1800
SQL Error: ORA-01779: cannot modify a column which maps to a non key->preserved table
While the this one, on the origin tables is doing it's job:
UPDATE (select d.location_id, e.salary
from employees e inner join departments d
on e.department_id=d.department_id)
set salary = salary+1
where location_id = 1800
Could anyone explain it to me?
Here is the explanation:
In your real life your relation supported by keys - reference constraint
employee.department_id(MANY) = departments.department_id(ONE)
In the case of UPDATE with JOIN, you can update only columns in your "MANY" table and only if they have real reference.
Your Create as select. . . tables definitely don't have these references, hence Oracle optimizer throws this error.
Here are some references
Reference 1
Reference 2

Replacing a table value with the previous expression in oracle

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

Query Multiple Columns Within a Table

I have a table of employees that are formatted as follows:
EMPLOYEE (FNAME,MINIT,LNAME,SSN(PK),BDATE,SUPERSSN(NULLABLE))
I need to query every employee and retrieve the following information:
FNAME(employee),LNAME(employee),SUPERSSN,(super)FNAME,(super)LNAME
UPDATED
After running this query:
SELECT A.FNAME,A.LNAME,A.SUPERSSN,B.FNAME,B.LNAME
FROM EMPLOYEE
A LEFT JOIN EMPLOYEE B
ON A.SUPERSSN = B.SSN;
The results were close, but when the superssn was null (CEO/Boss) it caused the remaining rows to populate as null also and did not populate with the actual supervisors ssn. I'm trying to use an IF statement to fix the problem with having a SuperSSN that is null, but I'm receiving the error: ORA-00905: missing keyword.
Below is the query that I ran that generated the error.
SELECT A.FNAME,A.LNAME,A.SUPERSSN,B.FNAME,B.LNAME
FROM EMPLOYEE A LEFT IF A.SUPERSSN <> 'NULL'
JOIN EMPLOYEE B ON A.SUPERSSN = B.SSN;
Select A.FName,
A.LNAme,
A.SuperSSN,
B.FName,
B.LName
from Employee A
Left Join Employee B
On A.SuperSSN = B.SSN