Tuesday, 24 January 2017

Oracle SQL Tuning Tips

1. Do not use the set operator UNION if the objective can be achieved through an UNION ALL. UNION incurs an extra sort operation which can be avoided.
2. Select ONLY those columns in a query which are required. Extra columns which are not actually used, incur more I/O on the database and increase network traffic.
3. Do not use the keyword DISTINCT if the objective can be achieved otherwise. DISTINCT incurs an extra sort operation and therefore slows your queries down.
4. If it is required to use a composite index, try to use the “Leading” column in the “WHERE” clause. Though Index skip scan is possible, it incurs extra cost in creating virtual indexes and may not be always possible depending on the cardinality of the leading columns.
5. There should not be any Cartesian product in the query unless there is a definite requirement to do so. I know this is a silly point but we all have done this mistake at one point ðŸ™‚
6. Wherever multiple tables are used, always refer to a column by either using an alias or using the fully qualified name. Do not leave the guess work for Oracle.
7. SQL statements should be formatted consistently (e.g the keywords should be in CAPS only) to aid readability. Now, this is not a performance tip really. However, it’s important and part of the practices.
8. If possible use bind variables instead of constant/literal values in the predicate filter conditions to reduce repeated parsing of the same statement.
9. Use meaningful aliases for tables/views
10. When writing sub-queries make use of the EXISTS operator where possible as Oracle knows that once a match has been found it can stop and avoid a full table scan (it does a SEMI JOIN).
11. If the selective predicate is in the sub query, then use IN.
12. If the selective predicate is in the parent query, then use EXISTS.
13. Do not modify indexed columns with functions such as RTRIM, TO_CHAR, UPPER, TRUNC as this will prevent the optimizer from identifying the index. If possible perform the modification on the constant side of the condition. If the indexed column is usually accessed through a function (e.g NVL), consider creating a function based index.
14. Try to use an index if less than 5% of the data needs to be accessed from a data set. The exception is a small table (a few hundred rows) which is usually best accessed through a FULL table scan irrespective of the percentage of data required.
15. Use equi-joins whenever possible, they improve SQL efficiency
16. Avoid the following kinds of complex expressions:
    • NVL (col1,-999) = ….
    • TO_DATE(), TO_NUMBER(), and so on
These expressions prevent the optimizer from assigning valid cardinality or selectivity estimates and can in turn affect the overall plan and the join method
17. It is always better to write separate SQL statements for different tasks, but if you must use one SQL statement, then you can make a very complex statement slightly less complex by using the UNION ALL operator
18. Joins to complex views are not recommended, particularly joins from one complex view to another. Often this results in the entire view being instantiated, and then the query is run against the view data
19. Querying from a view requires all tables from the view to be accessed for the data to be returned. If that is not required, then do not use the view. Instead, use the base table(s), or if necessary, define a new view.
20. While querying on a partitioned table try to use the partition key in the “WHERE” clause if possible. This will ensure partition pruning.
21. Consider using the PARALLEL hint (only when additional resources can be allocated) while accessing large data sets.
22. Avoid doing an ORDER BY on a large data set especially if the response time is important.
23. Consider changing the OPTIMIZER MODE to FIRST_ROWS(n) if the response time is important. The default is ALL_ROWS which gives better throughput.
24. Use CASE statements instead of DECODE (especially where nested DECODEs are involved) because they increase the readability of the query immensely.
25. Do not use HINTS unless the performance gains clear.
26. Check if the statistics for the objects used in the query are up to date. If not, use the DBMS_STATS package to collect the same.
27. It is always good to understand the data both functionally and it’s diversity and volume in order to tune the query. Selectivity (predicate) and Cardinality (skew) factors have a big impact on query plan. Use of Statistics and Histograms can drive the query towards a better plan.
28. Read explain plan and try to make largest restriction (filter) as the driving site for the query, followed by the next largest, this will minimize the time spent on I/O and execution in subsequent phases of the plan.
29. If Query requires quick response rather than good throughput is the objective, try to avoid sorts (group by, order by, etc.). For good throughput, optimizer mode should be set to ALL ROWS.
30. Queries tend to perform worse as they age due to volume increase, structural changes in the database and application, upgrades etc. Use Automatic Workload Repository (AWR) and Automatic Database Diagnostic Monitor (ADDM) to better understand change in execution plan and throughput of top queries over a period of time.
31. SQL Tuning Advisor and SQL Access Advisor can be used for system advice on tuning specific SQL and their join and access paths, however, advice generated by these tools may not be always applicable (point 28).
32. SQL Access paths for joins are an component determining query execution time. Hash Joins are preferable when 2 large tables need to be joined. Nested loops make work better when a large table is joined with a small table.

Tuesday, 3 November 2015

BOM Extract Query 11i

--Bill Of materials Extract Queries

SELECT
m.organization_id,
m.segment1 assembly_item,
b.alternate_bom_designator,
--m.REVISION,
--b.assembly_item_id,
--b.bill_sequence_id assembly_bom_seqid,
c.item_num item_seq,
c.operation_seq_num,
m1.segment1 component_item,
c.component_quantity,
c.component_yield_factor,
(SELECT meaning FROM fnd_lookup_values WHERE lookup_type='WIP_SUPPLY'AND language ='US'AND lookup_code =c.wip_supply_type)supply_type,
c.supply_subinventory,
(SELECT segment1 FROM apps.mtl_item_locations WHERE  organization_id = 1657 AND inventory_location_id =c.supply_locator_id)supply_locator,
c.check_atp,
c.component_remarks comments,
--c.component_item_id,
--c.bill_sequence_id component_bom_seqid
b.creation_date
FROM apps.bom_bill_of_materials b,
apps.mtl_system_items m,
apps.bom_inventory_components c,
apps.mtl_system_items m1
WHERE b.organization_id = 1657
AND b.assembly_item_id = m.inventory_item_id
AND c.component_item_id = m1.inventory_item_id
AND m1.organization_id = m.organization_id
AND c.bill_sequence_id = b.bill_sequence_id
--AND m.planning_make_buy_code = 1
AND m.enabled_flag = 'Y'
AND m.organization_id = b.organization_id
--AND m.segment1='A196298000'
AND to_date(trunc(b.creation_date))='02-SEP-14'
ORDER BY 2;

AR Transaction types SQL Query 11i

--AR Transactions SQL Query
SELECT
  (SELECT NAME FROM apps.hr_operating_units WHERE organization_id=rct.org_id
  ) ou_name ,
  rct.NAME ,
  (SELECT meaning
  FROM ar_lookups
  WHERE lookup_type='INV/CM'
  AND lookup_code  =rct.TYPE
  ) CLASS ,
  rct.description ,
  rct.post_to_gl ,
  rct.default_printing_option ,
  (SELECT meaning
  FROM ar_lookups
  WHERE lookup_type='INVOICE_TRX_STATUS'
  AND lookup_code  =rct.default_status
  )transaction_status,
  rct.allow_freight_flag ,
  rct.accounting_affect_flag ,
  rct.status ,
  (SELECT NAME FROM ra_terms WHERE term_id=rct.default_term
  ) terms,
  (SELECT NAME
  FROM ra_cust_trx_types_all
  WHERE cust_trx_type_id=rct.subsequent_trx_type_id
  ) invoice_type ,
  -- RCT.SET_OF_BOOKS_ID ,
  rct.allow_overapplication_flag ,
  rct.tax_calculation_flag ,
  rct.creation_sign ,
  (SELECT rule_set_name
  FROM apps.ar_app_rule_sets
  WHERE rule_set_id=rct.rule_set_id
  )application_rule_set,
  (SELECT NAME
  FROM ra_cust_trx_types_all
  WHERE cust_trx_type_id=rct.credit_memo_type_id
  )credit_memo_type ,
  rct.start_date ,
  rct.end_date ,
  rct.allocate_tax_freight,
  rct.signed_flag ,
  rct.drawee_issued_flag ,
  rct.natural_application_only_flag ,
  --RCT.MAGNETIC_FORMAT_CODE ,
  (
  SELECT gcc.segment1
    ||'-'
    || gcc.segment2
    ||'-'
    || gcc.segment3
    ||'-'
    || gcc.segment4
    ||'-'
    || gcc.segment5
    ||'-'
    || gcc.segment6
    ||'-'
    || gcc.segment7
  FROM gl_code_combinations gcc
  WHERE gcc.code_combination_id=rct.gl_id_rec
  )receivable_acc,
  (SELECT gcc.segment1
    ||'-'
    || gcc.segment2
    ||'-'
    || gcc.segment3
    ||'-'
    || gcc.segment4
    ||'-'
    || gcc.segment5
    ||'-'
    || gcc.segment6
    ||'-'
    || gcc.segment7
  FROM gl_code_combinations gcc
  WHERE gcc.code_combination_id=rct.gl_id_rev
  )revenue_account,
  (SELECT gcc.segment1
    ||'-'
    || gcc.segment2
    ||'-'
    || gcc.segment3
    ||'-'
    || gcc.segment4
    ||'-'
    || gcc.segment5
    ||'-'
    || gcc.segment6
    ||'-'
    || gcc.segment7
  FROM gl_code_combinations gcc
  WHERE gcc.code_combination_id=rct.gl_id_unpaid_rec
  )unapp_receivable_acc ,
  (SELECT gcc.segment1
    ||'-'
    || gcc.segment2
    ||'-'
    || gcc.segment3
    ||'-'
    || gcc.segment4
    ||'-'
    || gcc.segment5
    ||'-'
    || gcc.segment6
    ||'-'
    || gcc.segment7
  FROM gl_code_combinations gcc
  WHERE gcc.code_combination_id=rct.gl_id_tax
  ) tax_account,
  (SELECT gcc.segment1
    ||'-'
    || gcc.segment2
    ||'-'
    || gcc.segment3
    ||'-'
    || gcc.segment4
    ||'-'
    || gcc.segment5
    ||'-'
    || gcc.segment6
    ||'-'
    || gcc.segment7
  FROM gl_code_combinations gcc
  WHERE gcc.code_combination_id=rct.gl_id_freight
  )frieght_account,
  (SELECT gcc.segment1
    ||'-'
    || gcc.segment2
    ||'-'
    || gcc.segment3
    ||'-'
    || gcc.segment4
    ||'-'
    || gcc.segment5
    ||'-'
    || gcc.segment6
    ||'-'
    || gcc.segment7
  FROM gl_code_combinations gcc
  WHERE gcc.code_combination_id=rct.gl_id_clearing
  )clearing_account ,
  (SELECT gcc.segment1
    ||'-'
    || gcc.segment2
    ||'-'
    || gcc.segment3
    ||'-'
    || gcc.segment4
    ||'-'
    || gcc.segment5
    ||'-'
    || gcc.segment6
    ||'-'
    || gcc.segment7
  FROM gl_code_combinations gcc
  WHERE gcc.code_combination_id=rct.gl_id_unearned
  )unern_revenue_account,
  (SELECT gcc.segment1
    ||'-'
    || gcc.segment2
    ||'-'
    || gcc.segment3
    ||'-'
    || gcc.segment4
    ||'-'
    || gcc.segment5
    ||'-'
    || gcc.segment6
    ||'-'
    || gcc.segment7
  FROM gl_code_combinations gcc
  WHERE gcc.code_combination_id=rct.gl_id_unbilled
  )unbilled_account ,
  (SELECT gcc.segment1
    ||'-'
    || gcc.segment2
    ||'-'
    || gcc.segment3
    ||'-'
    || gcc.segment4
    ||'-'
    || gcc.segment5
    ||'-'
    || gcc.segment6
    ||'-'
    || gcc.segment7
  FROM gl_code_combinations gcc
  WHERE gcc.code_combination_id=rct.gl_id_remittance
  )remittance_account ,
  (SELECT gcc.segment1
    ||'-'
    || gcc.segment2
    ||'-'
    || gcc.segment3
    ||'-'
    || gcc.segment4
    ||'-'
    || gcc.segment5
    ||'-'
    || gcc.segment6
    ||'-'
    || gcc.segment7
  FROM gl_code_combinations gcc
  WHERE gcc.code_combination_id=rct.gl_id_factor
  )factor_account
FROM ra_cust_trx_types_all rct
WHERE org_id=:p_org_id
  --AND NAME='EIT Deposit'--'EIT Deposit';
  ;

AP AR Payment Terms Extract Query 11i

AR payments terms

SELECT t.NAME ,
  t.description,
  b.base_amount,
  b.partial_discount_flag,
  nvl(b.prepayment_flag,'N'),
  b.credit_check_flag,
  al.meaning discount_basis,
  b.start_date_active,
  b.end_date_active,
  b.printing_lead_days,
  b.due_cutoff_day,
  al.meaning installment_option,
  b.in_use,
  b.term_id,
  rtl.sequence_num,
  rtl.relative_amount,
  rtl.due_days,
  rtl.due_date,
  rtl.due_day_of_month,
  rtl.due_months_forward
FROM apps.ra_terms_tl t,
  apps.ra_terms_b b,
  apps.ar_lookups al,
  apps.ar_lookups al1,
  apps.ra_terms_lines rtl
WHERE b.term_id = t.term_id
AND rtl.term_id(+)=b.term_id
AND SYSDATE BETWEEN nvl(b.start_date_active,SYSDATE) AND nvl(b.end_date_active,SYSDATE)
AND al.lookup_code=  b.calc_discount_on_lines_flag
AND al.lookup_type='DISCOUNT_BASIS'
AND al1.lookup_type='INSTALLMENT_OPTION'
AND al1.lookup_code=    b.first_installment_code
AND t.language  = userenv('LANG')
ORDER BY 1;


--AP Payment terms

SELECT  b.NAME,
  b.description,
  b.term_id,
  b.enabled_flag,
  b.due_cutoff_day,
  b.TYPE,
  b.start_date_active,
  b.end_date_active,
  b.rank  ,
  atl.sequence_num,
  atl.due_percent,
  atl.due_amount,
  atl.fixed_date,
  atl.calendar,
  atl.due_days,
  atl.due_day_of_month,
  atl.due_months_forward,
  atl.discount_percent,
  atl.discount_days,
  atl.discount_day_of_month,
  atl.discount_months_forward,
  atl.discount_percent_2,
  atl.discount_days_2,
  atl.discount_day_of_month_2,
  atl.discount_months_forward_2,
  atl.discount_percent_3,
  atl.discount_days_3,
  atl.discount_day_of_month_3,
  atl.discount_months_forward_3
FROM apps.ap_terms_tl b,
 ap_terms_lines atl
WHERE b.language = userenv('LANG')
AND b.term_id=atl.term_id(+)
 AND SYSDATE BETWEEN nvl(b.start_date_active,SYSDATE) AND nvl(b.end_date_active,SYSDATE)
 ORDER BY 1;




SQL Query to get Session ID and Kill the session

SELECT a.request_id, d.sid, d.serial# ,d.osuser,d.process , c.SPID
 FROM apps.fnd_concurrent_requests a,
 apps.fnd_concurrent_processes b,
 v$process c,
 v$session d
 WHERE a.controlling_manager = b.concurrent_process_id
 AND c.pid = b.oracle_process_id
 AND b.session_id=d.audsid
 AND a.request_id = 277287411
 --AND a.phase_code = 'R'
 ;
alter system kill session '1403,15205'

AP AR tax Code Extract Query

--AP Tax Codes

SELECT atc.*,
    gcc.segment1
    ||'-'
    ||gcc.segment2
    ||'-'
    ||gcc.segment3
    ||'-'
    ||gcc.segment4
    ||'-'
    ||gcc.segment5
    ||'-'
    ||gcc.segment6
    ||'-'
    ||gcc.segment7
    ||'-'
    ||gcc.segment8
    ||'-'
    ||gcc.segment9
    ||'-'
    ||gcc.segment10 tax_code_combination  
FROM apps.ap_tax_codes_all atc,
apps.gl_code_combinations gcc
WHERE
gcc.code_combination_id = atc.tax_code_combination_id
AND atc.enabled_flag = 'Y'
AND SYSDATE BETWEEN nvl(atc.start_date, SYSDATE) AND nvl(atc.inactive_date,SYSDATE)
AND atc.org_id     = 2659;

-- AR tax Code:

SELECT avtb.* ,
  gcc.segment1
  ||'-'
  ||gcc.segment2
  ||'-'
  ||gcc.segment3
  ||'-'
  ||gcc.segment4
  ||'-'
  ||gcc.segment5
  ||'-'
  ||gcc.segment6
  ||'-'
  ||gcc.segment7
  ||'-'
  ||gcc.segment8
  ||'-'
  ||gcc.segment9
  ||'-'
  ||gcc.segment10 tax_code_combination
FROM apps.ar_vat_tax_all_b avtb,
  apps.ar_vat_tax_all_tl avtt,
  apps.gl_code_combinations gcc
WHERE gcc.code_combination_id (+) = avtb.tax_account_id
AND avtb.vat_tax_id               = avtt.vat_tax_id
AND avtt.language                 = 'US'
AND avtb.enabled_flag             = 'Y'
AND SYSDATE BETWEEN nvl(avtb.start_date, SYSDATE) AND nvl(avtb.end_date,SYSDATE)
AND avtt.org_id = :p_org_id

Monday, 12 May 2014

How to eliminate duplicates from PLSQL table?

In Oracle 10g it made easy to filter out duplicate data from pl/sql table or nested table. This is possible in 10g by using"MULTISET" key word.


Following is the example which eliminates duplicate values from the PLSQL table...

DECLARE
TYPE nested_typ IS TABLE OF VARCHAR2(200);
v_test_type    nested_typ := nested_typ();
v_temp         nested_typ ;
BEGIN
v_test_type.extend(9);
-- read values into array.
v_test_type(1) := 'NEW YORK';
v_test_type(2) := 'SYDNEY';
v_test_type(3) := 'SINGAPORE';
v_test_type(4) := 'PERTH';
v_test_type(5) := 'NEW YORK';
v_test_type(6) := 'NEW YORK';
v_test_type(7) := 'DELHI';
v_test_type(8) := 'PERTH';
v_test_type(9) := 'MADURAI';

dbms_output.put_line('Cities before distinct..');
-- display values before distinct..
FOR i IN v_test_type.first..v_test_type.last
LOOP
   dbms_output.put_line(v_test_type(i));
END LOOP ;

-- assign all values to v_temp, which are in v_test_type
v_temp := v_test_type ;

-- take ditinct values out of v_temp, v_test_type into v_test_type
v_test_type := v_test_type MULTISET UNION DISTINCT v_temp ;
dbms_output.put_line('Cities after distinct..');

-- display values after distinct..
FOR i IN v_test_type.first..v_test_type.last
LOOP
   dbms_output.put_line(v_test_type(i));
END LOOP ;
END ;
/

OUTPUT
========
Cities BEFORE DISTINCT..
------------------------
NEW YORK
SYDNEY
SINGAPORE
PERTH
NEW YORK
NEW YORK
DELHI
PERTH
MADURAI
.
Cities AFTER DISTINCT..
-----------------------
NEW YORK
SYDNEY
SINGAPORE
PERTH
DELHI
MADURAI

PL/SQL PROCEDURE successfully completed.

Account Combination Creation API

Account Combination Creation API:

There are 2 flexfield APIs which you can use to either validation the existing given code_combination_id (ccid) or to validate and create a new code combination (Account).
1. fnd_flex_keyval.VALIDATE_SEGS() - To validate and if required can create Account code combination.
2. fnd_flex_keyval.validate_ccid() - The API used to validate the code combination id value. If the code combination valid then this API will returns true else false.

1. fnd_flex_keyval.VALIDATE_SEGS():

------------------------------------------------------------------------------------------------------------
create or replace function create_ccid
( p_concat_segs in varchar2
) return varchar2
is
  -- pragma autonomous_transaction; -- if you need autonomy!
  l_keyval_status     BOOLEAN;
  l_coa_id            NUMBER;
begin
  begin
    select chart_of_accounts_id
    into   l_coa_id
    from   gl_sets_of_books
    where  set_of_books_id = fnd_profile.value('GL_SET_OF_BKS_ID');
  exception
    when no_data_found then
      dbms_output.put_line('Chart of Accounts ID not found from profile option GL_SET_OF_BKS_ID');
      dbms_output.put_line('Try setting up your environment with fnd_global.apps_initialize');
      raise;
  end;
  -- keyval_mode can be one of CREATE_COMBINATION CHECK_COMBINATION FIND_COMBINATION
  -- create will only work if dynamic inserts on and cross validation rules not broken
  l_keyval_status := fnd_flex_keyval.validate_segs(
                                           'CREATE_COMBINATION',
                                           'SQLGL',
                                           'GL#',
                                           l_coa_id,
                                           p_concat_segs,
                                           'V',
                                           sysdate,
                                           'ALL', NULL, NULL, NULL, NULL,
                                           FALSE,FALSE, NULL, NULL, NULL);
  if l_keyval_status then
    return 'S';
  else
    return 'F';
  end if;
end create_ccid;
------------------------------------------------------------------------------------------------------------
declare
  l_status varchar2(1);
begin
  l_status := create_ccid('1-9999-9999-9999-9999');
  dbms_output.put_line('Returned: ' || l_status);
  commit;
end;
------------------------------------------------------------------------------------------------------------
select gcc.code_combination_id
,      gcc.concatenated_segments
,      gcc.enabled_flag
,      gcc.start_date_active
,      gcc.end_date_active
,      gcc.detail_posting_allowed
,      gcc.detail_budgeting_allowed
from   gl_code_combinations_kfv gcc
where  concatenated_segments = '1-9999-9999-9999-9999';
------------------------------------------------------------------------------------------------------------
Here Operation can be :

'FIND_COMBINATION' - Combination must already exist.
'CREATE_COMBINATION' - Combination is created if doesn't exist.
'CREATE_COMB_NO_AT' - same as create_combination but does not use an autonomous transaction.
'CHECK_COMBINATION' - Checks if combination valid, doesn't create.
'DEFAULT_COMBINATION' - Returns minimal default combination.
'CHECK_SEGMENTS' - Validates segments individually.

If the above function returns FALSE then use fnd_flex_keyval.error_message() to get the exact FND FLEXFIELD error message to work on.
Another very important function - fnd_flex_keyval.combination_id() returns the ccid of the newly created code combination by the above API.

2. fnd_flex_keyval.validate_ccid():
------------------------------------------------------------------------------------------------------------
Begin
name VARCHAR(2000) := NULL;
result BOOLEAN := TRUE;
begin
IF :from_locator_id IS NULL THEN
RETURN '';
END IF;
result := FND_FLEX_KEYVAL.validate_ccid(appl_short_name=>'INV',
                                                                        key_flex_code=>:P_LOCATOR_FLEX_CODE,
                                                                        structure_number=>:LP_STRUCTURE_NUM,
                                                                        combination_id=>:from_locator_id,
                                                                        data_set=>:ORGANIZATION_ID);

IF result THEN
name := FND_FLEX_KEYVAL.concatenated_values;
END IF;
dbms_output.put_line(name);
end;

------------------------------------------------------------------------------------------------------------
FND_FLEX_KEYVAL.concatenated_values is the API used to concatenate segments of code combination id which was passed in the validate_ccid API.

Wednesday, 19 February 2014

SQL Query Supplier Details

SELECT *
FROM (
  (SELECT POV.VENDOR_ID VENDOR_ID,
    POV.VENDOR_NAME SUPPLIER_NAME ,
    POV.SEGMENT1 SUPPLIER_NUMBER ,
    POV.VENDOR_NAME_ALT ALTERNAETE_NAME ,
    POV.VAT_REGISTRATION_NUM TAX_REGISTRATION_NUMBER ,
    POV.ATTRIBUTE3 VENDOR_TYPE ,
    PLC.DISPLAYED_FIELD SUPPLIER_TYPE ,
    POVSA.VENDOR_SITE_CODE SITE_NAME ,
    FT.TERRITORY_SHORT_NAME COUNTRY ,
    POVSA.ADDRESS_LINE1 ADDRESS_LINE1 ,
    POVSA.ADDRESS_LINE2 ADDRESS_LINE2 ,
    POVSA.ADDRESS_LINE3 ADDRESS_LINE3 ,
    POVSA.ADDRESS_LINE4 ADDRESS_LINE4 ,
    POVSA.CITY CITY ,
    POVSA.STATE STATE ,
    POVSA.ZIP POSTAL_CODE ,
    POVSA.PROVINCE PROVINCE ,
    POVSA.COUNTY COUNTY ,
    POVSA.LANGUAGE LANGUAGE ,
    POVSA.PHONE COMMUNICATION_VOICE ,
    POVSA.FAX COMMUNICATION_FAX ,
    POVSA.EMAIL_ADDRESS COMMUNICATION_EMAIL ,
    POVC.LAST_NAME CONTACT_NAME ,
    POVC.PHONE CONTACT_TELEPHONE ,
    POVC.FAX CONTACT_FAX ,
    POVC.EMAIL_ADDRESS CONTACT_EMAIL ,
    GCC.SEGMENT1
    ||'-'
    ||GCC.SEGMENT2
    ||'-'
    ||GCC.SEGMENT3
    ||'-'
    ||GCC.SEGMENT4
    ||'-'
    ||GCC.SEGMENT5
    ||'-'
    ||GCC.SEGMENT6
    ||'-'
    ||GCC.SEGMENT7
    ||'-'
    ||GCC.SEGMENT8
    ||'-'
    ||GCC.SEGMENT9
    ||'-'
    ||GCC.SEGMENT10 LIABILITY_ACCOUNT ,
    DECODE(POVSA.MATCH_OPTION,'P','Purchase Order','R','Receipt') INVOICE_MATCH_OPTION ,
    POVSA.HOLD_UNMATCHED_INVOICES_FLAG HOLD_UNMATCHED_INVOICE ,
    (SELECT APT.NAME
    FROM AP_TERMS_TL APT
    WHERE APT.TERM_ID = POVSA.TERMS_ID
    AND APT.LANGUAGE  = 'US'
    )PAYMENT_TERMS ,
    POVSA.INVOICE_CURRENCY_CODE INVOICE_CURRENCY ,
    POVSA.PAYMENT_METHOD_LOOKUP_CODE PAYMENT_METHOD ,
    ABA.BANK_ACCOUNT_NAME BANK_ACCOUNT_NAME ,
    ABA.BANK_ACCOUNT_NUM BANK_ACCOUNT_NUMBER ,
    ABB.BANK_NAME BANK_NAME,
    ABB.BANK_NUMBER BANK_NUMBER,
    ABB.BANK_BRANCH_NAME BANK_BRANCH_NAME,
    ABB.BANK_NUM BANK_BRANCH_NUMBER,
    FTB.TERRITORY_SHORT_NAME BANK_COUNTRY,
    POVSA.VAT_CODE INVOICE_TAX_CODE ,
    POV.VAT_REGISTRATION_NUM TAX_REGISTRATION_NUMBER1 ,
    POVSA.TAX_REPORTING_SITE_FLAG INCOME_TAX_REPORTING_SITE ,
    HR_SHIPTO.LOCATION_CODE SHIP_TO_LOCATION ,
    HR_BILLTO.LOCATION_CODE BILL_TO_LOCATION,
    POV.CREATION_DATE CREATION_DATE,
    POV.CREATED_BY CREATED_BY,
    POV.LAST_UPDATE_DATE LAST_UPDATE_DATE,
    POV.LAST_UPDATED_BY LAST_UPDATED_BY,
    POV.LAST_UPDATE_LOGIN LAST_UPDATE_LOGIN
  FROM APPS.PO_VENDORS POV,
    APPS.PO_VENDOR_SITES_ALL POVSA ,
    APPS.FND_TERRITORIES_TL FT ,
    APPS.PO_VENDOR_CONTACTS POVC ,
    APPS.GL_CODE_COMBINATIONS GCC ,
    APPS.HR_LOCATIONS_ALL HR_SHIPTO ,
    APPS.HR_LOCATIONS_ALL HR_BILLTO ,
    APPS.PO_LOOKUP_CODES PLC ,
    APPS.AP_BANK_ACCOUNT_USES_ALL ABAU,
    APPS.AP_BANK_ACCOUNTS_ALL ABA,
    APPS.AP_BANK_BRANCHES ABB,
    APPS.FND_TERRITORIES_TL FTB
  WHERE POV.VENDOR_ID               = POVSA.VENDOR_ID
  AND FT.TERRITORY_CODE             = POVSA.COUNTRY
  AND FT.LANGUAGE                   = 'US'
  AND POVC.VENDOR_SITE_ID(+)        = POVSA.VENDOR_SITE_ID
  AND GCC.CODE_COMBINATION_ID       = POVSA.ACCTS_PAY_CODE_COMBINATION_ID
  AND HR_SHIPTO.LOCATION_ID(+)      = POVSA.SHIP_TO_LOCATION_ID
  AND HR_BILLTO.LOCATION_ID(+)      = POVSA.BILL_TO_LOCATION_ID
  AND PLC.LOOKUP_CODE(+)            = POV.VENDOR_TYPE_LOOKUP_CODE
  AND PLC.LOOKUP_TYPE(+)            = 'VENDOR TYPE'
  AND ABAU.VENDOR_ID                = POVSA.VENDOR_ID(+)
  AND ABAU.VENDOR_SITE_ID           = POVSA.VENDOR_SITE_ID(+)
  AND ABAU.EXTERNAL_BANK_ACCOUNT_ID = ABA.BANK_ACCOUNT_ID(+)
  AND ABA.BANK_BRANCH_ID            = ABB.BANK_BRANCH_ID
  AND FTB.TERRITORY_CODE(+)         = ABB.COUNTRY
  AND FTB.LANGUAGE(+)               = 'US'
  AND POVSA.ORG_ID                  = 2659
  )
UNION ALL
  (SELECT POV.VENDOR_ID VENDOR_ID,
    POV.VENDOR_NAME SUPPLIER_NAME ,
    POV.SEGMENT1 SUPPLIER_NUMBER ,
    POV.VENDOR_NAME_ALT ALTERNAETE_NAME ,
    POV.VAT_REGISTRATION_NUM TAX_REGISTRATION_NUMBER ,
    POV.ATTRIBUTE3 VENDOR_TYPE ,
    PLC.DISPLAYED_FIELD SUPPLIER_TYPE ,
    POVSA.VENDOR_SITE_CODE SITE_NAME ,
    FT.TERRITORY_SHORT_NAME COUNTRY ,
    POVSA.ADDRESS_LINE1 ADDRESS_LINE1 ,
    POVSA.ADDRESS_LINE2 ADDRESS_LINE2 ,
    POVSA.ADDRESS_LINE3 ADDRESS_LINE3 ,
    POVSA.ADDRESS_LINE4 ADDRESS_LINE4 ,
    POVSA.CITY CITY ,
    POVSA.STATE STATE ,
    POVSA.ZIP POSTAL_CODE ,
    POVSA.PROVINCE PROVINCE ,
    POVSA.COUNTY COUNTY ,
    POVSA.LANGUAGE LANGUAGE ,
    POVSA.PHONE COMMUNICATION_VOICE ,
    POVSA.FAX COMMUNICATION_FAX ,
    POVSA.EMAIL_ADDRESS COMMUNICATION_EMAIL ,
    POVC.LAST_NAME CONTACT_NAME ,
    POVC.PHONE CONTACT_TELEPHONE ,
    POVC.FAX CONTACT_FAX ,
    POVC.EMAIL_ADDRESS CONTACT_EMAIL ,
    GCC.SEGMENT1
    ||'-'
    ||GCC.SEGMENT2
    ||'-'
    ||GCC.SEGMENT3
    ||'-'
    ||GCC.SEGMENT4
    ||'-'
    ||GCC.SEGMENT5
    ||'-'
    ||GCC.SEGMENT6
    ||'-'
    ||GCC.SEGMENT7
    ||'-'
    ||GCC.SEGMENT8
    ||'-'
    ||GCC.SEGMENT9
    ||'-'
    ||GCC.SEGMENT10 LIABILITY_ACCOUNT ,
    DECODE(POVSA.MATCH_OPTION,'P','Purchase Order','R','Receipt') INVOICE_MATCH_OPTION ,
    POVSA.HOLD_UNMATCHED_INVOICES_FLAG HOLD_UNMATCHED_INVOICE ,
    (SELECT APT.NAME
    FROM AP_TERMS_TL APT
    WHERE APT.TERM_ID = POVSA.TERMS_ID
    AND APT.LANGUAGE  = 'US'
    )PAYMENT_TERMS ,
    POVSA.INVOICE_CURRENCY_CODE INVOICE_CURRENCY ,
    POVSA.PAYMENT_METHOD_LOOKUP_CODE PAYMENT_METHOD ,
    NULL BANK_ACCOUNT_NAME ,
    NULL BANK_ACCOUNT_NUMBER ,
    NULL BANK_NAME,
    NULL BANK_NUMBER,
    NULL BANK_BRANCH_NAME,
    NULL BANK_BRANCH_NUMBER,
    NULL BANK_COUNTRY,
    POVSA.VAT_CODE INVOICE_TAX_CODE ,
    POV.VAT_REGISTRATION_NUM TAX_REGISTRATION_NUMBER1 ,
    POVSA.TAX_REPORTING_SITE_FLAG INCOME_TAX_REPORTING_SITE ,
    HR_SHIPTO.LOCATION_CODE SHIP_TO_LOCATION ,
    HR_BILLTO.LOCATION_CODE BILL_TO_LOCATION,
    POV.CREATION_DATE CREATION_DATE,
    POV.CREATED_BY CREATED_BY,
    POV.LAST_UPDATE_DATE LAST_UPDATE_DATE,
    POV.LAST_UPDATED_BY LAST_UPDATED_BY,
    POV.LAST_UPDATE_LOGIN LAST_UPDATE_LOGIN
  FROM APPS.PO_VENDORS POV,
    APPS.PO_VENDOR_SITES_ALL POVSA ,
    APPS.FND_TERRITORIES_TL FT ,
    APPS.PO_VENDOR_CONTACTS POVC ,
    APPS.GL_CODE_COMBINATIONS GCC ,
    APPS.HR_LOCATIONS_ALL HR_SHIPTO ,
    APPS.HR_LOCATIONS_ALL HR_BILLTO ,
    APPS.PO_LOOKUP_CODES PLC
  WHERE POV.VENDOR_ID          = POVSA.VENDOR_ID
  AND FT.TERRITORY_CODE        = POVSA.COUNTRY
  AND FT.LANGUAGE              = 'US'
  AND POVC.VENDOR_SITE_ID(+)   = POVSA.VENDOR_SITE_ID
  AND GCC.CODE_COMBINATION_ID  = POVSA.ACCTS_PAY_CODE_COMBINATION_ID
  AND HR_SHIPTO.LOCATION_ID(+) = POVSA.SHIP_TO_LOCATION_ID
  AND HR_BILLTO.LOCATION_ID(+) = POVSA.BILL_TO_LOCATION_ID
  AND PLC.LOOKUP_CODE(+)       = POV.VENDOR_TYPE_LOOKUP_CODE
  AND PLC.LOOKUP_TYPE(+)       = 'VENDOR TYPE'
  AND POVSA.ORG_ID             = 2659
  AND NOT EXISTS
    (SELECT 1
    FROM APPS.AP_BANK_ACCOUNT_USES_ALL
    WHERE VENDOR_SITE_ID = POVSA.VENDOR_SITE_ID
    )
  ) )
ORDER BY VENDOR_ID;

SQL Scripts to get PO Quotations Details

---To extact PO Quotation  headers
SELECT pha.segment1 Documet_number
      ,pha.type_lookup_code document_type_code
      ,pha.quote_type_lookup_code Document_Sub_Type
      ,pv.segment1 Vendor_Number
      ,pvs.Vendor_Site_Code
      ,s_loc.location_code Ship_to_Location
      ,b_loc.location_code Bill_to_location 
      ,pap.employee_number Buyer
      ,pha.currency_code
      ,decode(status_lookup_code,'I','INCOMPLETE','A','APPROVED','C','CLOSED') Approva_status
      ,approved_date
      ,pha.org_id
      ,pha.rate_type
      ,pha.rate_date
      ,pha.creation_date
      ,pha.created_by
FROM apps.po_headers_all pha,
     apps.Po_vendors pv,
     apps.po_vendor_sites_all pvs,
     apps.per_all_people_f  pap,
     apps.hr_locations_all b_loc,
     apps.hr_locations_all s_loc
WHERE pha.vendor_id=pv.vendor_id
AND   pvs.vendor_site_id=pha.vendor_site_id
AND pap.person_id=pha.agent_id
AND b_loc.location_id=pha.bill_to_location_id
AND  s_loc.location_id=pha.ship_to_location_id
AND pha.type_lookup_code='QUOTATION'
AND pha.org_id            =2661
order by 1;

--To find inventory organizations for OU
select organization_id,organization_name from apps.org_organization_definitions where operating_unit=2661;
--To extact PO Quotation Lines
SELECT pha.segment1 Documet_number
      ,pla.line_num
      ,plt.line_type
      ,msi.segment1 Item
      ,pla.unit_meas_lookup_code Unit_Of_Measure
      ,pla.unit_price
      ,pla.vendor_product_num Supplier_Item
      ,pla.creation_date
      ,pla.created_by
FROM apps.po_lines_all pla
    ,apps.po_headers_all pha
   ,(select distinct segment1,inventory_item_id from apps.MTL_SYSTEM_ITEMS_B msi where organization_id in (3519,3619)) msi
   ,apps.po_line_types_tl plt
WHERE pha.po_header_id     =pla.po_header_id
AND pha.type_lookup_code='QUOTATION'
AND plt.line_type_id=pla.line_type_id
and plt.language='US'
AND msi.inventory_item_id=pla.item_id
AND pha.org_id          =2661
order by 1,2 asc;


--To extact Po Quotation line  Breaks
SELECT pha.segment1 Documet_number ,
  pla.line_num ,
  plla.shipment_num ,
  plla.shipment_type,
  msi.segment1 Item ,
  plla.quantity ,
  plla.PRICE_OVERRIDE Unit_Price ,
  plla.unit_meas_lookup_code Unit_Of_Measure ,
  (SELECT ood.organization_code
  FROM apps.ORG_ORGANIZATION_DEFINITIONS ood
  WHERE ood.organization_id=plla.ship_to_organization_id
  ) ship_to_organization_code ,
  plla.start_date ,
  plla.end_date ,
  plla.po_header_id ,
  plla.po_line_id,
  plla.creation_date,
  plla.created_by
FROM apps.po_line_locations_all plla ,
  apps.po_lines_all pla ,
  apps.po_headers_all pha ,
  (select distinct segment1,inventory_item_id from apps.MTL_SYSTEM_ITEMS_B msi where organization_id in (3519,3619)) msi
WHERE plla.po_line_id=pla.po_line_id
AND pha.po_header_id =pla.po_header_id
AND pha.type_lookup_code='QUOTATION'
AND msi.inventory_item_id=pla.item_id
AND pha.org_id           =2661
ORDER BY 1,2,3 ASC;