PL/SQL help me making this code - oracle

Create PL / SQL blocks using cursors to update salary columns on employee tables, from employees working in specific departments inputted via keyboard.
Salaries are raised by the rules:
If the employee is less than 16 years old, raise the salary 15%
If the length of employment of employees between 16-20 years, increase salary by 20%
If the employee is over 20 years old, raise salary 25%
Show employee_id, last_name, old job, new salary, on updated employees table.
Enter value for d_id: 30
EMPLOYEE_ID LAST_NAME LAMAKERJA SALARY
----------- ------------------------- ---------- ----------
119 Colmenares 16.1452239 3000
118 Himuro 16.8794705 3120
116 Baida 17.7726212 3480
117 Tobias 18.1917992 3360
115 Khoo 20.3781006 3720
114 Raphaely 20.8219362 13200
6 rows selected.

To get you started: age is a common problem so let's suppose that my trivial calculation does the job. Loop through all employees, check conditions you were told and update employee's salary. The final SELECT is up to you, completely.
FOR cur_r
IN (SELECT employee_id, TRUNC ( (SYSDATE - date_of_birth) / 365.25) age
FROM employees)
LOOP
UPDATE employees e
SET e.salary =
e.salary * CASE
WHEN age ... THEN ...
WHEN age ... THEN ...
END
WHERE e.employee_id = ...
END LOOP;

Related

How to use a rowcount in select statement to modify the query to fetch data for 10 days , if rowcount is 0 for 5 days?

I need to modify my script using rowcount to check if the data in table or not?. Here, i write the query to select a data for last 5 days from current system date. But sometimes there is no data in table for 5 days. So i need to fetch for 10 day or more.
Query:
Select ep.ENTERPRISE_NAME||'|'||s.id||'|'||s.SUBMISSION_DATE||'|'||E.VALUE
from JOB_SUMMARY_EXT e, ob_summary s, enterprise ep
where e.id = s.id and e.name_res_key = 'Model'
and s.job_id in (select id from job_summary where
trunc(start_date) > trunc(sysdate) -10 and service_name ='Model2' )
I don't know how to modify my Query using rowcount. If rowcount is 0 then i want select data for 10 days.Otherwise it should to fetch for 5 days automatically. I want this to be done as single query.
It looks that you want to select the last 5 "days" from that table. So, why would you anchor to SYSDATE if there aren't rows for each of those days? I'd suggest another approach: literally, select last 5 days. Here's how.
As I don't have your tables, I'm using Scott's EMP table which contains information about employees. It is an ancient one so HIREDATE column is set to 1980s, but never mind that. Sorting employees by HIREDATE in descending order shows:
SQL> alter session set nls_date_format = 'dd.mm.yyyy';
Session altered.
SQL> select ename, hiredate from emp order by hiredate desc;
ENAME HIREDATE
---------- ----------
ADAMS 12.01.1983 1.
SCOTT 09.12.1982 2.
MILLER 23.01.1982 3.
FORD 03.12.1981 4.
JAMES 03.12.1981 4.
KING 17.11.1981 5. --> I want to fetch rows up to KING
MARTIN 28.09.1981
TURNER 08.09.1981
CLARK 09.06.1981
BLAKE 01.05.1981
JONES 02.04.1981
WARD 22.02.1981
ALLEN 20.02.1981
SMITH 17.12.1980
14 rows selected.
SQL>
As you can see, the 4th date is shared by two employees so I want to include them both. DENSE_RANK analytic function helps:
SQL> with last5 as
2 (select ename,
3 job,
4 sal,
5 hiredate,
6 dense_rank() over (order by hiredate desc) rnk
7 from emp
8 )
9 select ename, job, sal, hiredate
10 from last5
11 where rnk <= 5;
ENAME JOB SAL HIREDATE
---------- --------- ---------- ----------
ADAMS CLERK 1100 12.01.1983
SCOTT ANALYST 3000 09.12.1982
MILLER CLERK 1300 23.01.1982
JAMES CLERK 950 03.12.1981
FORD ANALYST 3000 03.12.1981
KING PRESIDENT 5000 17.11.1981
6 rows selected.
SQL>
What does it do? The LAST5 CTE sorts employees (as above), DENSE_RANK ranks them; finally, the last SELECT (which begins at line #9) fetches desired rows.
In your case, that might look like this:
with last5 as
(select id,
dense_rank() over (order by start_date desc) rnk
from job_summary
where service_name = 'Model2'
)
select ep.enterprise_name,
s.id,
s.submission_date,
e.value
from job_summary_ext e
join ob_summary s on e.id = s.id
join last5 t on t.id = s.id
join enterprise ep on <you're missing join condition for this table>
where e.name_res_key = 'Model';
Note that you're missing join condition for the ENTERPRISE table; if that's really so, no problem - you'd use cross join for that table, but I somehow doubt that you want that.
Finally, as you use SQL*Plus, perhaps you don't need to concatenate all columns and separate them by the pipe | sign - set it as a column separator, e.g.
SQL> set colsep '|'
SQL>
SQL> select deptno, dname, loc from dept;
DEPTNO|DNAME |LOC
----------|--------------|-------------
10|ACCOUNTING |NEW YORK
20|RESEARCH |DALLAS
30|SALES |CHICAGO
40|OPERATIONS |BOSTON
SQL>
If you want to
return 10 last days if select count(*) returns 0, or
return 5 last days if select count(*) returns a positive number
then something like this might help (again based on Scott's EMP table):
with
tcnt as
-- count number of rows; use your own requirement, I'm checking
-- whether someone got hired today. In Scott's EMP table, nobody was
-- so CNT = 0
(select count(*) cnt
from emp
where hiredate >= trunc(sysdate)
)
select e.ename, e.job, e.sal, e.hiredate
from emp e cross join tcnt c
where e.hiredate >= case when c.cnt = 0 then trunc(sysdate) - 10
else trunc(sysdate) - 5
end;
Apply it to your tables; I don't know which of those 3 tables' count you want to check.
Tried to add in comments but it was too long for comments and Not clear on count based on but here is case in where clause substitute your count statement with nvl function
SELECT ep.ENTERPRISE_NAME||'|'||s.id||'|'||s.SUBMISSION_DATE||'|'||E.VALUE
FROM JOB_SUMMARY_EXT e,
ob_summary s,
enterprise ep
WHERE e.id = s.id
AND e.name_res_key = 'Model'
AND s.job_id IN
(SELECT id
FROM job_summary
WHERE service='Model'
AND trunc(start_date) >
CASE WHEN
(WRITE your SELECT COUNT criteria WITH NVL FUNCTION)<=0 THEN
trunc(sysdate) -10
ELSE trunc(sysdate)-5
END )

Reduction columns are not working for group by expression in oracle

I tried to concat two columns and using group by expression, but it is not work. how can I grouped multiple columns in oracle. first_name and last_name are reduction data.
SELECT employee_id,
employee_name,
employee_unique
FROM
(SELECT a.id AS employee_id,
(a.first_name
|| a.last_name) AS employee_name,
b.employee_unique
FROM A a
INNER JOIN b
ON a.id=b.employee_id
GROUP BY a.id,
b.employee_unique,
(a.first_name
|| a.last_name)
);
What does "not work" mean? It works for me (though, as I don't have your tables, I used Scott's EMP and DEPT, but everything else is more or less the same). If it isn't correct, you should explain what is wrong with it. If you want us to work with your data, please, provide CREATE TABLE and INSERT INTO sample data.
SQL> select employee_id,
2 employee_name,
3 employee_unique
4 from
5 (select a.empno as employee_id,
6 a.ename || a.job as employee_name,
7 b.dname as employee_unique
8 from emp a
9 inner join dept b
10 on a.deptno=b.deptno
11 group by a.empno,
12 b.dname,
13 a.ename || a.job
14 );
EMPLOYEE_ID EMPLOYEE_NAME EMPLOYEE_UNIQU
----------- ------------------- --------------
7654 MARTINSALESMAN SALES
7876 ADAMSCLERK RESEARCH
7566 JONESMANAGER RESEARCH
7698 BLAKEMANAGER SALES
7844 TURNERSALESMAN SALES
7369 SMITHCLERK RESEARCH
7788 SCOTTANALYST RESEARCH
7900 JAMESCLERK SALES
7902 FORDANALYST RESEARCH
7782 CLARKMANAGER ACCOUNTING
7934 MILLERCLERK ACCOUNTING
7499 ALLENSALESMAN SALES
7521 WARDSALESMAN SALES
7839 KINGPRESIDENT ACCOUNTING
14 rows selected.
SQL>
Though, as there's nothing really to be grouped (no aggregation here), you could have used distinct (without group by clause) and get the same result:
select employee_id,
employee_name,
employee_unique
from
(select distinct
a.empno as employee_id,
a.ename || a.job as employee_name,
b.dname as employee_unique
from emp a
inner join dept b
on a.deptno=b.deptno
);

Using case update in cursor

I have below 2 tables. One is dept a
DEPT_NO DEPT_NAME DEPT_VALUE
---------- ------------------------------ ----------
10 Chemistry 100
40 Physics 600
20 Mathematics 200
30 Biology 300
50 Cosmos 550
other one is updated_dept b
DEPT DEPT_NAME DEPT_UPDATED_VALUE
---------- ------------------------------ ------------------
10 Chemistry
20 Mathematics
30 Biology
90 Astrology
40 Numerology
50 Cosmos
With the help of the cursor, I want to fetch a.dept_value from 1st table dept a and on basis of (a.dept_no=b.dept and a.dept_name=b.dept_name) update column dept_updated_value in 2nd table updated_dept b using case.
If combination of dept_no and dept_name is not found, I want to update dept_updated_value to 0
I have written below code but it's not giving correct result. Please help
declare
v_dept dept.dept_no%type;
v_dept_name dept.dept_name%type;
v_dept_value dept.dept_value%type;
cursor c_dept_update
is
select dept_no, dept_name, dept_value from dept;
begin
open c_dept_update;
loop
fetch c_dept_update into v_dept,v_dept_name, v_dept_value;
exit when c_dept_update%notfound;
update dept_updated
set dept_updated_value=
case
when dept=v_dept and dept_name=v_dept_name
then v_dept_value
else 0
end;
commit;
end loop;
close c1;
end;
result is like this
DEPT DEPT_NAME DEPT_UPDATED_VALUE
---------- ---------------------------------- ------------------
10 Chemistry 0
20 Mathematics 0
30 Biology 0
90 Astrology 0
40 Numerology 0
50 Cosmos 550
To paraphrase Steven Feuerstein, don't do in a loop what you can do in SQL. So I'd join the tables in an updatable inline view. I haven't tested this so it may need tweaking:
UPDATE ( SELECT b.dept, b.dept_name, b.dept_updated_value, a.dept AS dept2
FROM updated_dept b
LEFT JOIN dept a ON a.dept = b.dept AND a.dept_name = b.dept_name )
SET dept_updated_value = NVL( dept2, 0 )

display manager name and count of employees reporting him in employees table

I want to display manager_name and count of employees reporting him in employees table.I want to sort the data based on count IE maximum employees reporting to a manager should come first.
I tried to write self join but i could not get the out put .
EMPLOYEE_ID FIRST_NAME MANAGER_ID SALARY HIRE_DATE
198 Donald 124 2600 21-JUN-99
199 Douglas 124 2600 13-JAN-00
200 Jennifer 101 4400 17-SEP-87
201 Michael 100 13000 17-FEB-96
202 Pat 201 6000 17-AUG-97
203 Susan 101 6500 07-JUN-94
204 Hermann 101 10000 07-JUN-94
205 Shelley 101 12000 07-JUN-94
206 William 205 8300 07-JUN-94
100 Steven 24000 17-JUN-87
101 Neena 100 17000 21-SEP-89
the table name is employees and i want to see names also
You can use the aggregate function COUNT and ORDER BY clause
You didn't mention the table name assuming the table name as EMPLOYEES, below query would help you.
SELECT MANAGER_ID, COUNT(EMPLOYEE_ID) as EMP_COUNT
FROM EMPLOYEES
GROUP BY MANAGER_ID
ORDER BY EMP_COUNT DESC;
Here EMP_COUNT is the column alias name.If you don't want any column alias you can simply use the query below.
SELECT MANAGER_ID, COUNT(EMPLOYEE_ID)
FROM EMPLOYEES
GROUP BY MANAGER_ID
ORDER BY COUNT(EMPLOYEE_ID) DESC;
If you want to sort by ascending order instead of DESC you can use ASC.
We can get this output using an analytical function:
SELECT E.EMPID,E.EMPNAME as "Manager Name",M.EMPNAME AS "Employee Name",count(*) over(partition by e.empid) reportee_count
from empmgid m,empmgid e where M.MAGID=e.EMPID order by reportee_count desc;
Please employ the following SQL-Query:
SELECT
e.empno,
e.ename,
e1.empcnt
FROM
emp e,
(
SELECT
mgr,
COUNT(*) empcnt
FROM
emp
GROUP BY
mgr
) e1
WHERE
e.empno = e1.mgr;
-- Restricting which manager is having two employees working under them
-----------------------------------------------------------------------
SELECT E1.* FROM
(
SELECT E1.EMPNO,E1.ENAME AS EMPLOYE,
M1.ENAME AS MANAGERS,
COUNT(*)
OVER
(
PARTITION BY E1.EMPNO
) EMPCNT
FROM EMP E1,EMP M1
WHERE M1.MGR=E1.EMPNO
) E1
WHERE EMPCNT = 2;
select count(distinct manager_id) from employees;
select count(distinct manager_id) from employees;
Ans:
COUNT(DISTINCTMANAGER_ID)
18

the number of same record until this row

Is it possible to retrieve current number of same record for each row by using oracle pl/sql?
For example,
I have class table which consists of id, name, age columns
I want to have the sequence of student with the same name and age entering the class, assuming that id is countering up without altering data structure.
Thanks.
Regards,
Jim
Not sure I entirely get what you're asking for; you have an odd turn of phrase. An example of input data and expected result is always useful.
Perhaps something like this:
select id, name, age
from your_table
where (name, age) in
( select name. age
from your_table
group by name, age
having count(id) > 1 )
order by name, age, id
/
You could solve this with analytics. However, you still need an outer query to filter out the records which aren't duplicated, so I'm not sure what you'd gain:
select * from (
select id, name, age
, count(id) over (partition by name, age) as dup_count
from your_table )
where dup_count > 1
order by name, age, id
/
I'm not sure either, but it sounds to me something related to analytic functions. Take this as an example, look at srlno column, calculated using analytic functions:
SELECT empno, deptno, hiredate,
ROW_NUMBER( ) OVER (PARTITION BY
deptno ORDER BY hiredate
NULLS LAST) SRLNO
FROM emp
WHERE deptno IN (10, 20)
ORDER BY deptno, SRLNO;
EMPNO DEPTNO HIREDATE SRLNO
------ ------- --------- ----------
7782 10 09-JUN-81 1
7839 10 17-NOV-81 2
7934 10 23-JAN-82 3
7369 20 17-DEC-80 1
7566 20 02-APR-81 2
7902 20 03-DEC-81 3
7788 20 09-DEC-82 4
7876 20 12-JAN-83 5
More on analyltic functions:
http://www.orafaq.com/node/55
Remember, is a better approach if you can achieve your goal with a SQL instead of PL/SQL.

Resources