Showing posts with label Queries. Show all posts
Showing posts with label Queries. Show all posts

Wednesday, November 12, 2014

OVER and PARTITION BY - Analytic Function

I have come across a situation to sum-up a particular column of a table without using the group by clause. And below is the way i tackled it using the Analytic Function(OVER and PARTITION BY).

OVER :

OVER allows you to get aggregate information without using a GROUP BY. In other words, you can retrieve detail rows, and get aggregate data alongside it. For example, the below query:

SELECT SUM(Cost) OVER () AS Cost, OrderNum
FROM Orders;

Will return something like this:

Cost        OrderNum
10.00            345
10.00            346
10.00            347
10.00            348

Quick translation :

SUM(cost) –  get me the sum of the COST column
OVER –   for the set of rows
() –   that encompasses the entire result set.

OVER(PARTITION BY) :

OVER, as used in our previous example, exposes the entire result-set to the aggregation…”Cost” was the sum of all [Cost]  in the result-set.  We can break up that result-set into partitions with the use of PARTITION BY:

SELECT SUM(Cost) OVER (PARTITION BY CustomerNo) AS Cost
, OrderNum
, CustomerNo
FROM Orders;

My partition is by CustomerNo – each “window” of a single customer’s orders will be treated separately from each other “window”….I’ll get the sum of cost for Customer 1, and then the sum for Customer 2:

Cost  OrderNum   CustomerNo
 8.00      345                1
 8.00      346                1
 8.00      347                1
 2.00      348                2

Quick translation :

SUM(cost) – get me the sum of the COST column
OVER – for the set of rows….
(PARTITION BY CustomerNo) – …that have the same CustomerNo.

For more Analytic Functions Click Me.

Challa.

Wednesday, July 30, 2014

Query to find the Duplicates in the Table and Delete them.

Use the below Query to find the duplicates in the table :

SELECT *
FROM TABLE_NAME A
WHERE A.ROWID IN
(SELECT MIN(B.ROWID)
FROM TABLE_NAME B
WHERE
A.COLUMN1=B.COLUMN1
GROUP BY B.COLUMN1,B.COLUMN2);

Use the below Query to delete the duplicates in the table :

DELETE
FROM TABLE_NAME A
WHERE A.ROWID IN
(SELECT MIN(B.ROWID)
FROM TABLE_NAME B
WHERE
A.COLUMN1=B.COLUMN1
GROUP BY B.COLUMN1,B.COLUMN2);

Friday, July 4, 2014

Query to fetch the Parameter List and associated Value Sets of a Concurrent Program.

The following query will fetch the Parameter List and associated Value Sets of a Concurrent Program.

SELECT
fcpl.user_concurrent_program_name "Concurrent Program Name",
fcp.concurrent_program_name "Short Name",
fdfcuv.column_seq_num "Column Seq Number",
fdfcuv.end_user_column_name "Parameter Name",
fdfcuv.form_left_prompt "Prompt",
fdfcuv.enabled_flag " Enabled Flag",
fdfcuv.required_flag "Required Flag",
fdfcuv.display_flag "Display Flag",
fdfcuv.flex_value_set_id "Value Set Id",
ffvs.flex_value_set_name "Value Set Name",
flv.meaning "Default Type",
fdfcuv.DEFAULT_VALUE "Default Value"
FROM
fnd_concurrent_programs fcp,
fnd_concurrent_programs_tl fcpl,
fnd_descr_flex_col_usage_vl fdfcuv,
fnd_flex_value_sets ffvs,
fnd_lookup_values flv
WHERE
fcp.concurrent_program_id = fcpl.concurrent_program_id
--AND fcpl.user_concurrent_program_name = :conc_prg_name
AND fdfcuv.descriptive_flexfield_name = '$SRS$.'|| fcp.concurrent_program_name
AND ffvs.flex_value_set_id = fdfcuv.flex_value_set_id
AND flv.lookup_type(+) = 'FLEX_DEFAULT_TYPE'
AND flv.lookup_code(+) = fdfcuv.default_type
AND fcpl.LANGUAGE = USERENV ('LANG')
AND flv.LANGUAGE(+) = USERENV ('LANG')
ORDER BY fdfcuv.column_seq_num;

Friday, June 27, 2014

Query To Find Concurrent Program's Parameters and Value Sets


SELECT
fcp.user_concurrent_program_name "Concurrent Program Name",
fcp.concurrent_program_name "Short Name",
fdfcuv.column_seq_num "Column Seq Number",
fdfcuv.end_user_column_name "Parameter Name",
fdfcuv.form_left_prompt "Prompt",
fdfcuv.srw_param,
ffvs.flex_value_set_name "Value Set Name",
flv.meaning "Default Type",
fdfcuv.DEFAULT_VALUE "Default Value"
FROM
fnd_concurrent_programs_vl fcp,
fnd_descr_flex_col_usage_vl fdfcuv,
fnd_flex_value_sets ffvs,
fnd_lookup_values flv
WHERE 1=1
AND fdfcuv.descriptive_flexfield_name = '$SRS$.'||fcp.concurrent_program_name
AND ffvs.flex_value_set_id = fdfcuv.flex_value_set_id
AND flv.lookup_type(+) = 'FLEX_DEFAULT_TYPE'
AND flv.lookup_code(+) = fdfcuv.default_type
-- AND fcp.user_concurrent_program_name like 'XXAA%Report'
-- AND ffvs.flex_value_set_name LIKE '%JOB%'
ORDER BY
fcp.user_concurrent_program_name
,fdfcuv.column_seq_num;

Query to Extract AOL Menu Hierarchy Query

SELECT   lev "LEVEL", fm.user_menu_name "MASTER_MENU",
         entry_sequence "ENTRY_SEQ", a.prompt "PROMPT",
         fms.user_menu_name "CHILD_MENU",
         ffv.user_function_name "FUNCTION_NAME"
FROM (SELECT     LEVEL lev, fme.menu_id, fme.sub_menu_id, function_id,
                     entry_sequence, PRIOR entry_sequence prior_entry_seq,
                     fme.prompt
                FROM fnd_menu_entries_vl fme
               WHERE fme.grant_flag = 'Y'
          START WITH fme.menu_id = '69914'
          CONNECT BY PRIOR fme.sub_menu_id = fme.menu_id) a,
         fnd_menus_vl fm,
         fnd_menus_vl fms,
         fnd_form_functions_vl ffv
   WHERE a.menu_id = fm.menu_id AND fms.menu_id(+) = a.sub_menu_id
         AND ffv.function_id(+) = a.function_id
ORDER BY lev, prior_entry_seq, entry_sequence;

Attachments in Oracle Applications.


What is attachment in oracle application?

The attachments feature in oracle application enables users to link unstructured data, such as images, word-processing documents, spreadsheets, or text to their application data. For example, users can link images to items or video to operations as operation instructions.

Where to find an attachment?

There is an attachment icon in the oracle application toolbar that indicates whether the Attachments feature is enabled in a form block. When the button is dimmed, the Attachment feature is not available. When the Attachment feature is enabled in a form block, the icon becomes a solid paper clip. The icon switches to a paper clip holding a paper when the Attachment feature is enabled in a form lock and the current record has at least one attachment.

Attachment types:  An attached document can be:

1] Short Text
Text stored in the database containing less than 2000 characters.

2] Long Text
Text stored in the database containing 2000 characters or more.

3] Image
An image that Oracle Forms can display, including: bmp, cals, jfif, jpeg, gif, pcd, pcx, pict, ras, and tif.

4] OLE Object
An OLE Object that requires other OLE server applications to view, such as Microsoft Word or Microsoft Excel.

5] Web Page
A URL reference to a web page which you can view with your web browser.

Tables Involved:
For Importing Attachments in oracle application one has to populate following tables.

1. FND_DOCUMENTS
2. FND_ATTACHED_DOCUMENTS
3. FND_DOCUMENTS_TL
4. FND_DOCUMENT_DATATYPES.
5. FND_DOCUMENT_CATEGORIES
6. FND_DOCUMENTS_LONG_TEXT (Long text type attachment).
7. FND_DOCUMENTS_SHORT_TEXT (Short text type attachment).
8. FND_DOCUMENTS_LONG_RAW
9. FND_LOBS (File type attachments).

FND_DOCUMENTS:
FND_DOCUMENTS stores language-independent information about a document. For example, each row contains a document identifier, a category identifier, the method of security used for the document (SECURITY_TYPE, where 1=Organization,2=Set of Books, 3=Business unit,4=None), the period in which the document is active, and a flag to indicate whether or not the document can be shared outside of the security type (PUBLISH_FLAG).

Other specifications in this table include: datatype (DATATYPE_ID, where 1=short text,2=long text, 3=image, 4=OLE object), image type, and storage type (STORAGE_TYPE, where 1=stored in the database, 2=stored in the file system).

The document can be referenced by many application entities and changed only in the define document form (USAGE_TYPE=S); it can be used as a fill-in-the-blanks document, where each time you use a template, you make a copy of it (USAGE_TYPE=T); or it can be used only one time (USAGE_TYPE=O).Images and OLE Objects cannot be used as templates.

FND_ATTACHED_DOCUMENTS:
FND_ATTACHED_DOCUMENTS stores information relating a document to an application entity. For example, a record may link a document to a sales order or an item. Each row contains foreign keys to FND_DOCUMENTS and FND_DOCUMENT_ENTITIES. There is also a flag to indicate whether or not an attachment was created automatically.

FND_DOCUMENTS_TL:
FND_DOCUMENTS_TL stores translated information about the documents in FND_DOCUMENTS. Each row includes the document identifier, the language the row is translated to, the description of the document, the file in which the image is stored, and an identifier (MEDIA_ID) of the sub-table in which the document is saved (FND_DOCUMENTS_SHORT_TEXT, FND_DOCUMENTS_LONG_TEXT, or FND_DOCUMENTS_LONG_RAW).

FND_DOCUMENT_DATATYPES:
FND_DOCUMENT_DATATYPES stores the document datatypes that are supported. Initial values are: short text, long text, image, and OLE Object (DATATYPE_ID=1, 2, 3, or 4). Customers can add datatypes to handle documents stored outside of Oracle and use non-native Forms applications to view/edit their documents. The table uses a “duplicate record” model for handling multi-lingual needs. That is, for each category there will be one record with the same CATEGORY_ID and CATEGORY_NAME for each language.

FND_DOCUMENT_CATEGORIES:
FND_DOCUMENT_CATEGORIES stores information about the categories in which documents are classified. For example, documents may be considered “Bill of Material Comments”, “WIP Job Comments”, etc. Document categories are used to provide a measure of security on documents. Each form that enables the attachment feature lists which categories of documents can be viewed in the form. This table uses a “duplicate record” model for handling multi-lingual needs.

FND_DOCUMENTS_LONG_TEXT:
FND_DOCUMENTS_LONG_TEXT stores information about long text documents.

FND_DOCUMENTS_SHORT_TEXT:
FND_DOCUMENTS_SHORT_TEXT stores information about short text documents.

FND_DOCUMENTS_LONG_RAW:
FND_DOCUMENTS_LONG_RAW stores images and OLE Objects, such as Word Documents and Excel spreadsheets, in the database.

FND_DOCUMENT_ENTITIES:
FND_DOCUMENT_ENTITIES lists each entity to which attachments can be linked. For example, attachments can be linked to Items, Sales Orders, etc. Since the table uses a “duplicate record” model for handling multi-lingual needs, for each document entity there will be one record with the same DOCUMENT_ENTITY_ID and DATA_OBJECT_CODE for each language.

Queries:

1] To find all Long Text attachments:

SELECT
        FAD.SEQ_NUM "Seq Number",
        FDAT.USER_NAME "Data Type",
        FDCT.USER_NAME "Category User Name",
        FAD.ATTACHED_DOCUMENT_ID "Attached Document Id",
        FDET.USER_ENTITY_NAME "User Entity",
        FD.DOCUMENT_ID "Document Id",
        FAD.ENTITY_NAME "Entity Name",
        FD.MEDIA_ID "Media Id",
        FD.URL "Url",
        FDT.TITLE "Title",
        FDLT.LONG_TEXT "Attachment Text"
FROM
        FND_DOCUMENT_DATATYPES FDAT,
        FND_DOCUMENT_ENTITIES_TL FDET,
        FND_DOCUMENTS_TL FDT,
        FND_DOCUMENTS FD,
        FND_DOCUMENT_CATEGORIES_TL FDCT,
        FND_ATTACHED_DOCUMENTS   FAD,
        FND_DOCUMENTS_LONG_TEXT FDLT
WHERE
        FD.DOCUMENT_ID          = FAD.DOCUMENT_ID
        AND FDT.DOCUMENT_ID     = FD.DOCUMENT_ID
        AND FDCT.CATEGORY_ID    = FD.CATEGORY_ID
        AND FD.DATATYPE_ID      = FDAT.DATATYPE_ID
        AND FAD.ENTITY_NAME     = FDET.DATA_OBJECT_CODE
        AND FDLT.MEDIA_ID       = FD.MEDIA_ID
        AND FDAT.NAME           = 'LONG_TEXT';

2] To find all Short Text attachments:

SELECT
        FAD.SEQ_NUM "Seq Number",
        FDAT.USER_NAME "Data Type",
        FDCT.USER_NAME "Category User Name",
        FAD.ATTACHED_DOCUMENT_ID "Attached Document Id",
        FDET.USER_ENTITY_NAME "User Entity",
        FD.DOCUMENT_ID "Document Id",
        FAD.ENTITY_NAME "Entity Name",
        FD.MEDIA_ID "Media Id",
        FD.URL "Url",
        FDT.TITLE "Title",
        FDST.SHORT_TEXT "Attachment Text"
FROM
        FND_DOCUMENT_DATATYPES FDAT,
        FND_DOCUMENT_ENTITIES_TL FDET,
        FND_DOCUMENTS_TL FDT,
        FND_DOCUMENTS FD,
        FND_DOCUMENT_CATEGORIES_TL FDCT,
        FND_ATTACHED_DOCUMENTS   FAD,
        FND_DOCUMENTS_SHORT_TEXT FDST
WHERE
        FD.DOCUMENT_ID          = FAD.DOCUMENT_ID
        AND FDT.DOCUMENT_ID     = FD.DOCUMENT_ID
        AND FDCT.CATEGORY_ID    = FD.CATEGORY_ID
        AND FD.DATATYPE_ID      = FDAT.DATATYPE_ID
        AND FAD.ENTITY_NAME     = FDET.DATA_OBJEC_CODE
        AND FDST.MEDIA_ID       = FD.MEDIA_ID
        AND FDAT.NAME           = 'SHORT_TEXT';



Attachment upload through API:
Attachments can also be uploaded through an oracle provided API called FND_ATTACHED_DOCUMENTS_PKG.

It consist of three procedures
1) Insert Row
2) Update Row
3) Lock Row

Names of these procedures are self explanatory. insert row is used to insert a new row for attachment data, update row is used to update existing row for a particular row and Lock Row is used to lock a existing row for further modification.

Challa.

Query to Get All Active Employee Details.

SELECT papf.full_name "Full Name",
         papf.last_name "Last Name",
         papf.first_name "First Name",
         DECODE (papf.Person_Type_id,
                 '6', 'Emp',
                 '9', 'Ex-Emp',
                 '13', 'Cont')
            "Pers Type",
         papf.current_employee_flag "Current Emp Flag",
         papf.employee_number "Employee Number",
         papf.current_npw_flag "Current NPW Flag",
         papf.npw_number "NPW Number",
         b.d_job_id "Job Title",
         b.in_organization_flag "Internal",
         b.location_code "Location Code",
         b.office_site_flag "Office Site",
         b.d_supervisor_id "Supervisor",
         fu.user_name,
         fu.description "User Description",
         papf.email_address "User Email",
         fu.start_date "User Start"
    FROM apps.FND_USER fu,
         apps.PER_ALL_PEOPLE_F papf,
         apps.PER_ALL_ASSIGNMENTS_F asg,
         apps.PER_ASSIGNMENTS_V7 b,
         apps.HR_ALL_POSITIONS_F hapf,
         apps.HR_ALL_ORGANIZATION_UNITS haou,
         apps.PER_JOBS pjb
   WHERE (papf.person_id = asg.person_id(+) AND asg.person_id = b.person_id)
         AND b.effective_start_date = (SELECT MAX (b2.effective_start_date)
                                         FROM apps.per_assignments_v7 b2
                                        WHERE b2.person_id = b.person_id)
         AND SYSDATE BETWEEN papf.effective_start_date
                         AND papf.effective_end_date
         AND SYSDATE BETWEEN asg.effective_start_date
                         AND asg.effective_end_date
         AND asg.position_id = hapf.position_id(+)
         AND fu.employee_id(+) = papf.person_id
         AND haou.organization_id = asg.organization_id
         AND b.job_id = pjb.job_id(+)
ORDER BY papf.full_name;

SQL Trace


In this article, I am going to explain what to get trace for various technology components of the Oracle applications.

Hence we are going to see
  • What is sql trace?
  • How to take sql trace for a session?
  • How to do sql trace for a Form?
  • How to do sql trace for a Report?
  • How to do sql trace for a OAF Page?

What is sql trace?
  • SQL Trace is a diagnostic tool for sql runtime and it gives a raw dump of SQL queries executed in the session and this dump can be read using tkprof command.
  • Statements are displayed in the order they are processed.
  • Every statement excuted will be displayed with statistics and optimizer routing.
  • You can see what values are being bound at runtime.
  • If you are getting a runtime error like ORA-942 or ORA-904, you can find out which statement is causing it.
How to take sql trace for a session?

Enabling trace for the current session
  • alter session set sql_trace=true;
  • alter session set events ‘10046 trace name context forever, level <x>’;
  • dbms_session.set_sql_trace(true);
  • dbms_support.start_trace(waits=>true,binds=>true);
Enabling trace for a different session
  • dbms_system.set_sql_trace_in_session (SID,SERIAL#,TRUE);
  • DBMS_SUPPORT.START_TRACE_IN_SESSION( SID , SERIAL#, waits=>TRUE, binds=>TRUE )
How to do sql trace for a Form?
  • Help -> Diagnostics -> Trace and then Choose the trace level.
  • Note the path of the trace file displayed in the Dialog box
  • Do the transcation that is causing the performance problme
  • Help -> Diagnostics -> No Trace, to disable the trace
  • Trace file XXX.trc is returned in the path displayed in the dialog box.


Copy the path displayed in the alert window



How to do sql trace for a Report?

Add the following statement in the before report trigger

1. SRW.DO_SQL ('ALTER SESSION SET SQL_TRACE=TRUE');
2. Upload the report in Oracle Apps
3. Goto Application Developer Resposibility
Select Concurrent -> Program
4. Query for the Concurrent Program that executes the report
5. Select "Enable Trace" checkbox and save the record


6. Now run the report to get the trace file. (don't forget to disable the trace after running the report)

How to do sql trace for a OAF Page?
  • Set profile FND : Diagnostics to Yes at user level
  • Login to Self Service as the above user
  • Click on Diagnostics icon at the top of page



Select ‘Set Trace Level’ and click Go

It Displays following options
  • Disable Trace
  • Trace (regular)
  • Trace with binds
  • Trace with waits
  • Trace with binds and waits
  • Select the desired trace level and click Save


  • Perform the activity that you want to trace
  • Disable the Trace using Diagnostics Page.
  • Exit application
To determine where the raw trace file is located.
From SQLPlus execute following query:

SELECT value FROM v$parameter WHERE name = 'user_dump_dest'

How do I read the trace file or .trc file?
  • tkprof <tracefile> <outputfile> explain=username/password sort='(sorting options)'
  • Eg: tkprof POSTDI9837.trc output.prf explain=apps/apps sort=‘(prsela, exeela, fchela)’ 
Challa.

BPEL Tree SQL query


How to query the BPEL tree in the dehydration database directly?

Tree finder using SQL query.

select
lpad(' ', (level - 1) * 2) || title as padded_name, process_id, modify_date-creation_date, creation_date, modify_date, state
from cube_instance ci1
connect by prior cikey = parent_id
start with cikey = :parent_process;

TreeFinder jsp is the most resource hungry webpage in the BPEL console. This query is an alternative to the Tree Finder jsp.

Challa.