Search This Blog

Monday, March 5, 2012

LAG and LEAD Analytic Functions

Introduction

Both LAG and LEAD functions have the same usage, as shown below.
LAG  (value_expression [,offset] [,default]) OVER ([query_partition_clause] order_by_clause)
LEAD (value_expression [,offset] [,default]) OVER ([query_partition_clause] order_by_clause)
  • value_expression - Can be a column or a built-in function, except for other analytic functions.
  • offset - The number of rows preceeding/following the current row, from which the data is to be retrieved. The default value is 1.
  • default - The value returned if the offset is outside the scope of the window. The default value is NULL.
Looking at the EMP table, we query the data in salary (SAL) order.
SELECT empno,
       ename,
       job,
       sal
FROM   emp
ORDER BY sal;

     EMPNO ENAME      JOB              SAL
---------- ---------- --------- ----------
      7369 SMITH      CLERK            800
      7900 JAMES      CLERK            950
      7876 ADAMS      CLERK           1100
      7521 WARD       SALESMAN        1250
      7654 MARTIN     SALESMAN        1250
      7934 MILLER     CLERK           1300
      7844 TURNER     SALESMAN        1500
      7499 ALLEN      SALESMAN        1600
      7782 CLARK      MANAGER         2450
      7698 BLAKE      MANAGER         2850
      7566 JONES      MANAGER         2975
      7788 SCOTT      ANALYST         3000
      7902 FORD       ANALYST         3000
      7839 KING       PRESIDENT       5000

SQL>

LAG

The LAG function is used to access data from a previous row. The following query returns the salary from the previous row to calculate the difference between the salary of the current row and that of the previous row. Notice that the ORDER BY of the LAG function is used to order the data by salary.
SELECT empno,
       ename,
       job,
       sal,
       LAG(sal, 1, 0) OVER (ORDER BY sal) AS sal_prev,
       sal - LAG(sal, 1, 0) OVER (ORDER BY sal) AS sal_diff
FROM   emp;

     EMPNO ENAME      JOB              SAL   SAL_PREV   SAL_DIFF
---------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK            800          0        800
      7900 JAMES      CLERK            950        800        150
      7876 ADAMS      CLERK           1100        950        150
      7521 WARD       SALESMAN        1250       1100        150
      7654 MARTIN     SALESMAN        1250       1250          0
      7934 MILLER     CLERK           1300       1250         50
      7844 TURNER     SALESMAN        1500       1300        200
      7499 ALLEN      SALESMAN        1600       1500        100
      7782 CLARK      MANAGER         2450       1600        850
      7698 BLAKE      MANAGER         2850       2450        400
      7566 JONES      MANAGER         2975       2850        125
      7788 SCOTT      ANALYST         3000       2975         25
      7902 FORD       ANALYST         3000       3000          0
      7839 KING       PRESIDENT       5000       3000       2000

SQL>

LEAD

The LEAD function is used to return data from the next row. The following query returns the salary from the next row to calulate the difference between the salary of the current row and the following row.
SELECT empno,
       ename,
       job,
       sal,
       LEAD(sal, 1, 0) OVER (ORDER BY sal) AS sal_next,
       LEAD(sal, 1, 0) OVER (ORDER BY sal) - sal AS sal_diff
FROM   emp;

     EMPNO ENAME      JOB              SAL   SAL_NEXT   SAL_DIFF
---------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK            800        950        150
      7900 JAMES      CLERK            950       1100        150
      7876 ADAMS      CLERK           1100       1250        150
      7521 WARD       SALESMAN        1250       1250          0
      7654 MARTIN     SALESMAN        1250       1300         50
      7934 MILLER     CLERK           1300       1500        200
      7844 TURNER     SALESMAN        1500       1600        100
      7499 ALLEN      SALESMAN        1600       2450        850
      7782 CLARK      MANAGER         2450       2850        400
      7698 BLAKE      MANAGER         2850       2975        125
      7566 JONES      MANAGER         2975       3000         25
      7788 SCOTT      ANALYST         3000       3000          0
      7902 FORD       ANALYST         3000       5000       2000
      7839 KING       PRESIDENT       5000          0      -5000

Friday, March 2, 2012

Datetrack Modes in Oracle HRMS

p_datetrack_update_mode
-----------------------
APIs that do updates on at least one datetracked table will contain this
parameter. It defines the type of datetracked operation to be performed. The
choices are
UPDATE                    Keep history of existing information
CORRECTION                Correct existing information
UPDATE_OVERRIDE           Replace all scheduled changes
UPDATE_CHANGE_INSERT      Insert this change before next scheduled change.
Please note that the OVN for each datetrack change for the same primary key
will be different. It is,therefore, important that the correct datetrack record
is read for the appropriate datetrack mode of operation.  i.e; at the correct
p_effective_date.

p_datetrack_delete_mode
-----------------------
APIs that do delete operations on a datetracked table will contain this
parameter. It allows you to define the type of datetrack deletes to make.
The choices are
ZAP                      Completely remove from database
DELETE                   Set end date of record to effective date
FUTURE_CHANGE            Remove all scheduled changes
DELETE_NEXT_CHANGE       Remove next change

Monday, February 13, 2012

Wednesday, February 8, 2012

How to configure Oracle Approval Management (AME) for developer for Specefic Application User

Follow the steps below to have access to AME.

Steps to AME configuration for any user

1) Select the User Management responsibility.
2) Select the Users page.
3) Search for the user to whom you wish to grant AME roles.
4) In the results table, click Update. In the Update User page, you can view user details along with a list of roles available to the user.
5) Click Assign Roles.
6) Select following roles and click Apply.

1) Approvals Management Administrator
2) Approvals Management Analyst
3) Approvals Management System Viewer
4) Approvals Management System Administrator
5) Approvals Management Process Owner
7) Grant data access to users
1) Login as Administrator.
2) Select the Functional Administrator responsibility.
3) Select the Grants tab.
4) Click Create Grant.
5) Select Specific User as grantee type.
6) Select the user as grantee key.
7) Select AME Transaction Types as Object
8) Select AME Calling Applications as Set
9) Follow the screen and click on Finish
8) Run “Workflow Directory Services User/Role Validation”
9) Select Approval Management Business Analyst responsibility and check functionality

Wednesday, February 1, 2012

Find table used by which Packages and Procedures

Query:

SELECT name FROM dba_dependencies WHERE referenced_name = <your table name> UNION SELECT referenced_name FROM dba_dependencies WHERE name = <your table name>;

Sunday, January 29, 2012

Employee Supervisor based on Position Hierarchy

select   psv.position_structure_id,
         pps.name hierarchy_name,
         curr_person.person_id curr_person_id,
         curr_person.employee_number curr_colleague_number,
           curr_person.full_name as curr_colleague_name,
           str.subordinate_position_id as curr_pos_id,
           pos.name as curr_pos_name,
           next_person.person_id supervisor_person_id,
           next_person.employee_number supervisor_collegue_num,
           next_person.full_name as supervisor_name,
           str.parent_position_id as supervisor_pos_id,
           next_pos.name as supervisor_pos_name
    FROM   per_all_people_f curr_person,--fnd_user cur_user,
           per_pos_structure_elements str,
           per_pos_structure_versions psv,
           per_all_positions pos,
           per_all_positions next_pos,
           per_all_assignments_f cur_assign,
           per_all_people_f next_person,
           per_position_structures pps,
           per_all_assignments_f next_assign
   WHERE   str.pos_structure_version_id = psv.pos_structure_version_id
           AND TRUNC (SYSDATE) BETWEEN psv.date_from
                                   AND  NVL (psv.date_to, SYSDATE)
           AND pos.position_id = str.subordinate_position_id
           AND next_pos.position_id = str.parent_position_id
           and cur_assign.position_id = pos.position_id
           and cur_assign.person_id = curr_person.person_id
           and trunc(sysdate) between curr_person.effective_start_date and curr_person.effective_end_date
           and trunc(sysdate) between cur_assign.effective_start_date and cur_assign.effective_end_date
           and trunc(sysdate) between next_person.effective_start_date and next_person.effective_end_date
           and trunc(sysdate) between next_assign.effective_start_date and next_assign.effective_end_date
           and next_assign.position_id  = next_pos.position_id
           and next_assign.person_id = next_person.person_id
           AND  psv.position_structure_id = pps.position_structure_id  

Tuesday, January 3, 2012

Purchase Order Status Report

CREATE OR REPLACE FORCE VIEW apps.xxaye_po_order_status_disc
AS   create or replace view XXAYE_PO_ORDER_STATUS_DISC
   as
   SELECT apps.xxaly_scm_general_pkg.get_operating_unit (pha.org_id)
                                                               operating_unit,
          pha.segment1 po_number, TRUNC (pha.creation_date) po_date,
          pll.promised_date, TO_CHAR (pll.promised_date, 'YYYY') promise_year,
          pha.authorization_status po_status, pv.vendor_name, xtm.item_code,
          xtm.description, xtm.CATEGORY category_seg1,
          xtm.sub_category cat_seg2,
          xtm.CATEGORY || '.' || xtm.sub_category CATEGORY,
          pll.quantity qty_ordered, pll.quantity_received qty_received,
          pll.quantity - pll.quantity_received bal_qty, NULL grn_num,
          NULL grn_date, NULL grn_qty,xxh.user_name created_by,null receiving_organization
     FROM apps.po_headers_all pha,
          apps.po_lines_all pla,
          apps.po_vendors pv,
          apps.po_line_locations_all pll,
          apps.xx_mtl_master_items_v xtm,
          apps.xxaly_hr_user_person_v xxh
    WHERE pha.po_header_id = pla.po_header_id
      AND pll.po_header_id(+) = pla.po_header_id
      AND pll.po_line_id(+) = pla.po_line_id
      AND pla.po_line_id NOT IN (SELECT NVL (rsl.po_line_id, 0)
                                   FROM apps.rcv_shipment_lines rsl
                                  WHERE pla.po_line_id = rsl.po_line_id)
      AND pv.vendor_id(+) = pha.vendor_id
      AND xtm.inventory_item_id(+) = pla.item_id
      AND pha.created_by = xxh.user_id      
   UNION
   SELECT apps.xxaly_scm_general_pkg.get_operating_unit (pha.org_id)
                                                               operating_unit,
          pha.segment1 po_number, TRUNC (pha.creation_date) po_date,
          pll.promised_date, TO_CHAR (pll.promised_date, 'YYYY') promise_year,
          pha.authorization_status po_status, pv.vendor_name, xtm.item_code,
          xtm.description, xtm.CATEGORY category_seg1,
          xtm.sub_category cat_seg2,
          xtm.CATEGORY || '.' || xtm.sub_category CATEGORY,
          pll.quantity qty_ordered, pll.quantity_received qty_received,
          pll.quantity - pll.quantity_received bal_qty,
          rsh.receipt_num grn_num, TRUNC (rt.transaction_date) grn_date,
          rsl.quantity_received grn_qty,xxh.user_name created_by,ood.organization_name receiving_organization
     FROM apps.po_headers_all pha,
          apps.po_lines_all pla,
          apps.rcv_transactions rt,
          apps.rcv_shipment_headers rsh,
          apps.rcv_shipment_lines rsl,
          apps.po_vendors pv,
          apps.po_line_locations_all pll,
          apps.xx_mtl_master_items_v xtm,
          apps.xxaly_hr_user_person_v xxh,
          apps.org_organization_definitions ood
    WHERE pha.po_header_id = pla.po_header_id
      AND pha.po_header_id = rt.po_header_id
      AND pla.po_line_id = rt.po_line_id
      AND rsh.shipment_header_id = rt.shipment_header_id
      AND rt.transaction_type = 'RECEIVE'
      AND pv.vendor_id = pha.vendor_id
      AND pll.po_header_id(+) = pla.po_header_id
      AND pll.po_line_id(+) = pla.po_line_id
      AND xtm.inventory_item_id(+) = pla.item_id
      AND rsh.shipment_header_id = rsl.shipment_header_id
      AND rsl.po_line_id = pla.po_line_id
      AND pha.created_by = xxh.user_id
      AND ood.organization_id = rt.organization_id ;
/