Wednesday, 25 February 2015

Cursor using the for loop
Example:
declare
p number(6);
cursor c is
select * from emp for update;
begin
for i in c
loop
select avg(sal) into p from emp where empno=i.empno group by deptno;
dbms_output.put_line(i.empno);
end loop;
end;

WHILE LOOP:
declare
cursor c is
select * from emp;
x emp%rowtype;
begin
open c;
fetch c into x;
while (c%found) loop
dbms_output.put_line(x.ename);
fetch c into x;
end loop;
end;


so, better to use the for loop

Friday, 20 February 2015

*******Fields present in the Requisition Form*****************

select distinct prha.segment1 Requisition_Number
,hou.name operating_unit
,prha.description
,prha.authorization_status Status
,prla.item_description
,prla.unit_meas_lookup_code UOM
,prla.unit_price
,prla.quantity
,prla.need_by_date
,prla.purchase_basis
,prla.destination_type_code
,papf.full_name
,msib.segment1 item
,mcb.segment1||'.'||mcb.segment2 as Categories
,gcc.code_combination_id
,gcc.segment1||'-'||gcc.segment2||'-'||gcc.segment3||'-'||gcc.segment4 as Charge_Account
,okhb.CONTRACT_NUMBER
from
hr_operating_units hou
,po_requisition_headers_all prha
,po_requisition_lines_all prla
,per_all_people_f papf
,mtl_system_items_b msib
,mtl_categories_b mcb
,gl_code_combinations gcc
,PO_REQ_DISTRIBUTIONS_ALL prda
,okc_k_headers_b okhb
where 1=1
and prha.org_id=hou.organization_id
AND prla.creation_date BETWEEN papf.effective_start_date AND papf.effective_end_date
and prla.requisition_header_id=prha.requisition_header_id
and prha.preparer_id =papf.person_id
and msib.inventory_item_id =prla.item_id
and prda.code_combination_id=gcc.code_combination_id
and prda.requisition_line_id =prla.requisition_line_id
and prla.category_id =mcb.category_id
and prha.segment1 = :Requisition
and prla.OKE_CONTRACT_HEADER_ID=okhb.id;
Query to build the  list the supervisors for a given employee in a single column

the format is as

Employee_1 -> Employee_2 -> Employee_3 -> Employee_4
where Employee_2 is the supervisor of Employee_1 and so on.




SELECT
 DISTINCT SYS_CONNECT_BY_PATH(FULL_NAME, '->') "Path",
LEVEL LVL
FROM
  PER_ALL_PEOPLE_F PPFS,PER_ALL_ASSIGNMENTS_F PAFE
WHERE 1 = 1
and PPFS.PERSON_ID =  PAFE.person_ID
AND TRUNC (SYSDATE) BETWEEN PAFE.EFFECTIVE_START_DATE AND PAFE.EFFECTIVE_END_DATE
AND TRUNC (SYSDATE) BETWEEN PPFS.EFFECTIVE_START_DATE AND PPFS.EFFECTIVE_END_DATE
and level = 3
  CONNECT BY Ppfs.PERSON_ID = PRIOR Pafe.SUPERVISOR_ID  -- PPFS,PAFE
 START WITH Ppfs.PERSON_ID = &Numbe ;

Wednesday, 18 February 2015

Nth highest Salary.........................!  :P



select top 1 from
(select distnict top n from emp  --retrving the nth highest salary
order by sal desc) result
order by sal;



with result as
(select sal,dense_rank() over (order by salary desc) as denserank
from emp)
select salary 
from emp
where result.denserank=n;


SQL::
Updating the column with value as D when it is duplicated.

Please go through the process.............................!

create table Emp_testing as select * from emp;//Creating the Table

select * from Emp_testing;

insert into Emp_testing values('7369','SMITH','CLERK','7902','17-DEC-80','800','','20');                          
insert into Emp_testing values('7369','SMITH','CLERK','7902','17-DEC-80','800','','20');
insert into Emp_testing values('7369','SMITH','CLERK','7902','17-DEC-80','800','','20');
 insert into Emp_testing values('7369','SMITH','CLERK','7902','17-DEC-80','800','','20');
commit;

alter table Emp_testing add status varchar2(4);

select empno,ename,job,mgr,hiredate,sal,comm,deptno,status,count(1) as counts
from Emp_testing group by empno,ename,job,mgr,hiredate,sal,comm,deptno,status;

UPDATE Emp_testing t2
    SET t2.status = 'D'
    WHERE t2.rowid > (SELECT min(t1.rowid)
                    FROM Emp_testing t1
                    WHERE t1.ename=t2.ename);

you will the output in Status  column with D value it indicates that Those are duplicates