Friday, March 16, 2012

Select statement to detail PO and PO approver details


Following is the query which details all the purchase orders approved by particular approver for a given date range:-

Parameters: P_START_DATE  -- PO Creation Date
                   P_END_DATE      -- PO Creation Date
                   P_EMP_NUM -- Approver Employee Number

SELECT   ph.segment1 po_number, poah1.object_sub_type_code po_type,
         pv.segment1 supplier_number, pv.vendor_name supplier_name,
         pl.po_amount, ph.currency_code, poah1.action_date approved_date,
         poah1.employee_name approver_name,
         ppf.employee_number appr_emp_number,
         (SELECT ppf1.full_name
            FROM po_action_history poa1,
                 per_all_people_f ppf1
           WHERE TRUNC (SYSDATE) BETWEEN TRUNC (ppf1.effective_start_date)
                                     AND TRUNC (ppf1.effective_end_date)
             AND ppf1.person_id = poa1.employee_id
             AND poa1.action_code = 'SUBMIT'
             AND poa1.object_type_code = 'PO'
             AND poa1.object_id = poah1.object_id
             AND poa1.object_type_code = poah1.object_type_code
             AND poa1.sequence_num =
                    (SELECT MIN (sequence_num)
                       FROM po_action_history
                      WHERE object_type_code = poa1.object_type_code
                        AND object_id = poa1.object_id)) po_submitted_by,
         poah1.sequence_num
    FROM po_headers_all ph,
         (SELECT poah.ROWID row_id, poah.created_by, poah.action_code,
                 poah.action_date, poah.employee_id, poah.approval_path_id,
                 poah.note, poah.object_revision_num, poah.offline_code,
                 poah.last_update_login, poah.request_id,
                 poah.program_application_id, poah.program_id,
                 poah.program_update_date, poah.program_date, poah.object_id,
                 poah.object_type_code, poah.object_sub_type_code,
                 poah.sequence_num, poah.last_update_date,
                 poah.last_updated_by, poah.creation_date,
                 (SELECT DISTINCT full_name
                             FROM per_all_people_f
                            WHERE TRUNC (SYSDATE)
                                     BETWEEN TRUNC
                                               (effective_start_date
                                               )
                                         AND TRUNC (effective_end_date)
                              AND person_id = poah.employee_id) employee_name,
                 NVL (polc1.displayed_field, polc2.displayed_field),
                 polc1.security_group_id
            FROM po_action_history poah,
                 (SELECT lookup_code, lookup_type, last_update_date,
                         last_updated_by, last_update_login, creation_date,
                         created_by, meaning displayed_field, description,
                         end_date_active, enabled_flag,
                         TO_NUMBER (NULL) request_id,
                         TO_NUMBER (NULL) program_application_id,
                         TO_NUMBER (NULL) program_id,
                         TO_DATE (NULL) program_update_date,
                         attribute_category, attribute1, attribute2,
                         attribute3, attribute4, attribute5, attribute6,
                         attribute7, attribute8, attribute9, attribute10,
                         attribute11, attribute12, attribute13, attribute14,
                         attribute15, security_group_id
                    FROM fnd_lookup_values lv
                   WHERE LANGUAGE = USERENV ('LANG')
                     AND view_application_id = 201
                     AND security_group_id =
                            0 /*fnd_global.lookup_security_group (lv.lookup_type,
                                             lv.view_application_id
                                            )*/) polc1,
                 (SELECT lookup_code, lookup_type, last_update_date,
                         last_updated_by, last_update_login, creation_date,
                         created_by, meaning displayed_field, description,
                         end_date_active, enabled_flag,
                         TO_NUMBER (NULL) request_id,
                         TO_NUMBER (NULL) program_application_id,
                         TO_NUMBER (NULL) program_id,
                         TO_DATE (NULL) program_update_date,
                         attribute_category, attribute1, attribute2,
                        attribute3, attribute4, attribute5, attribute6,
                         attribute7, attribute8, attribute9, attribute10,
                         attribute11, attribute12, attribute13, attribute14,
                         attribute15, security_group_id
                    FROM fnd_lookup_values lv
                   WHERE LANGUAGE = USERENV ('LANG')
                     AND view_application_id = 201
                     AND security_group_id =
                            0 /*  fnd_global.lookup_security_group (lv.lookup_type,
                                           lv.view_application_id
                                          )*/) polc2
           WHERE polc1.lookup_code(+) = poah.action_code
            AND polc1.lookup_type(+) = 'APPROVER ACTIONS'
             AND polc2.lookup_code(+) = poah.action_code
             AND polc2.lookup_type(+) = 'CONTROL ACTIONS'
             AND poah.object_type_code = 'PO') poah1,
         per_all_people_f ppf,
         (SELECT   SUM (unit_price * quantity) po_amount, po_header_id
              FROM po_lines_all
          GROUP BY po_header_id) pl,
         po_vendors pv
   WHERE poah1.object_id = ph.po_header_id
     AND poah1.object_type_code = 'PO'
     AND poah1.action_code = 'APPROVE'
     AND pv.vendor_id = ph.vendor_id
     AND pl.po_header_id = ph.po_header_id
     AND NVL (ph.cancel_flag, 'N') = 'N'
     AND ppf.employee_number = :P_EMP_NUM
     AND TRUNC (SYSDATE) BETWEEN TRUNC (ppf.effective_start_date)
                             AND TRUNC (ppf.effective_end_date)
     AND ppf.person_id = poah1.employee_id
     AND TRUNC (ph.creation_date) BETWEEN TRUNC (TO_DATE (:P_START_DATE))
                                      AND TRUNC (TO_DATE (:P_END_DATE))
     AND poah1.sequence_num =
            (SELECT MAX (sequence_num)
               FROM po_action_history
              WHERE object_type_code = poah1.object_type_code
                AND object_id = poah1.object_id)
ORDER BY ph.segment1, pl.po_amount DESC;

No comments:

Post a Comment