How to change this db2 merge query? - sql

can someone help me? i am trying to convert the following merge into another query and i am allowed only to use insert and update once:
MERGE INTO MYEMPLOYEE ME USING EMPLOYEE E ON ME.EMPNO = E.EMPNO
WHEN MATCHED THEN
UPDATE SET ME.SALARY = CASE WHEN ME.SALARY > E.SALARY THEN ME.SALARY ELSE E.SALARY END
WHEN NOT MATCHED THEN
INSERT VALUES(E.EMPNO, E.FIRSTNME, E.MIDINIT, E.LASTNAME, E.WORKDEPT, E.PHONENO, E.HIREDATE, E.JOB, E.EDLEVEL, E.SEX, E.BIRTHDATE, E.SALARY, E.BONUS, E.COMM);
How can I achieve this? the above merge copies the data if not exists, and if it exists it checks for the salary and selects the higher one and copies that one.
how can I achieve the same thing by only using one insert and one update? Can someone give me hints, please?
Thanks in advance :)

The purpose of the MERGE command is suppose to take into account multiple actions of UPDATE, INSERT, DELETE. MERGE statement explained
If you cannot/unable to use MERGE, then you have to resort to doing each request individually.
UPDATE MYEMPLOYEE ME
SET ME.SALARY = (
SELECT CASE WHEN ME.SALARY > E.SALARY THEN ME.SALARY ELSE E.SALARY END
FROM EMPLOYEE E
WHERE ME.EMPNO = E.EMPNO
)
WHERE EXISTS(
SELECT 1
FROM EMPLOYEE E
WHERE ME.EMPNO = E.EMPNO
);
Then do an insert where the employee don't exist in the master table.
INSERT INTO MYEMPLOYEE ME
SELECT *
FROM EMPLOYEE E
LEFT OUTER JOIN MYEMPLOEE ME ON E.EMPNO=ME.EMPNO
WHERE ME.EMPNO IS NULL;
If you need to do in one full sweep you can use the IMPORT command. But then you are dealing with files. You would need to export the EMPLOYEE table (probably with salary already formatted) and then import using the INSERT_REPLACE ability.

After trying I noticed that the INSERT code should actually be like this:
1) Don't use the Name ME twice
2) Select the necessary columns because after the join there are twice the column count
INSERT INTO MYEMPLOYEE (
SELECT E.EMPNO, E.FIRSTNME, E.MIDINIT, E.LASTNAME, E.WORKDEPT, E.PHONENO, E.HIREDATE, E.JOB, E.EDLEVEL, E.SEX, E.BIRTHDATE, E.SALARY, E.BONUS, E.COMM
FROM EMPLOYEE E LEFT OUTER JOIN MYEMPLOYEE ME ON E.EMPNO = ME.EMPNO WHERE ME.EMPNO IS NULL
);

Related

How to use UPDATE JOIN correctly?

I tried to update a table row as below:
update EMPLOYEE set Salary=Salary * 1.15 where Dno=1;
which works fine, but I wanted to learn how to use UPDATE JOIN. so I use the Department Name instead of DNo.
I wrote below code:
update EMPLOYEE set Salary=e.Salary * 1.15 from EMPLOYEE e join DEPARTMENT d on e.DNo=d.DNumber where d.DName='Headquarters';
But the result for above code is that the SQL Command updates all the rows in EMPLOYEE table.
Where did I do wrong?
In BigQuery, change this to a filter in the where clause:
update EMPLOYEE e
set Salary = Salary * 1.15
where exists (select 1
from DEPARTMENT d
where d.DNumber = e.DNo and
d.DName = 'Headquarters'
);
The above is standard SQL and should work in any database. If you want a FROM clause, then use:
update EMPLOYEE e
set Salary = e.Salary * 1.15
from DEPARTMENT d
where d.DNumber = e.DNo and
d.DName = 'Headquarters';
There are (at least) two different ways of implementing FROM in an update, the "SQL Server method" and the "Postgres" method. In the SQL Server method, the table being updated should be in the FROM clause. In the "Postgres method", the table cannot be in the FROM clause. BigQuery adheres to the "Postgres method".
The above actually works in both methods, though.
Try to use table alias name in update
update Emp
Set Salary = Emp.Salary * 1.15
From EMPLOYEE As Emp
join DEPARTMENT As dep On Emp.DNo = dep.DNumber
Where dep.DName = 'Headquarters';

SQL Developer : Holding Multiple values in Variable From Query Result

I need help with query for sql developer.The requirement is a select statement which returns ID Column.I need to hold this ids in a variable and fetch the records associated with each id.Thanks for any help.
You can use join statement.
for example:
select id from table_1 join table_2
on table_1.id = table_2.id;
Not possible, as far as I can tell.
But, if it is the same query all over again and you use its result (those ID values you mentioned) in another queries, then you could create a view
create or replace view v_id as
select id
from some_table
where some_conditions;
and then use that view as yet another data source, e.g.
select d.dname, e.ename, e.job, e.sal
from emp e join v_id v on v.id = e.empno
join dept d on d.deptno = e.deptno;
or
select e.ename, e.job, e.sal
from emp e
where e.empno in (select v.id from v_id v);
or any other option you might choose.

SQL: delete parent when child does not exist

I have two tables:
tblEmployee
EmployeeID (PK), Name, Age, Department
tblDesignation
EmployeeID(FK), Designation, DesignationID
I need help to write a stored procedure to delete an employee record only if it does not have a record in the designation table. (delete employeeID only if there is no designation assigned)
I am not sure if I should use the ON DELETE RESTRICT constraint or NOT EXISTS clause.
Well you can have below code for your purpose. Please excuse with syntax
create procedure usp_deleteemployee
as
begin
delete from tblEmployee
where id not in (select empid from tblDesignation);
end
Or you can try with exist also. Not sure about syntax
DELETE tblEmployee
FROM tblEmployee
WHERE NOT EXISTS (SELECT 1
FROM tblDesignation
WHERE tblEmployee.Id = tblDesignation.empid)
You could try something like below.
DELETE
E
FROM
tblEmployee E
LEFT JOIN tblDesignation D
ON E.EmployeeID = D.EmployeeID
WHERE
D.EmployeeID IS NULL
Hope this Helps!!

Oracle IN vs Exists difference?

I am confused with oracle IN and EXISTS. I have below requirement.
I need to get all the employees whose names are in-
select * from emp where ename in('smith','brown','john','johnson');
Can i use EXISTS here? Also IN clause has 1000 limitation. Does EXISTS also has any such limitation?
Thanks!
simply put, EXISTS is usually used for checking whether rows that meet a criteria exist in another (or the same) table.
your SQL using EXISTS would look like this:
select *
from emp e
where exists(select * from emp e2 where e.empno = e2.empno and e2.ename in ('smith', 'brown', 'john', 'johnson'))
so you can see it's not what you need here
IN picks the list of matching values. EXISTS returns the boolean values like true or false. Exists is faster than in.
Example
IN
select ename from emp e where mgr in(select empno from emp where ename='KING');
EXISTS
select ename from emp e
where exists (select 1 from emp where e.mgr = empno and ename = 'KING');

SQL query how to print out manager name?

I need to create a query to display employee name and number along with their super's name and super number. Listing should also include employees who don't have any supervisor.
Select e.ename,e.empno,super.ename,super from emp e;
I don't know how to print out the manager/supervisor name, i just need that.
Join the table to itself using an outer join:
select e.ename, e.empno, super.ename, super.empno
from emp e
left join emp super on super.empno = e.super_empno
The left join will still return rows from emp that do not have a super defined.
EDIT
Due to OP's comment, here's how to do it without a join (using a nasty correlated sub-query):
select
ename,
empno,
(select super.ename from emp where empno = e.super_empno) as super_name,
super_empno
from emp e;