Need help in writing the query

The initial data is as follows:
ID PARENT_ID
=============
1 NULL
2 NULL
3 2
4 6
5 3
6 7
7 4
8 NULL
but output is as follows in such way that Parent_Id should be displayed in ID place
ID PARENT_ID
=============
1 NULL
2 NULL
3 2
8 NULL
6 8
4 6
5 3
7 4
Can any one help how resolve this query?
Thanks in advance

Your output is inconsistent with initial data :) But
select * from t start with parent_id is null connect by parent_id = prior id;

Similar Messages

  • Need help in refining the query

    Hello Experts,
    Need your help in refining the query further more.
    table structure 
    Mskey  Col A Col B
    1   empno [20141127-20151128]1234
    1   empno [20151201-99991231]232544
    1   salutation [20141127-99991231]Mrs
    1   salutation [20151127-99991231]Mr
    2   empno [20141127-20151128]1234
    2   empno [20151201-99991231]232544
    2   salutation [20141127-99991231]Mrs
    2   salutation [20151127-99991231]Mr
    My requirement is to find the list of overlapping records based on the dates
    user details may be varying from time to time as new data would be pushed through HR systems to identity store via an interface.
    The job is getting failed whenever there is any overlapping with dates. So we proactively decided to schedule a job in this regards which runs weekly and would let us know for which and all the users there is overlapping dates are there, so that we can send the list to HR team for pushing new data.
    Overlapping Issue Example:
    The users employee id for an year it is 1234 and later he moved to another department and his employee id got changed and it became 2345 remaining all details are same. So the HR systems send the data for this user as empno - [20141127-20151128]1234 and empno - [20151201-99991231]232544
    it means from 20141127 to 20151128 his employee no is 1234 and from 20151201 to 99991231 his employee would be 2345.
    This is a correct case and the tool would accept this data.
    the below cases are invald
    Case 1: 1 salutation [20141127-99991231]Mrs 1 salutation [20151127-99991231]Mr
    Case 2: 2 salutation [20141127-99991231]Mrs 2 salutation [20141127-99991231]Mr
    So we wanted to find these overlapping records from tables.
    My Query:
    I am able to successfully write query for the case 2 type but unable to write for case1.
    select id,colA
    count(left(ColB,CHARINDEX(']',ColB))) as 'Count'
    from tblA with (nolock)
    where id in (Select distinct id from tblb with (nolock))
    group by id, cola,left(ColB,CHARINDEX(']',ColB))
    having count(left(ColB,CHARINDEX(']',ColB)))>1

    Finally got the required answer with the below query
    WITH Cte AS
    SELECT ID,ColA,ColB,ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS RN,
    CAST(SUBSTRING(ColB,2,CHARINDEX('-',ColB)-2) AS DATE) AS StartDT,
    CAST(SUBSTRING(ColB,CHARINDEX('-',ColB)+1,8) AS DATE)  AS EndDT  FROM TblA
    SELECT c1.ID, c1.ColA,c1.ColB
    FROM Cte c1 JOIN Cte c2
    ON c1.RN != c2.RN
    AND c1.ID=c2.ID
    AND c1.ColA=c2.ColA
    AND (c1.StartDT BETWEEN c2.StartDT AND c2.EndDT OR c2.StartDT BETWEEN c1.StartDT AND c1.EndDT )

  • Need help in tunnig the query

    Experts,
    Plz help in tuning this query
    SELECT
    DECODE(WSH_DELIVERY_DETAILS.LOT_NUMBER,'5I27164/1U',1, TO_NUMBER(SUBSTR(WSH_DELIVERY_DETAILS.LOT_NUMBER, (LENGTH(WSH_DELIVERY_DETAILS.LOT_NUMBER) - INSTR(REVERSE(WSH_DELIVERY_DETAILS.LOT_NUMBER),'/') + 2), (INSTR(REVERSE(WSH_DELIVERY_DETAILS.LOT_NUMBER),'/')-1))) ) AS NO_OF_PLATES,
         P_FORM.DESCRIPTION FORMOFPRODUCT,
    HZ_PARTIES.PARTY_NAME CUSTOMER, HZ_CUST_ACCOUNTS.ACCOUNT_NUMBER ACCT_NUM,
         NVL(OE_ORDER_HEADERS_ALL.CUST_PO_NUMBER,'XXXX') CUST_PO,
         INITCAP(HZ_PARTIES.ADDRESS1) BILL_ADD1,
    INITCAP(HZ_PARTIES.ADDRESS2) BILL_ADD2,
    INITCAP(HZ_PARTIES.ADDRESS3) BILL_ADD3,
    INITCAP(HZ_PARTIES.ADDRESS4) BILL_ADD4,
         INITCAP(HZ_PARTIES.CITY) BILL_CITY,
    INITCAP(HZ_PARTIES.STATE) BILL_STATE,
    HZ_PARTIES.POSTAL_CODE BILL_PC,
         INITCAP(HZ_LOCATIONS.ADDRESS1) SHIP_ADD1,
    INITCAP(HZ_LOCATIONS.ADDRESS2) SHIP_ADD2,
    INITCAP(HZ_LOCATIONS.ADDRESS3) SHIP_ADD3,
    INITCAP(HZ_LOCATIONS.ADDRESS4) SHIP_ADD4,
         INITCAP(HZ_LOCATIONS.CITY) SHIP_CITY,
    INITCAP(HZ_LOCATIONS.STATE) SHIP_STATE,
    HZ_LOCATIONS.POSTAL_CODE SHIP_PC,
         OE_TRANSACTION_TYPES_TL.NAME CATEGORY ,
         OE_ORDER_HEADERS_ALL.ORDER_NUMBER,
    OE_ORDER_HEADERS_ALL.SOURCE_DOCUMENT_ID,
    OE_ORDER_HEADERS_ALL.HEADER_ID,
    OE_ORDER_HEADERS_ALL.FREIGHT_TERMS_CODE,
    /* to_char(oe_order_headers_all.ORDERED_DATE,'DD-MON-RR HH24:MI:SS') ORDER_DATE,*/
    to_char(sysdate,'DD/MM/RRRR HH24:MI:SS') ORDERED_DATE,
         ROWNUM,
    OE_ORDER_LINES_ALL.INVENTORY_ITEM_ID,
         OE_ORDER_LINES_ALL.ORDERED_ITEM,
         OE_ORDER_LINES_ALL.ORDERED_QUANTITY,
         WSH_DELIVERY_DETAILS.SHIPPED_QUANTITY+DECODE(WSH_DELIVERY_DETAILS.DELIVERY_DETAIL_ID,129881,.22,0) AS SHIPPED_QUANTITY1, (WSH_DELIVERY_DETAILS.SHIPPED_QUANTITY+DECODE(WSH_DELIVERY_DETAILS.DELIVERY_DETAIL_ID,129881,.22,0))*OE_ORDER_LINES_ALL.UNIT_SELLING_PRICE AS LINE_PRICE,
         OE_ORDER_LINES_ALL.UNIT_SELLING_PRICE,
         JA_IN_RG_I.FOR_HOME_USE_PAY_ED_VAL,
    ----      SUBSTR(JA_IN_RG_I.EXCISE_INVOICE_NUMBER,1,2)|| SUBSTR(JA_IN_RG_I.EXCISE_INVOICE_NUMBER,4,8) INVOICE_NO,
         JA_IN_RG_I.EXCISE_INVOICE_NUMBER INVOICE_NO,
    to_char(JA_IN_RG_I.CREATION_DATE,'DD/MM/RRRR') INVOICE_DATE1,
         to_char(JA_IN_RG_I.EXCISE_INVOICE_DATE,'DD-MON-RR HH24:MI:SS') INV_DATE,
         to_char(JA_IN_RG_I.CREATION_DATE,'DD/MM/RRRR HH24:MI:SS') INVOICE_DATE,
         JA_IN_RG_I.EXCISE_DUTY_RATE,
         JA_IN_RG_I.EXCISE_DUTY_AMOUNT,
         WSH_DELIVERY_DETAILS.LOT_NUMBER,
    WSH_TRIPS.CARRIER_ID CARRIER_ID,
    WSH_TRIPS.VEHICLE_NUM_PREFIX,
    WSH_TRIPS.SEAL_CODE,
    WSH_TRIPS.ROUTING_INSTRUCTIONS,
    WSH_TRIPS.OPERATOR, WSH_NEW_DELIVERIES. DELIVERY_ID,
    WSH_NEW_DELIVERIES. ADDITIONAL_SHIPMENT_INFO TRUCK_NO,
         WSH_TRIPS.MODE_OF_TRANSPORT VEHICLE_TYPE,
    WSH_TRIPS.ATTRIBUTE6||' '|| WSH_TRIPS.ATTRIBUTE7||' '|| WSH_TRIPS.ATTRIBUTE8 REMARKS,
         WSH_TRIPS.ATTRIBUTE1 LR_NO, WSH_TRIPS.ATTRIBUTE7,
         WSH_TRIPS.ATTRIBUTE2 LR_DATE,
         DECODE(WSH_TRIPS.ATTRIBUTE3, '', '', 'ARE NO '||WSH_TRIPS.ATTRIBUTE3) AS ARE_NO,
         DECODE(WSH_TRIPS.ATTRIBUTE6, '', '', WSH_TRIPS.ATTRIBUTE6) AS REMARK1,
         DECODE(WSH_TRIPS.ATTRIBUTE4, '', '', 'R.P. NO. ' ||WSH_TRIPS.ATTRIBUTE4) AS PERMIT,
         DECODE(WSH_TRIPS.ATTRIBUTE5, '', '', 'Export Under '||WSH_TRIPS.ATTRIBUTE5) AS EXPORT_UNDER,
         DECODE(WSH_NEW_DELIVERIES.PORT_OF_DISCHARGE,'','', 'Seal No : '||WSH_NEW_DELIVERIES.PORT_OF_DISCHARGE) AS SEAL_NO,
         DECODE(WSH_NEW_DELIVERIES.DESCRIPTION, '', '', 'Container No : ' ||WSH_NEW_DELIVERIES.DESCRIPTION) AS CONTAINER_NO,
         MTL_SYSTEM_ITEMS_B.ATTRIBUTE4 AS EXCISE_TARIFF_NO,
         --MTL_SYSTEM_ITEMS_B.DESCRIPTION,
         HZ_CUST_ACCOUNTS.ACCOUNT_NUMBER,
         JSW_LOT_PACKSLIP.PACKSLIP_NO PACKSLIP,
         PR_TYPE.DESCRIPTION ||' ,'||
         P_FORM.DESCRIPTION || ' ,'||
         PR_GRADE.DESCRIPTION ||' ,'||
         PR_QTY.DESCRIPTION || ' ,'||
         DECODE(SUBSTR(PR_STL.DESCRIPTION,1,3),'Not','', PR_STL.DESCRIPTION) DESCRIPTION ,
    HZ_CUST_ACCT_SITES_ALL.ATTRIBUTE7 S_CST_NO,
    HZ_CUST_ACCT_SITES_ALL.ATTRIBUTE6 S_LST_NO,
    HZ_CUST_ACCT_SITES_ALL.ATTRIBUTE5 S_ECC_NO,
    A.ATTRIBUTE7 B_CST_NO,
    A.ATTRIBUTE6 B_LST_NO,
    INTERFACECRM.JSW_MES_COMMON_TNGL_T.JSW_ADDMINUTES(WSH_TRIPS.VEHICLE_NUM_PREFIX) POSTFIX_NUM
    FROM -- jsw_vat_slno,
         MTL_PARAMETERS,
         HZ_PARTIES,
         HZ_CUST_ACCOUNTS,
         HZ_LOCATIONS,
         HZ_PARTY_SITES,
         HZ_CUST_ACCT_SITES_ALL,
         HZ_CUST_SITE_USES_ALL,
    HZ_CUST_ACCT_SITES_ALL A,
    HZ_CUST_SITE_USES_ALL B,
         OE_TRANSACTION_TYPES_TL,
         JA_IN_RG_I,
         WSH_DELIVERY_DETAILS,
         OE_ORDER_LINES_ALL,
         OE_ORDER_HEADERS_ALL,
         WSH_DELIVERY_ASSIGNMENTS,
         WSH_NEW_DELIVERIES,
         WSH_DELIVERY_LEGS,
         WSH_TRIP_STOPS,
         WSH_TRIPS,
         MTL_SYSTEM_ITEMS_B,
         JSW_LOT_PACKSLIP,
         IC_TRAN_PND,
                   JSW_ITM_SEARCH_PRD_TYP          PR_TYPE,
              JSW_ITM_SEARCH_PRD_FRM          P_FORM     ,
                   JSW_ITM_SEARCH_PRD_GRD          PR_GRADE,      
              JSW_ITM_SEARCH_QUALITY_LEVEL PR_QTY,      
                   JSW_ITEM_SEARCH_SLITTING      PR_STL
    WHERE /* jsw_vat_slno.excise_invoice_number=ja_in_rg_i.excise_invoice_number and jsw_vat_slno.organization_id = ja_in_rg_i.organization_id */
    HZ_PARTIES.PARTY_ID = HZ_CUST_ACCOUNTS.PARTY_ID
         AND HZ_CUST_ACCOUNTS.CUST_ACCOUNT_ID = OE_ORDER_HEADERS_ALL.SOLD_TO_ORG_ID
         AND OE_ORDER_HEADERS_ALL.SHIP_TO_ORG_ID = HZ_CUST_SITE_USES_ALL.SITE_USE_ID
         AND HZ_CUST_SITE_USES_ALL.CUST_ACCT_SITE_ID = HZ_CUST_ACCT_SITES_ALL.CUST_ACCT_SITE_ID
    AND OE_ORDER_HEADERS_ALL.INVOICE_TO_ORG_ID = B.SITE_USE_ID
         AND B.CUST_ACCT_SITE_ID                          = A.CUST_ACCT_SITE_ID
         AND HZ_CUST_ACCT_SITES_ALL.PARTY_SITE_ID      = HZ_PARTY_SITES.PARTY_SITE_ID
         AND HZ_PARTY_SITES.LOCATION_ID = HZ_LOCATIONS.LOCATION_ID
         AND OE_ORDER_LINES_ALL.SHIP_FROM_ORG_ID = MTL_PARAMETERS.ORGANIZATION_ID
         AND OE_ORDER_HEADERS_ALL.ORDER_TYPE_ID = OE_TRANSACTION_TYPES_TL.TRANSACTION_TYPE_ID
    and upper(oe_transaction_types_tl.description) not like '%SUP%'
         AND OE_ORDER_HEADERS_ALL.HEADER_ID = WSH_DELIVERY_DETAILS.SOURCE_HEADER_ID
         AND OE_ORDER_HEADERS_ALL.HEADER_ID = OE_ORDER_LINES_ALL.HEADER_ID
         AND OE_ORDER_LINES_ALL.LINE_ID = WSH_DELIVERY_DETAILS.SOURCE_LINE_ID
         AND TO_CHAR(WSH_DELIVERY_DETAILS.DELIVERY_DETAIL_ID ) = JA_IN_RG_I.REF_DOC_ID
         AND WSH_DELIVERY_DETAILS.DELIVERY_DETAIL_ID = WSH_DELIVERY_ASSIGNMENTS.DELIVERY_DETAIL_ID
         AND WSH_DELIVERY_ASSIGNMENTS.DELIVERY_ID = WSH_NEW_DELIVERIES.DELIVERY_ID
         AND WSH_NEW_DELIVERIES.DELIVERY_ID = WSH_DELIVERY_LEGS.DELIVERY_ID
         AND WSH_DELIVERY_LEGS.PICK_UP_STOP_ID = WSH_TRIP_STOPS.STOP_ID
         AND WSH_TRIP_STOPS.TRIP_ID                = WSH_TRIPS.TRIP_ID
    /* Input parameter can be either Invoice number or Delivery Id */
         AND (WSH_NEW_DELIVERIES.DELIVERY_ID = :DEL_ID OR JA_IN_RG_I.EXCISE_INVOICE_NUMBER = :INVOICE_NUMBER)
         AND NVL(WSH_DELIVERY_DETAILS.SHIPPED_QUANTITY,0) <>0
         AND     MTL_SYSTEM_ITEMS_B.ORGANIZATION_ID     = WSH_DELIVERY_DETAILS.ORGANIZATION_ID
         AND     MTL_SYSTEM_ITEMS_B.INVENTORY_ITEM_ID = WSH_DELIVERY_DETAILS.INVENTORY_ITEM_ID
         AND IC_TRAN_PND.LINE_DETAIL_ID               = WSH_DELIVERY_DETAILS.DELIVERY_DETAIL_ID
         AND     JSW_LOT_PACKSLIP.LOT_ID                    = IC_TRAN_PND.LOT_ID(+)
         AND     JSW_LOT_PACKSLIP.DELIVERY_ID          = WSH_NEW_DELIVERIES.DELIVERY_ID(+)
         AND      IC_TRAN_PND.LINE_ID                    = OE_ORDER_LINES_ALL.LINE_ID                                                  
         AND     IC_TRAN_PND.DELETE_MARK                = 0
         AND IC_TRAN_PND.COMPLETED_IND      = 1
         AND      IC_TRAN_PND.STAGED_IND                    =     1
         AND     P_FORM.CODE(+)                                   = substr(OE_ORDER_LINES_ALL.ORDERED_ITEM,3,2)
         AND     PR_TYPE.CODE                                   = substr(OE_ORDER_LINES_ALL.ORDERED_ITEM,1,2)
         AND     PR_TYPE.CODE                                   = P_FORM.PRD_TYPE_CODE
         AND     P_FORM.CODE                                   = substr(OE_ORDER_LINES_ALL.ORDERED_ITEM,3,2)
    AND     P_FORM.CODE                                   = PR_GRADE.PRD_FRM_CODE
         AND     PR_GRADE.CODE                                   = substr(OE_ORDER_LINES_ALL.ORDERED_ITEM,5,2)     
         AND     PR_GRADE.CODE                                   = PR_QTY.PRD_GRD_CODE
         AND     PR_QTY.CODE                                   = substr(OE_ORDER_LINES_ALL.ORDERED_ITEM,7,2)
         AND     PR_STL.PROD_CODE                              = PR_TYPE.CODE
         AND     PR_STL.QUALITY_LEVEL_CODE                    = PR_QTY.CODE
         AND     PR_STL.CODE                                   = substr(OE_ORDER_LINES_ALL.ORDERED_ITEM,9,2)
         AND rownum=1
    thanks,
    baskar.l

    Here is the explain plan
    PLAN_TABLE_OUTPUT
    | Id | Operation | Name | Rows | Bytes | Cost |
    | 0 | SELECT STATEMENT | | 1 | 920 | 12628 |
    | 1 | COUNT STOPKEY | | | | |
    | 2 | NESTED LOOPS | | 1 | 920 | 12628 |
    | 3 | NESTED LOOPS | | 1 | 728 | 12627 |
    | 4 | NESTED LOOPS | | 1 | 718 | 12626 |
    | 5 | NESTED LOOPS | | 1 | 684 | 12625 |
    | 6 | NESTED LOOPS | | 1 | 674 | 12624 |
    | 7 | NESTED LOOPS | | 1 | 665 | 12623 |
    | 8 | NESTED LOOPS | | 1 | 655 | 12622 |
    | 9 | NESTED LOOPS | | 1 | 532 | 12621 |
    | 10 | NESTED LOOPS | | 1 | 522 | 12620 |
    | 11 | FILTER | | | | |
    | 12 | NESTED LOOPS OUTER | | | | |
    | 13 | NESTED LOOPS | | 1 | 494 | 12617 |
    | 14 | NESTED LOOPS | | 1 | 477 | 12614 |
    | 15 | NESTED LOOPS | | 1 | 467 | 12611 |
    | 16 | NESTED LOOPS | | 1 | 382 | 12610 |
    | 17 | NESTED LOOPS | | 1 | 366 | 12609 |
    | 18 | NESTED LOOPS | | 1 | 321 | 12606 |
    | 19 | FILTER | | | | |
    | 20 | HASH JOIN OUTER | | | | |
    | 21 | TABLE ACCESS BY INDEX ROWID| IC_TRAN_PND | 1 | 22 | 3 |
    | 22 | NESTED LOOPS | | 1 | 292 | 12601 |
    | 23 | HASH JOIN | | 1 | 270 | 12598 |
    | 24 | NESTED LOOPS | | 4011 | 908K| 6272 |
    | 25 | HASH JOIN | | 4012 | 893K| 6272 |
    | 26 | TABLE ACCESS FULL | JSW_ITM_SEARCH_PRD_GRD | 33 | 759 | 2 |
    | 27 | HASH JOIN | | 20547 | 4113K| 6269 |
    | 28 | TABLE ACCESS FULL | JSW_ITM_SEARCH_QUALITY_LEVEL | 54 | 1296 | 2 |
    | 29 | HASH JOIN | | 24352 | 4304K| 6266 |
    | 30 | TABLE ACCESS FULL | JSW_ITM_SEARCH_PRD_TYP | 7 | 154 | 2 |
    | 31 | HASH JOIN | | 170K| 25M| 6260 |
    | 32 | TABLE ACCESS FULL | JSW_ITEM_SEARCH_SLITTING | 73 | 1971 | 2 |
    | 33 | HASH JOIN | | 44367 | 5719K| 6257 |
    | 34 | HASH JOIN | | 4469 | 410K| 367 |
    | 35 | TABLE ACCESS FULL| OE_TRANSACTION_TYPES_TL | 18 | 846 | 4 |
    | 36 | TABLE ACCESS FULL| OE_ORDER_HEADERS_ALL | 38240 | 1755K| 362 |
    | 37 | TABLE ACCESS FULL | OE_ORDER_LINES_ALL | 379K| 13M| 5761 |
    | 38 | INDEX UNIQUE SCAN | MTL_PARAMETERS_U1 | 1 | 4 | |
    | 39 | TABLE ACCESS FULL | WSH_DELIVERY_DETAILS | 579K| 21M| 5926 |
    | 40 | INDEX RANGE SCAN | IC_TRAN_PND_N1 | 2 | | 2 |
    | 41 | TABLE ACCESS FULL | JSW_ITM_SEARCH_PRD_FRM | 16 | 272 | 2 |
    | 42 | TABLE ACCESS BY INDEX ROWID | MTL_SYSTEM_ITEMS_B | 1 | 12 | 2 |
    | 43 | INDEX UNIQUE SCAN | MTL_SYSTEM_ITEMS_B_U1 | 1 | | 1 |
    | 44 | TABLE ACCESS BY INDEX ROWID | JA_IN_RG_I | 1 | 45 | 3 |
    | 45 | INDEX RANGE SCAN | JA_IN_RG_I_N3 | 1 | | 2 |
    | 46 | TABLE ACCESS BY INDEX ROWID | HZ_CUST_ACCOUNTS | 1 | 16 | 1 |
    | 47 | INDEX UNIQUE SCAN | HZ_CUST_ACCOUNTS_U1 | 1 | | |
    | 48 | TABLE ACCESS BY INDEX ROWID | HZ_PARTIES | 1 | 85 | 1 |
    | 49 | INDEX UNIQUE SCAN | HZ_PARTIES_U1 | 1 | | |
    | 50 | TABLE ACCESS BY INDEX ROWID | WSH_DELIVERY_ASSIGNMENTS | 1 | 10 | 3 |
    | 51 | INDEX RANGE SCAN | WSH_DELIVERY_ASSIGNMENTS_N3 | 1 | | 2 |
    | 52 | TABLE ACCESS BY INDEX ROWID | JSW_LOT_PACKSLIP | 1 | 17 | 3 |
    | 53 | INDEX RANGE SCAN | JSW_LOT_PACKSLIP_N1 | 1 | | 2 |
    | 54 | TABLE ACCESS BY INDEX ROWID | WSH_NEW_DELIVERIES | 1 | 18 | 1 |
    | 55 | INDEX UNIQUE SCAN | WSH_NEW_DELIVERIES_U1 | 1 | | |
    | 56 | TABLE ACCESS BY INDEX ROWID | WSH_DELIVERY_LEGS | 1 | 10 | 2 |
    | 57 | INDEX RANGE SCAN | WSH_DELIVERY_LEGS_N1 | 1 | | 1 |
    | 58 | TABLE ACCESS BY INDEX ROWID | WSH_TRIP_STOPS | 1 | 10 | 1 |
    | 59 | INDEX UNIQUE SCAN | WSH_TRIP_STOPS_U1 | 1 | | |
    | 60 | TABLE ACCESS BY INDEX ROWID | WSH_TRIPS | 1 | 123 | 1 |
    | 61 | INDEX UNIQUE SCAN | WSH_TRIPS_U1 | 1 | | |
    | 62 | TABLE ACCESS BY INDEX ROWID | HZ_CUST_SITE_USES_ALL | 1 | 10 | 1 |
    | 63 | INDEX UNIQUE SCAN | HZ_CUST_SITE_USES_U1 | 1 | | |
    | 64 | TABLE ACCESS BY INDEX ROWID | HZ_CUST_ACCT_SITES_ALL | 1 | 9 | 1 |
    | 65 | INDEX UNIQUE SCAN | HZ_CUST_ACCT_SITES_U1 | 1 | | |
    | 66 | TABLE ACCESS BY INDEX ROWID | HZ_CUST_SITE_USES_ALL | 1 | 10 | 1 |
    | 67 | INDEX UNIQUE SCAN | HZ_CUST_SITE_USES_U1 | 1 | | |
    | 68 | TABLE ACCESS BY INDEX ROWID | HZ_CUST_ACCT_SITES_ALL | 1 | 34 | 1 |
    | 69 | INDEX UNIQUE SCAN | HZ_CUST_ACCT_SITES_U1 | 1 | | |
    | 70 | TABLE ACCESS BY INDEX ROWID | HZ_PARTY_SITES | 1 | 10 | 1 |
    | 71 | INDEX UNIQUE SCAN | HZ_PARTY_SITES_U1 | 1 | | |
    | 72 | TABLE ACCESS BY INDEX ROWID | HZ_LOCATIONS | 1 | 192 | 1 |
    | 73 | INDEX UNIQUE SCAN | HZ_LOCATIONS_U1 | 1 | | |
    we need to tune this query to reduce it time of execution as our users are very much affected in running requests.
    thanks,
    baskar.l

  • Need help in highlighting the query text in document

    Hi, I am trying to load the files in the blob column and trying to create the text index on it.
    i need to query the blob column in the document table with a string, which needs to return the relevant documents with the query string highlighted with some color.
    Can you please help me with an example on the above.
    Thanks in advance.

    SCOTT@orcl_11gR2> -- table:
    SCOTT@orcl_11gR2> CREATE TABLE document_tab
      2    (document_col  BLOB)
      3  /
    Table created.
    SCOTT@orcl_11gR2> -- procedure to load documents:
    SCOTT@orcl_11gR2> CREATE OR REPLACE PROCEDURE load_document
      2    (p_dir  IN VARCHAR2,
      3       p_file IN VARCHAR2)
      4  AS
      5    v_blob       BLOB;
      6    v_bfile       BFILE;
      7  BEGIN
      8    INSERT INTO document_tab (document_col)
      9    VALUES (EMPTY_BLOB())
    10    RETURNING document_col INTO v_blob;
    11    v_bfile := BFILENAME (UPPER (p_dir), p_file);
    12    DBMS_LOB.FILEOPEN (v_bfile, DBMS_LOB.LOB_READONLY);
    13    DBMS_LOB.LOADFROMFILE (v_blob, v_bfile, DBMS_LOB.GETLENGTH (v_bfile));
    14    DBMS_LOB.FILECLOSE (v_bfile);
    15  END load_document;
    16  /
    Procedure created.
    SCOTT@orcl_11gR2> SHOW ERRORS
    No errors.
    SCOTT@orcl_11gR2> -- load documents (directory and files must be on server, not client):
    SCOTT@orcl_11gR2> CREATE OR REPLACE DIRECTORY my_dir AS 'c:\my_oracle_files'
      2  /
    Directory created.
    SCOTT@orcl_11gR2> BEGIN
      2    load_document ('my_dir', 'banana.pdf');
      3    load_document ('my_dir', 'english.doc');
      4    load_document ('my_dir', 'sample.txt');
      5  END;
      6  /
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> -- confirm files were loaded:
    SCOTT@orcl_11gR2> SELECT DBMS_LOB.GETLENGTH (document_col)
      2  FROM   document_tab
      3  /
    DBMS_LOB.GETLENGTH(DOCUMENT_COL)
                              222824
                               22016
                                  60
    3 rows selected.
    SCOTT@orcl_11gR2> -- text index:
    SCOTT@orcl_11gR2> CREATE INDEX document_idx
      2  ON document_tab (document_col)
      3  INDEXTYPE IS CTXSYS.CONTEXT
      4  /
    Index created.
    SCOTT@orcl_11gR2> -- confirm files were indexed:
    SCOTT@orcl_11gR2> SELECT COUNT(*) FROM dr$document_idx$i
      2  /
      COUNT(*)
           319
    1 row selected.
    SCOTT@orcl_11gR2> -- function to return highlighted document:
    SCOTT@orcl_11gR2> CREATE OR REPLACE FUNCTION your_markup
      2    (p_index_name IN VARCHAR2,
      3       p_textkey    IN VARCHAR2,
      4       p_text_query IN VARCHAR2,
      5       p_plaintext  IN BOOLEAN  DEFAULT TRUE,
      6       p_tagset     IN VARCHAR2 DEFAULT 'HTML_DEFAULT',
      7       p_starttag   IN VARCHAR2 DEFAULT '*',
      8       p_endtag     IN VARCHAR2 DEFAULT '*',
      9       p_key_type   IN VARCHAR2 DEFAULT 'ROWID')
    10    RETURN          CLOB
    11  AS
    12    v_clob          CLOB;
    13  BEGIN
    14    CTX_DOC.SET_KEY_TYPE (p_key_type);
    15    CTX_DOC.MARKUP
    16        (index_name => p_index_name,
    17         textkey    => p_textkey,
    18         text_query => p_text_query,
    19         restab     => v_clob,
    20         plaintext  => p_plaintext,
    21         tagset     => p_tagset,
    22         starttag   => p_starttag,
    23         endtag     => p_endtag);
    24    RETURN v_clob;
    25  END your_markup;
    26  /
    Function created.
    SCOTT@orcl_11gR2> SHOW ERRORS
    No errors.
    SCOTT@orcl_11gR2> -- query that returns highlighted document:
    SCOTT@orcl_11gR2> VARIABLE string VARCHAR2(100)
    SCOTT@orcl_11gR2> EXEC :string := 'test AND demonstration'
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> SELECT your_markup ('document_idx', ROWID, :string)
      2           AS highlighted_text
      3  FROM   document_tab
      4  WHERE  CONTAINS (document_col, :string) > 0
      5  /HIGHLIGHTED_TEXT
    This is a test document for demonstration of highlighting.
    1 row selected.
    SCOTT@orcl_11gR2>

  • Need help in optimizing the query with joins and group by clause

    I am having problem in executing the query below.. it is taking lot of time. To simplify, I have added the two tables FILE_STATUS = stores the file load details and COMM table that is actual business commission table showing records successfully processed and which records were transmitted to other system. Records with status = T is trasnmitted to other system and traansactions with P is pending.
    CREATE TABLE FILE_STATUS
    (FILE_ID VARCHAR2(14),
    FILE_NAME VARCHAR2(20),
    CARR_CD VARCHAR2(5),
    TOT_REC NUMBER,
    TOT_SUCC NUMBER);
    CREATE TABLE COMM
    (SRC_FILE_ID VARCHAR2(14),
    REC_ID NUMBER,
    STATUS CHAR(1));
    INSERT INTO FILE_STATUS VALUES ('12345678', 'CM_LIBM.TXT', 'LIBM', 5, 4);
    INSERT INTO FILE_STATUS VALUES ('12345679', 'CM_HIPNT.TXT', 'HIPNT', 4, 0);
    INSERT INTO COMM VALUES ('12345678', 1, 'T');
    INSERT INTO COMM VALUES ('12345678', 3, 'T');
    INSERT INTO COMM VALUES ('12345678', 4, 'P');
    INSERT INTO COMM VALUES ('12345678', 5, 'P');
    COMMIT;Here is the query that I wrote to give me the details of the file that has been loaded into the system. It reads the file status and commission table to show file name, total records loaded, total records successfully loaded to the commission table and number of records that has been finally transmitted (status=T) to other systems.
    SELECT
        FS.CARR_CD
        ,FS.FILE_NAME
        ,FS.FILE_ID
        ,FS.TOT_REC
        ,FS.TOT_SUCC
        ,NVL(C.TOT_TRANS, 0) TOT_TRANS
    FROM FILE_STATUS FS
    LEFT JOIN
        SELECT SRC_FILE_ID, COUNT(*) TOT_TRANS
        FROM COMM
        WHERE STATUS = 'T'
        GROUP BY SRC_FILE_ID
    ) C ON C.SRC_FILE_ID = FS.FILE_ID
    WHERE FILE_ID = '12345678';In production this query has more joins and is taking lot of time to process.. the main culprit for me is the join on COMM table to get the count of number of transactions transmitted. Please can you give me tips to optimize this query to get results faster? Do I need to remove group and use partition or something else. Please help!

    I get 2 rows if I use my query with your new criteria. Did you commit the record if you are using a second connection to query? Did you remove the criteria for file_id?
    select carr_cd, file_name, file_id, tot_rec, tot_succ, tot_trans
      from (select fs.carr_cd,
                   fs.file_name,
                   fs.file_id,
                   fs.tot_rec,
                   fs.tot_succ,
                   count(case
                            when c.status = 'T' then
                             1
                            else
                             null
                          end) over(partition by c.src_file_id) tot_trans,
                   row_number() over(partition by c.src_file_id order by null) rn
              from file_status fs
              left join comm c
                on c.src_file_id = fs.file_id
             where carr_cd = 'LIBM')
    where rn = 1;
    CARR_CD FILE_NAME            FILE_ID           TOT_REC   TOT_SUCC  TOT_TRANS
    LIBM    CM_LIBM.TXT          12345678                5          4          2
    LIBM    CM_LIBM.TXT          12345677               10          0          0Using RANK can potentially produce multiple rows to be returned though your data may prevent this. ROW_NUMBER will always prevent duplicates. The ordering of the analytical function is irrelevant in your query if you use ROW_NUMBER. You can remove the outermost query and inspect the data returned by the inner query;
    select fs.carr_cd,
           fs.file_name,
           fs.file_id,
           fs.tot_rec,
           fs.tot_succ,
           count(case
                    when c.status = 'T' then
                     1
                    else
                     null
                  end) over(partition by c.src_file_id) tot_trans,
           row_number() over(partition by c.src_file_id order by null) rn
    from file_status fs
    left join comm c
    on c.src_file_id = fs.file_id
    where carr_cd = 'LIBM';
    CARR_CD FILE_NAME            FILE_ID           TOT_REC   TOT_SUCC  TOT_TRANS         RN
    LIBM    CM_LIBM.TXT          12345678                5          4          2          1
    LIBM    CM_LIBM.TXT          12345678                5          4          2          2
    LIBM    CM_LIBM.TXT          12345678                5          4          2          3
    LIBM    CM_LIBM.TXT          12345678                5          4          2          4
    LIBM    CM_LIBM.TXT          12345677               10          0          0          1

  • Need help with writing a query with dynamic FROM clause

    Hi Folks,
    I need help with an query that should generate the "FROM" clause dynamically.
    My main query is as follows
    select DT_SKEY, count(*)
    from *???*
    where DT_SKEY between 20110601 and 20110719
    group by DT_SKEY
    having count(*) = 0
    order by 1; The "from" clause of the above query should be generated as below
    select 'Schema_Name'||'.'||TABLE_NAME
    from dba_tables
    where OWNER = 'Schema_Name'Simply sticking the later query in the first query does not work.
    Any pointers will be appreciated.
    Thanks
    rogers42

    Hi,
    rogers42 wrote:
    Hi Folks,
    I need help with an query that should generate the "FROM" clause dynamically.
    My main query is as follows
    select DT_SKEY, count(*)
    from *???*
    where DT_SKEY between 20110601 and 20110719
    group by DT_SKEY
    having count(*) = 0
    order by 1; The "from" clause of the above query should be generated as below
    select 'Schema_Name'||'.'||TABLE_NAME
    from dba_tables
    where OWNER = 'Schema_Name'
    Remember that anything inside quotes is case-sensitive. Is the owner really "Schema_Name" with a capital S and a capital N, and 8 lower-case letters?
    Simply sticking the later query in the first query does not work.Right; the table name must be given when you compile the query. It's not an expression that you can generate in the query itself.
    Any pointers will be appreciated.In SQL*Plus, you can do something like the query bleow.
    Say you want to count the rows in scott.emp, but you're not certain that the name is emp; it could be emp_2011 or emp_august, or anything else that starts with e. (And the name could change every day, so you can't just look it up now and hard-code it in a query that you want to run in the future.)
    Typically, how dynamic SQL works is that some code (such as a preliminary query) gets some of the information you need to write the query first, and you use that information in a SQL statement that is compiled and run after that. For example:
    -- Preliminary Query:
    COLUMN     my_table_name_col     NEW_VALUE my_table_name
    SELECT     table_name     AS my_table_name_col
    FROM     all_tables
    WHERE     owner          = 'SCOTT'
    AND     table_name     LIKE 'E%';
    -- Main Query:
    SELECT     COUNT (*)     AS cnt
    FROM     scott.&my_table_name
    ;This assumes that the preliminary query will find exactly one row; that is, it assumes that SCOTT has exactly one table whose name starts with E. Could you have 0 tables in the schema, or more than 1? If so, what results would you want? Give a concrete example, preferably suing commonly available tables (like those in the SCOTT schema) so that the poepl who want to help you can re-create the problem and test their ideas.
    Edited by: Frank Kulash on Aug 11, 2011 2:30 PM

  • Need help in writing inline query or subquery

    Hi All,
    I have 4 select statements like below ,
    1>Select customer_number, customer_name,customer_address1
    FROM ABC
    WHERE Site_use_code='BILL_TO'
    2>Select customer_number, customer_name,customer_address1
    FROM ABC
    WHERE Site_use_code='SHIP_TO'
    3>Select customer_number, customer_name,customer_address1
    FROM ABC
    WHERE Site_use_code='SOLD_TO'
    I need to write all these 3 in single select statement in such a way that if any select statement doesnot return any data it should execute the other select statements means it should not fail.
    for each column of the select statment i can write separate select statements in that way it should work but the query should be very big.
    So please help me on thsi.
    Thanks

    Example of my solution:
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (
      2    select 1 as customer_number, 'Fred' as customer_name, 'address1' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      3    select 1 as customer_number, 'Fred' as customer_name, 'address1' as customer_address1, 'SHIP_TO' as site_use_code from dual union all
      4    select 2 as customer_number, 'Bob' as customer_name, 'address2' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      5    select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      6    select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'SHIP_TO' as site_use_code from dual union all
      7    select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'SOLD_TO' as site_use_code from dual union all
      8    select 4 as customer_number, 'Tim' as customer_name, 'address4' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      9    select 5 as customer_number, 'Sam' as customer_name, 'address5' as customer_address1, 'BILL_TO' as site_use_code from dual
    10  )
    11  --
    12  -- END OF TEST DATA
    13  --
    14  select customer_number
    15        ,customer_name
    16        ,customer_address1
    17        ,site_use_code
    18  from (
    19        select customer_number
    20              ,customer_name
    21              ,customer_address1
    22              ,site_use_code
    23              ,row_number() over (partition by customer_number order by decode(Site_use_code,'BILL_TO',1,'SHIP_TO',2,3) desc) as rn
    24        FROM t
    25        WHERE Site_use_code IN ('BILL_TO','SHIP_TO','SOLD_TO')
    26       )
    27* where rn = 1
    SQL> /
    CUSTOMER_NUMBER CUSTOM CUSTOMER SITE_US
                  1 Fred   address1 SHIP_TO
                  2 Bob    address2 BILL_TO
                  3 George address3 SOLD_TO
                  4 Tim    address4 BILL_TO
                  5 Sam    address5 BILL_TO
    SQL>Or alternative...
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (
      2    select 1 as customer_number, 'Fred' as customer_name, 'address1' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      3    select 1 as customer_number, 'Fred' as customer_name, 'address1' as customer_address1, 'SHIP_TO' as site_use_code from dual union all
      4    select 2 as customer_number, 'Bob' as customer_name, 'address2' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      5    select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      6    select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'SHIP_TO' as site_use_code from dual union all
      7    select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'SOLD_TO' as site_use_code from dual union all
      8    select 4 as customer_number, 'Tim' as customer_name, 'address4' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      9    select 5 as customer_number, 'Sam' as customer_name, 'address5' as customer_address1, 'BILL_TO' as site_use_code from dual
    10  )
    11  --
    12  -- END OF TEST DATA
    13  --
    14  select distinct
    15         customer_number
    16        ,customer_name
    17        ,customer_address1
    18        ,first_value(site_use_code) over (partition by customer_number order by decode(Site_use_code,'BILL_TO',1,'SHIP_TO',2,3) desc) as site_use_code
    19  FROM t
    20  WHERE Site_use_code IN ('BILL_TO','SHIP_TO','SOLD_TO')
    21* order by customer_number
    SQL> /
    CUSTOMER_NUMBER CUSTOM CUSTOMER SITE_US
                  1 Fred   address1 SHIP_TO
                  2 Bob    address2 BILL_TO
                  3 George address3 SOLD_TO
                  4 Tim    address4 BILL_TO
                  5 Sam    address5 BILL_TO

  • Need help asap regarding the query windowing

    Dear friends,
    The database i'm using (mvdemo - mapviewer demo DB that comes along with Oracle 10g), has only data about united states. Say if i have my query window like this, -180, 0, 180 , 90 (which is the upper half of the earth), i am getting the desired result. ie., Here is that query,
    SELECT count(s.geom) FROM states s WHERE
    SDO_FILTER(s.geom,
    SDO_GEOMETRY(
         2003,
         8265,
         NULL,
         SDO_ELEM_INFO_ARRAY(1,1003,3),
         SDO_ORDINATE_ARRAY(-180, 0 , 180, 90))
    ) = 'TRUE';
    Returns 55
    I'm currently having a problem with my rectangular window query. When i query with a window value of -180, -90, 180, 90 (which covers the whole earth), the number of spatial objects (SDO_GEOM) that is returned is 0. It is not throwing any ORA error. But the expected result is as usual 55.
    SELECT count(s.geom) FROM states s WHERE
    SDO_FILTER(s.geom,
    SDO_GEOMETRY(
         2003,
         8265,
         NULL,
         SDO_ELEM_INFO_ARRAY(1,1003,3),
         SDO_ORDINATE_ARRAY(-180, 0 , 180, 90))
    ) = 'TRUE';
    Returns 0
    Could anyone please help me out with this issue??
    thank you in advance,
    Bragadeesh

    hi all,
    i found the solution to my problem.
    actually the query given here works just fine.
    wat i was doing was using the sdo_intersection(<some geom>, <query window>). The query window am using here is the whole earth. But according to spatial rule, there cant be any geometry greater than half the surface of the earth.
    Thank you all..

  • Need help in writing the code

    Hi,
    My table data is as below.
    Kunnr  BDAT EDAT  D1 D2 D3 D4 D5
    1111    2001  2002   1      2    1      2     1
    1111    2003  2008   1      0    4     1      2
    1111    2008  2011   1      1    1     1      1
    2222    2003  2008   1      2    2             2
    2222    2008  2011           1    2     1      1
    Here for kunnr  between begin and end date we have to caluclate the dependencies. (I.e if there is any value in D1or D2 or D3orD4 it shoulbe counted as 1. of it is zero or blank then it should not be coutned).
    for Ex: for 1111 between 2001 & 2002 tot dependencies are 5.            
               for 1111 between 2003 & 2008 tot dependencies are 4.
    in this scenario what will be the best way of writing the code. Do we need to check each and every field when counting the Sum value or and simple way to improve the performance.
    Regards,
    Vishnu
    Edited by: Vishnuvadhan K on May 13, 2011 10:30 AM
    Moderator Message: This is not a forum, where you can get your work done by others.
    Edited by: kishan P on May 13, 2011 3:52 PM

    Hi Vishnu,
    Here, you will have to check for the range of your begin date & end date. If it comes in the said range, then you simply have to add the values of your D1, D2,... DX values which you can store in some variables by setting out a flag as one counter for each.
    If your case is satisfied for any of the date range, then just check for the flag values which are satisfied & add them or display the sum of the number of counters.
    @Ambujavalli, At End Of won't work here because the date ranges given by Vishnu will differ.
    Those statements are useful in a case when suppose for the same field value, you need to calculate a particular sum.
    Here, that is not the case. Hence, AT First & At End Of won't work.
    Thanks,
    Gayathri.
    Edited by: gnikesh88 on May 13, 2011 11:52 AM

  • Need help in writing the script in windows to read the file at runtime

    Hi All,
    I need a help with a Script. I need to read a filename from a directorty based on a specific format. I will be looking for the file in a directory that will have a file added to it each month. The script will need to go out to that directory determine the current month and Year and then grab that file based on the MONTHYEAR contained itn the file name and
    in the same script pass the filename to SQL Loader.
    The filename will consist of three parts VARIABLECONSTANTDATE.ext (example: FUELFILE11262007.txt: FUEL is a VARIABLE, FILE is constant, 11 two digit month, 26 two digit day,2007 four digit year.)
    I am stuck as how to how to read that into a string, find the file name and then pass that to sql loader.
    Any help much appreciated..
    Looking forward for your response.

    Use of External Tables will ease your problem...
    Here's what you can do:
    1. Create External tables (fuelfile.asc)
    2. Use Java code to read all filenames in a particular directory and store them in table
    3. Use PL/SQL block to loop through each filename in the table and write the file to the file of external table (fuelfile.asc)
    3.1 Transfer data from External table to main table
    Aalap Sharma :)

  • Need help in writing a query

    Hi,
    I have a table with two columns as follows
    child parent
    mom grandma
    daughter mom
    How can I write a single query so that the result will be daughter,mom,grandma?
    Thanks
    Ravi.

    Responses to these postings will help:
    pivot the column names
    PIVOT in oracle.
    Re: Pivot Query

  • Need help in fixing the query for performance reasons

    hi
    I have a view in which I am calling a function to return some values I wand to get rid of the function and want to some how do all in the sql. Can I use case or something similar that will help me in avoiding the use of this function. Any help in this regard will highly be appreciated.
    Query
    SELECT SUBSTR(f_type_px17(ap.cidn,ap.currentstatusoid, 'DESC'), 1, 255)
                        from
                             px17_appointment ap,
                             /*context co_ap,*/
                             px17_payrollagreement pa,
                             px17_payrollgroup pg;
    FUNCTION
    CREATE OR REPLACE FUNCTION f_type_px17
    fv_cidn IN px17_type_client.cidn%TYPE,
    fv_oid IN px17_type_client.oid%TYPE,
    fv_col IN VARCHAR2 DEFAULT NULL
    ) RETURN VARCHAR2 AUTHID CURRENT_USER AS
    v_rtn_name px17_type_client.name%TYPE := NULL ;
    v_rtn_desc px17_type_client.description%TYPE := NULL ;
    BEGIN
    IF fv_oid IS NOT NULL THEN
    BEGIN
    IF UPPER(SUBSTR(fv_oid,1,3)) = 'SYS' THEN
    SELECT name, description
    INTO v_rtn_name, v_rtn_desc
    FROM px17_sys_type
    WHERE oid = fv_oid
    AND ROWNUM = 1 ;
    ELSE
    SELECT name, description
    INTO v_rtn_name, v_rtn_desc
    FROM px17_type_client
    WHERE oid = fv_oid
         AND cidn = fv_cidn
    AND ROWNUM = 1 ;
    END IF ;
    EXCEPTION
    WHEN no_data_found THEN
    v_rtn_name := NULL ;
    v_rtn_desc := NULL ;
    END ;
    END IF ;
    IF (UPPER(NVL(fv_col, '~')) = 'NAME') THEN
    RETURN v_rtn_name ;
    ELSIF ((UPPER(NVL(fv_col, '~')) = 'DESC')
    OR (UPPER(NVL(fv_col, '~')) = 'DESCRIPTION')) THEN
    RETURN v_rtn_desc ;
    ELSE
    RETURN (NVL(v_rtn_name, CHR(155)) || '~' || NVL(v_rtn_desc, CHR(155))) ;
    END IF ;
    END f_type_px17 ;
    /

    Well following is the whole code of a view and the functions releated, This will give you the whole picture and this query is running for long time.
    create or replace view PX17_CHK_VW_SUMMARY_VW
    (CIDN,BATCH_NUMBER, BUSINESS_UNIT, CHECK_VOUCHER_NUMBER, CHECK_VOUCHER_CODE, CHECKVIEW_CLOCK_NUMBER,
    COMPANY_CHECKVIEW_HOME_DEPT, PERIOD_END_DATE, FEDERAL_TAX, GROSS_PAY, HOME_DEPARTMENT,
    HOME_DEPARTMENT_DESC, LIVED_LOCAL_TAX_CODE, LIVED_STATE_TAX_CODE, LIVED_LOCAL_TAX, LIVED_STATE_TAX,
    MEDICARE_TAX, NET_PAY, DEPARTMENT_WORKED_IN, PAY_TO_COMPANY_INDICATOR, PAY_DATE,
    PAYROLL_NUMBER, SCHOOL_DISTRICT_TAX, CHECKVIEW_SCHOOL_DISTRICT, SOCIAL_SECURITY_TAX, SUI_SDI_TAX,
    SUI_SDI_TAX_CODE, VOID_CHECK_INDICATOR, WEEK_NUMBER, WORKED_LOCAL_TAX_CODE, WORKED_STATE_TAX_CODE,
    WORKED_LOCAL_TAX, WORKED_STATE_TAX, YEAR, COMPANY_CODE, FILE_NUMBER,
    FIRST_NAME, LAST_NAME, LAST_NAME_FIRST_NAME, SOCIAL_SECURITY_NUMBER, STATUS,
    OVERTIME_EARNINGS, OVERTIME_HOURS, REGULAR_EARNINGS, REGULAR_HOURS, VIEWPK,
    HOME_COST_NUMBER, TAX_FREQUENCY, COMPANY_COST_NUMBER, COST_NUMBER_WORKED_IN, DISTRIBUTION_NUMBER,
    TOTAL_DEDUCTIONS_AMOUNT, TOTAL_HOURS_AMOUNT, TOTAL_MEMO_AMOUNT, TOTAL_OTHER_EARNINGS, TOTAL_OTHER_HOURS,
    CHECK_SEQ_NO, JOINKEY, CHECK_REVERSAL_INDICATOR
    as
    with user_security_homedept
    AS
    (select /*+ INLINE */ distinct cg.cidn,co_code,asso.oid,asso.name,department_Access ,t2.column_value dep,cg.oid
    from px17_cocodegroup cg ,
    px17_associate asso ,
         px17_securityobject so,
         px17_cocodegrp_securitygrp cs,
         px17_security_group sg ,
         TABLE(f_str2tbl_px17(cg.department_access)) t2
    where cg.USEROID = asso.OID
    and cg.co_code = substr(so.name,8,3)
    and upper(asso.name) = upper(sys_context('payx_r17_app_context', 'app_userid'))
    and cg.oid = cs.cocodegroupoid
    and cs.securitygroupoid = sg.oid
         and cg.cidn = asso.cidn
         and cg.cidn = so.cidn
         and cg.cidn = cs.cidn
         and cs.cidn = sg.cidn
    order by 2,1
    user_security_cost_no
    AS
    (select /*+ INLINE */
    cg.cidn,co_code,count(distinct cd.code)cost_no,cd.code
    from px17_cocodegroup cg ,
    px17_associate asso ,
         px17_securityobject so,
         px17_cocodegrp_securitygrp cs,
         px17_security_group sg ,
         px17_customaccessdetail cd
    where cg.USEROID = asso.OID
    and cg.co_code = substr(so.name,8,3)
    and upper(asso.name) = upper(sys_context('payx_r17_app_context', 'app_userid'))
    and cg.oid = cs.cocodegroupoid
    and cs.securitygroupoid = sg.oid
    and cg.oid = cd.cocodegroupoid(+)
         and cg.cidn = cd.cidn(+)
         and cg.cidn = asso.cidn
         and cg.cidn = so.cidn
         and cg.cidn = cs.cidn
         and cs.cidn = sg.cidn
    group by cg.cidn,co_code,cd.code
    order by 2,1
    super_user_check as
    (SELECT
    distinct a.cidn su_cidn,1 as su_user
    FROM
    px17_LINK LN
    , px17_userprofile up
    , px17_ASSOCIATE a
    , px17_pcp_user pu
    WHERE
    a.oid = pu.oid
    AND pu.userprofileoid = up.oid
    AND LN.parentoid = up.oid
    AND a.active = 1
    AND a.cidn=pu.cidn
    AND pu.cidn=up.cidn
    AND ln.cidn=up.cidn
    AND upper(a.name) = upper(sys_context('payx_r17_app_context', 'app_userid'))
    AND upper(up.name) = upper('Super user')
    nonsuper_user_check as
    SELECT distinct a.cidn s_cidn , 1 as non_user
    FROM
    px17_LINK LN
    , px17_userprofile up
    , px17_ASSOCIATE a
    , px17_pcp_user pu
    WHERE
    a.oid = pu.oid
    AND pu.userprofileoid = up.oid
    AND LN.parentoid = up.oid
    AND a.active = 1
    AND a.cidn=pu.cidn
    AND pu.cidn=up.cidn
    AND ln.cidn=up.cidn
    AND upper(a.name) = upper(sys_context('payx_r17_app_context', 'app_userid'))
         AND LN.childoid IN ( 'SYS:5:7478')
    SELECT distinct
    cidn
         ,batch_number     Batch_Number
         , business_unit     Business_Unit
         , check_voucher_number     Check_Voucher_Number
         , check_voucher_code     Check_Voucher_Code
         , clock_number     CheckView_Clock_Number
         , company_code_or_dept     Company_CheckView_Home_Dept
         , period_end_date     Period_End_Date
         , TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, 1, (INSTR(taxes_and_rates, '~', 1, 1) -1)), CHR(155), NULL))     Federal_Tax
         , gross_pay     Gross_Pay
         , home_department     Home_Department
         , home_department_descr     Home_Department_Desc
         , SUBSTR(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 1) +1)
                   , INSTR(taxes_and_rates, '~', 1, 2) - (INSTR(taxes_and_rates, '~', 1, 1) +1)), CHR(155), NULL), 1, 32)     Lived_Local_Tax_Code
         , SUBSTR(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 2) +1)
                   , INSTR(taxes_and_rates, '~', 1, 3) - (INSTR(taxes_and_rates, '~', 1, 2) +1)), CHR(155), NULL), 1, 32)     Lived_State_Tax_Code
         , TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 3) +1)
                   , INSTR(taxes_and_rates, '~', 1, 4) - (INSTR(taxes_and_rates, '~', 1, 3) +1)), CHR(155), NULL))     Lived_Local_Tax
         , TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 4) +1)
                   , INSTR(taxes_and_rates, '~', 1, 5) - (INSTR(taxes_and_rates, '~', 1, 4) +1)), CHR(155), NULL))     Lived_State_Tax
         , TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 5) +1)
                   , INSTR(taxes_and_rates, '~', 1, 6) - (INSTR(taxes_and_rates, '~', 1, 5) +1)), CHR(155), NULL))     Medicare_Tax
         , net_pay     Net_Pay
         , department_paid     Department_Worked_In
         , to_pay_indicator     Pay_to_Company_Indicator
         , pay_date     Pay_Date
         , payroll_number     Payroll_Number
         , TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 6) +1)
                   , INSTR(taxes_and_rates, '~', 1, 7) - (INSTR(taxes_and_rates, '~', 1, 6) +1)), CHR(155), NULL))     School_District_Tax
         , SUBSTR(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 7) +1)
                   , INSTR(taxes_and_rates, '~', 1, 8) - (INSTR(taxes_and_rates, '~', 1, 7) +1)), CHR(155), NULL), 1, 32) CheckView_School_District
         , TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 8) +1)
                   , INSTR(taxes_and_rates, '~', 1, 9) - (INSTR(taxes_and_rates, '~', 1, 8) +1)), CHR(155), NULL))     Social_Security_Tax
         , TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 9) +1)
                   , INSTR(taxes_and_rates, '~', 1, 10) - (INSTR(taxes_and_rates, '~', 1, 9) +1)), CHR(155), NULL))     SUI_SDI_Tax
         , SUBSTR(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 10) +1)
                   , INSTR(taxes_and_rates, '~', 1, 11) - (INSTR(taxes_and_rates, '~', 1, 10) +1)), CHR(155), NULL), 1, 32) SUI_SDI_Tax_Code
         , void_check_indicator     Void_Check_Indicator
         , week_number     Week_Number
         , SUBSTR(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 11) +1)
                   , INSTR(taxes_and_rates, '~', 1, 12) - (INSTR(taxes_and_rates, '~', 1, 11) +1)), CHR(155), NULL), 1, 32) Worked_Local_Tax_Code
         , SUBSTR(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 12) +1)
                   , INSTR(taxes_and_rates, '~', 1, 13) - (INSTR(taxes_and_rates, '~', 1, 12) +1)), CHR(155), NULL), 1, 32) Worked_State_Tax_Code
         , TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 13) +1)
                   , INSTR(taxes_and_rates, '~', 1, 14) - (INSTR(taxes_and_rates, '~', 1, 13) +1)), CHR(155), NULL))     Worked_Local_Tax
         , TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 14) +1)
                   , INSTR(taxes_and_rates, '~', 1, 15) - (INSTR(taxes_and_rates, '~', 1, 14) +1)), CHR(155), NULL))     Worked_State_Tax
         , year          Year
         , company_code     Company_Code
         , file_number     File_Number
         , first_name     First_Name
         , last_name          Last_Name
         , name          Last_Name_First_Name
         , ssn               Social_Security_Number
         , status          Status
         , TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 15) +1)
                   , INSTR(taxes_and_rates, '~', 1, 16) - (INSTR(taxes_and_rates, '~', 1, 15) +1)), CHR(155), NULL))     Overtime_Earnings
         , TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 16) +1)
                   , INSTR(taxes_and_rates, '~', 1, 17) - (INSTR(taxes_and_rates, '~', 1, 16) +1)), CHR(155), NULL))     Overtime_Hours
         , TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 17) +1)
                   , INSTR(taxes_and_rates, '~', 1, 18) - (INSTR(taxes_and_rates, '~', 1, 17) +1)), CHR(155), NULL))     Regular_Earnings
         , TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 18) +1)
                   , INSTR(taxes_and_rates, '~', 1, 19) - (INSTR(taxes_and_rates, '~', 1, 18) +1)), CHR(155), NULL))     Regular_Hours
         , (company_code || '/' || ssn || '/' || file_number || '/' || TO_CHAR(payroll_number) || '/'
              || TO_CHAR(year) || '/' || TO_CHAR(week_number) || '/' || check_voucher_number
              || '/' || to_char(check_seq_num) || '/' || TO_CHAR(distribution_number)) Viewpk
         , home_costnumber_desc Home_Cost_Number
         , tax_frequency Tax_Frequency
         , co_chkv_home_cost_no Company_Cost_Number
         , cost_no_worked_in Cost_Number_Worked_In
         , distribution_number Distribution_Number
         , TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 19) +1)
                   , INSTR(taxes_and_rates, '~', 1, 20) - (INSTR(taxes_and_rates, '~', 1, 19) +1)), CHR(155), NULL))     Total_Deductions_Amount
         , NVL(TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 20) +1)
                   , INSTR(taxes_and_rates, '~', 1, 21) - (INSTR(taxes_and_rates, '~', 1, 20) +1)), CHR(155), NULL)), 0) Total_Hours_Amount
         , NVL(TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 21) +1)
                   , INSTR(taxes_and_rates, '~', 1, 22) - (INSTR(taxes_and_rates, '~', 1, 21) +1)), CHR(155), NULL)), 0) Total_Memo_Amount
         , NVL(TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 22) +1)
                   , INSTR(taxes_and_rates, '~', 1, 23) - (INSTR(taxes_and_rates, '~', 1, 22) +1)), CHR(155), NULL)), 0) Total_Other_Earnings
         , TO_NUMBER(REPLACE(SUBSTR(taxes_and_rates, (INSTR(taxes_and_rates, '~', 1, 23) +1)), CHR(155), NULL))     Total_Other_Hours
         , check_seq_num Check_Seq_No
         , (NVL(company_code, '~') || CHR(155) || NVL(file_number, '~') || CHR(155) || NVL(ssn, '~') || CHR(155) || NVL(TO_CHAR(year), '~') || CHR(155)
              || NVL(TO_CHAR(week_number), '~') || CHR(155) || NVL(TO_CHAR(payroll_number), '~')
              || CHR(155) || NVL(check_voucher_number, '~') || CHR(155) || NVL(TO_CHAR(check_seq_num), '~') || CHR(155) || NVL(TO_CHAR(distribution_number), '~')) Joinkey
         , chk_reverse Check_Reversal_Indicator
    FROM
              SELECT
              ch.cidn cidn
                   /*BATCH NUMBER*/
                   , ch.batch_nb     batch_number
                   , /*BUSINESS UNIT*/
                   (select asso.description
                        from     px17_appointment ap,
                             px17_jobposition jp,
                             px17_corporation co,
                             px17_associate asso,
                             px17_payrollagreement pa,
                             px17_payrollgroup pg
                        where
                        ch.cidn = pg.cidn
                                       AND ch.cidn = pa.cidn
                                       AND pa.cidn = pg.cidn
                                       AND pa.cidn = ap.cidn
                                       AND em.cidn = ap.cidn
                                       AND ap.cidn = jp.cidn
                                       and jp.cidn = co.cidn
                                       and co.cidn = asso.cidn
                        and ch.co_code=pg.co_code
                        and     ch.file_nb=pa.file_number
                        and     pa.cocodeoid=pg.oid
                        and     pa.appointmentoid=ap.oid
                        and     em.oid=ap.employmentoid
                        and     ap.jobpositionoid=jp.oid
                             and     jp.corporationoid=co.oid
                        and     co.oid=asso.oid)     business_unit
                   , /*CHECK/VOUCHER NUMBER*/
                   ch.check_nb     check_voucher_number
                   , /*CHECK/VOUCHER CODE*/
                   SUBSTR(f_type_px17(ch.cidn,ch.chkvchcodeoid, 'NAME'), 1, 255) check_voucher_code
                   , /*CLOCK NUMBER*/
                   ch.clock_id     clock_number
                   , /* COMPANY CODE OR DEPT*/
                   ch.co_code||'/'||substr(ch.home_dept_code,1,20)     company_code_or_dept
                   , /*PERIOD END DATE*/
                   ch.payroll_ending_date     period_end_date
                   , /*GROSS PAY*/
                   NVL(chd.gross_pay_amt,0)     gross_pay
                   , /*HOME DEPARTMENT*/
                   ch.home_dept_code     home_department
                   , /*HOME DEPARTMENT DESCRIPTION*/
                   (select     ty.description
                        from     px17_type ty,px17_securityobject sc
                        where
                        ch.cidn = sc.cidn     
                        and ty.cidn in ('COMMON', ch.cidn)
                        and ch.home_dept_code=ty.name
                        and     ty.category='T_CO_DEPT'
                        and      ty.securityoid=sc.oid
                        and     ch.co_code=substr(sc.name,8))     home_department_descr
                   , /*NET PAY*/
                   nvl(chd.net_pay_amt,0)     net_pay
                   , /*DEPARTMENT PAID*/
                   -- ch.paid_in_debt_code     department_paid
                   chd.worked_in_dept     department_paid
                   , /*TO PAY INDICATOR*/
                   decode(to_char(ch.pay_to_co),'1','Y','0','N',to_char(ch.pay_to_co))     to_pay_indicator
                   , /*PAY DATE*/
                   ch.pay_date     pay_date
                   , /*PAYROLL NUMBER*/
                   to_char(ch.payroll_nb)     payroll_number
                   , /*VOID CHECK INDICATOR*/
                   decode(to_char(ch.void_flag),'1','Y','0','N',to_char(ch.void_flag))     void_check_indicator
                   , /*WEEK NUMBER*/
                   TO_number(ch.week)     week_number
                   , /*YEAR*/
                        to_char(ch.year)     year
                   , /*COMPANY CODE*/
                   ch.co_code     company_code
                   , /*FILE NUMBER*/
                   LPAD(ch.file_nb,6,0)     file_number
                   , /*FIRST NAME*/
                   pe.first_name     first_name
                   , /*LAST NAME*/
                   pe.last_name     last_name
                   , /*NAME*/
                   pe.last_name||', '||pe.first_name     name
                   , /*SSN*/
                   payx_r17_principal_info.masked_ssn(pe.unique_id, ch.cidn)      ssn
                   , /*STATUS*/
                   (SELECT SUBSTR(f_type_px17(ap.cidn,ap.currentstatusoid, 'DESC'), 1, 255)
                        from
                             px17_appointment ap,
                             /*context co_ap,*/
                             px17_payrollagreement pa,
                             px17_payrollgroup pg
                        where     
                        pg.cidn=ch.cidn
                             and pa.cidn=ch.cidn
                                  and ap.cidn=pa.cidn
                                  and ap.cidn = em.cidn
                        and ap.employmentoid=em.oid
                        and     ap.active=1
                        and     ap.oid=pa.appointmentoid
                        and     lpad(to_char(pa.file_number),6,0)=ch.file_nb
                        and     pa.cocodeoid=pg.oid
                        and     pg.co_code=ch.co_code)     status
                   --          FED TAX
                   --          , LIVED LOCAL TAX CODE, LIVED STATE TAX CODE, LIVED LOCAL TAX, LIVED STATE TAX, MEDICARE TAX
                   --          , SCHOOL DISTRICT TAX, SCHOOL DISTRICT, SOCIAL SECURITY TAX, SUI SDI TAX, SUI SDI TAX CODE
                   --          , WORKED LOCAL TAX CODE, WORKED STATE TAX CODE, WORKED LOCAL TAX, WORKED STATE TAX
                   --          , OVERTIME EARNINGS, OVERTIME HOURS, REGULAR EARNINGS, REGULAR HOURS
                   NVL (
                        (SELECT
                             TO_CHAR(NVL(SUM(DECODE(ci.histtypeoid, 'SYS:4:3709', DECODE(ci.taxcodeoid, 'SYS:4:2982', NVL(ci.amount,0), 0), 0) ), 0)) || '~'
                             || NVL(MAX(DECODE(ci.histtypeoid, 'SYS:4:3709', DECODE(ci.taxcodeoid, 'SYS:4:2984', ci.code, NULL), NULL)), CHR(155)) || '~'
                             || NVL(MAX(DECODE(ci.histtypeoid, 'SYS:4:3709', DECODE(ci.taxcodeoid, 'SYS:4:2987', DECODE(ci.code,'XX',null,ci.code), NULL), NULL)), CHR(155)) || '~'
                             || TO_CHAR(NVL(SUM(DECODE(ci.histtypeoid, 'SYS:4:3709', DECODE(ci.taxcodeoid, 'SYS:4:2984', NVL(ci.amount,0), 0), 0) ), 0)) || '~'
                             || TO_CHAR(NVL(SUM(DECODE(ci.histtypeoid, 'SYS:4:3709', DECODE(ci.taxcodeoid, 'SYS:4:2987', NVL(ci.amount,0), 0), 0) ), 0)) || '~'
                             || TO_CHAR(NVL(SUM(DECODE(ci.histtypeoid, 'SYS:4:3709', DECODE(ci.taxcodeoid, 'SYS:4:4118', NVL(ci.amount,0), 0), 0) ), 0)) || '~'
                             || TO_CHAR(NVL(SUM(DECODE(ci.histtypeoid, 'SYS:4:3709', DECODE(ci.taxcodeoid, 'SYS:4:4114', NVL(ci.amount,0), 0), 0) ), 0)) || '~'
                             || NVL(MAX(DECODE(ci.histtypeoid, 'SYS:4:3709', DECODE(ci.taxcodeoid, 'SYS:4:4114', ci.code, NULL), NULL)), CHR(155)) || '~'
                             || TO_CHAR(NVL(SUM(DECODE(ci.histtypeoid, 'SYS:4:3709', DECODE(ci.taxcodeoid, 'SYS:4:2988', NVL(ci.amount,0), 0), 0) ), 0)) || '~'
                             || TO_CHAR(NVL(SUM(DECODE(ci.histtypeoid, 'SYS:4:3709', DECODE(ci.taxcodeoid, 'SYS:4:2989', NVL(ci.amount,0), 0), 0) ), 0)) || '~'
                             || NVL(MAX(DECODE(ci.histtypeoid, 'SYS:4:3709', DECODE(ci.taxcodeoid, 'SYS:4:2989', ci.code, NULL), NULL)), CHR(155)) || '~'
                             || NVL(MAX(DECODE(ci.histtypeoid, 'SYS:4:3709', DECODE(ci.taxcodeoid, 'SYS:4:2983', ci.code, NULL), NULL)), CHR(155)) || '~'
                             || NVL(MAX(DECODE(ci.histtypeoid, 'SYS:4:3709', DECODE(ci.taxcodeoid, 'SYS:4:2986', ci.code, NULL), NULL)), CHR(155)) || '~'
                             || TO_CHAR(NVL(SUM(DECODE(ci.histtypeoid, 'SYS:4:3709', DECODE(ci.taxcodeoid, 'SYS:4:2983', NVL(ci.amount,0), 0), 0) ), 0)) || '~'
                             || TO_CHAR(NVL(SUM(DECODE(ci.histtypeoid, 'SYS:4:3709', DECODE(ci.taxcodeoid, 'SYS:4:2986', NVL(ci.amount,0), 0), 0) ), 0)) || '~'
                             || TO_CHAR(NVL(SUM(DECODE(ci.histtypeoid, 'SYS:4:3713', NVL(ci.amount,0), 0)), 0)) || '~'
                             || TO_CHAR(NVL(SUM(DECODE(ci.histtypeoid, 'SYS:4:3727', NVL(ci.amount,0), 0)), 0)) || '~'
                             || TO_CHAR(NVL(SUM(DECODE(ci.histtypeoid, 'SYS:4:3715', NVL(ci.amount,0), 0)), 0)) || '~'
                             || TO_CHAR(NVL(SUM(DECODE(ci.histtypeoid, 'SYS:4:3716', NVL(ci.amount,0), 0)), 0)) || '~'
                             -- R16 columns.
                             -- Total Deductions Amount === Other Deductions on UI screen.
                             || TO_CHAR(NVL(SUM(DECODE(chcat.name, 'DeductionHistory'
                                  , DECODE(NVL(ci.statutory_ded, 0), 0
                                       , DECODE(ci.taxcodeoid, NULL
                                            , DECODE(ci.code, NULL, 0, NVL(ci.amount,0)), 0), 0), 0)), 0)) || '~' -- "Total Deductions Amount"
                             || TO_CHAR(NVL(SUM(DECODE(chcat.name, 'OvertimeHours', NVL(ci.amount,0)
                                  , 'AdditionalHours', NVL(ci.amount,0), 'RegularHours', NVL(ci.amount,0), 0)), 0)) || '~' -- "Total Hours Amount"
                             || TO_CHAR(NVL(SUM(DECODE(chcat.name, 'Memo', NVL(ci.amount,0), 0)), 0)) || '~' -- "Total Memo Amount"
                             || TO_CHAR(NVL(SUM(DECODE(chcat.name, 'AdditionalEarnings', NVL(ci.amount,0), 0)), 0)) || '~' -- "Total Other Earnings"
                             || TO_CHAR(NVL(SUM(DECODE(chcat.name, 'AdditionalHours', NVL(ci.amount,0), 0)), 0)) -- "Total Other Hours"
                        FROM
                             px17_checkhistoryitem ci
                             , px17_sys_type chcat
                        WHERE
                        ci.cidn=chd.cidn
                             AND ci.checkhistorydistributionoid = chd.oid
                             AND chcat.category = 'CheckHistoryItem'
                             AND chcat.oid = ci.histtypeoid
                             -- AND ci.histtypeoid IN ('SYS:4:3713', 'SYS:4:3727', 'SYS:4:3715', 'SYS:4:3716', 'SYS:4:3709')
                        ), '0' || '~' || CHR(155) || '~' || CHR(155)
                             || '0~0~0~0~' || CHR(155) || '~0~0~' || CHR(155) || '~' || CHR(155) || '~'
                             || CHR(155) || '~0~0~0~0~0~0'
                        )     taxes_and_rates
                   , ch.check_seq_num check_seq_num
                   , ch.home_cost_number home_costnumber_desc
                   , SUBSTR(f_type_px17(ch.cidn,ch.payfrequencyoid, 'NAME'), 1, 255) tax_frequency
                   , (ch.co_code || '/' || ch.home_cost_number) co_chkv_home_cost_no
                   , chd.worked_in_cost_number cost_no_worked_in
                   , chd.distribution_number distribution_number
                   , DECODE(NVL(ch.reversed_flag, 0), 1, 'Y', 'N') chk_reverse
              FROM
                   px17_checkhistory ch
                   , px17_employment em
                   , px17_person pe
                   , px17_payrollgroup pg
                   , px17_checkhistory_dist chd
                   , px17_appointment ap
              WHERE
              ch.cidn = em.cidn
              and em.cidn = pe.cidn
              and pg.cidn = ch.cidn
              and ch.cidn = chd.cidn
              and ap.cidn(+) = em.cidn
                   and ch.employmentoid=em.oid
                   AND     em.personoid=pe.oid
                   AND     pg.co_code = ch.co_code
                   AND ch.active = 1
                   AND ch.oid = chd.checkhistoryoid
                   AND ap.employmentoid(+) = em.oid
                   AND DECODE(ap.oid, NULL, 1, NVL((SELECT 1 FROM px17_payrollagreement pa
                                  WHERE
                                  pa.cidn = ap.cidn
                                  and pa.cidn = ch.cidn
                                  and pa.cidn = pg.cidn
                                  and pa.appointmentoid = ap.oid
                                       AND pa.file_number = ch.file_nb
                                       AND pa.cocodeoid = pg.oid), 0)) = 1
                                       and ch.cidn in (select distinct s_cidn from nonsuper_user_check)
                                       AND ( sys_context('payx_r17_app_context', 'app_role') in ('ADP', 'CSR')
              or (exists (select 1 from super_user_check where su_cidn =ch.cidn))
              or (ch.cidn,ch.co_code,ch.home_dept_code) in (select cidn,co_code,dep from user_security_homedept)          
              or (ch.cidn,ch.co_code,ch.home_cost_number) in (select cidn,co_code,code from user_security_cost_no)
                   or ( exists(select co_code from user_security_homedept where cidn=ch.cidn and co_code = ch.co_code and dep is null)
                   and exists(select co_code from user_security_cost_no where cidn=ch.cidn and co_code = ch.co_code and cost_no = 0) )
    Following are the functions being called:
    create or replace type myTable_17 as table of varchar2(4000);
    CREATE OR REPLACE function f_str2tbl_px17( p_str in varchar2 ) return myTable_17 pipelined
    as
    l_str long default p_str || ',';
    l_n number;
    begin
    loop
    l_n := instr( l_str, ',' );
    exit when (nvl(l_n,0) = 0);
    pipe row( ltrim(rtrim(substr(l_str,1,l_n-1))) );
    l_str := ltrim( substr( l_str, l_n+1 ) );
    end loop;
    return;
    end f_str2tbl_px17;
    CREATE OR REPLACE FUNCTION f_check_purge_px17
              fv_cidn px17_reservedfilenumber.cidn%TYPE,     
              fv_file_number     px17_reservedfilenumber.file_number%TYPE,
              fv_cocode     px17_payrollgroup.co_code%TYPE DEFAULT NULL
         ) RETURN NUMBER AUTHID CURRENT_USER
    AS
         v_val     NUMBER(5) ;
    BEGIN
         IF fv_cocode IS NULL THEN
              RETURN 0 ;
         END IF ;
         SELECT 1 INTO v_val
         FROM px17_reservedfilenumber a,
    px17_payrollgroup b
         WHERE
         a.cidn = fv_cidn
         AND b.cidn = fv_cidn
         AND a.file_number = fv_file_number
         AND a.parentoid = b.oid
    AND b.co_code = fv_cocode
         AND ROWNUM = 1 ;
         RETURN 1 ;
    EXCEPTION
         WHEN no_data_found THEN
              RETURN 0 ;
    END f_check_purge_px17 ;
    CREATE OR REPLACE FUNCTION f_check_security_px17
         fv_cidn IN px17_type_client.cidn%TYPE,
              fv_cocode          px17_cocodegroup.co_code%TYPE -- pg.cocode
              , fv_home_dept     px17_type_client.name%TYPE -- ap.home_dept
         ) RETURN NUMBER AUTHID CURRENT_USER
    AS
         v_val     NUMBER(5) ;
         v_key          VARCHAR2(20) ;
         v_users     VARCHAR2(4000) ;
         v_cocodes     VARCHAR2(4000) ;
    BEGIN
         BEGIN
              SELECT c_user_list, c_cocodes, c_run_key
              INTO v_users, v_cocodes, v_key
              FROM px17_t_context_vals
              WHERE c_run_key = (SELECT MAX(c_run_key) FROM px17_t_context_vals)
                   AND ROWNUM = 1 ;
         EXCEPTION
              WHEN no_data_found THEN
                   v_cocodes := NULL ;
         END ;
         IF v_cocodes IS NULL THEN
              RETURN 1 ;
         ELSIF INSTR( (v_cocodes || ','), (fv_cocode || ',') ) = 0 THEN
              RETURN 0 ;
         ELSE
              BEGIN
                   SELECT
                        1
                   INTO
                        v_val
                   FROM
                        px17_type_client t
                        , px17_securityobject s
                        , px17_cocodegroup c
                   WHERE
                        t.securityoid=s.oid
                        AND t.category='T_CO_DEPT'
                        AND c.useroid=(SELECT a.oid
                                       FROM px17_associate a
                                       WHERE a.oid = c.useroid
                                            AND a.active=1
                                            AND UPPER(a.name) = UPPER(v_users)
                                            AND ROWNUM = 1)
                        AND SUBSTR(s.name,8,3)=c.co_code
                        AND (c.department_access IS NULL
                             OR c.department_access LIKE '%' || t.name || '%')
                        AND SUBSTR(s.name,8,3) = fv_cocode
                        AND t.name = fv_home_dept
                        AND ROWNUM = 1 ;
                        RETURN 1 ;
              EXCEPTION
                   WHEN no_data_found THEN
                        BEGIN
                             SELECT
                                  1
                             INTO
                                  v_val
                             FROM
                                  px17_cocodegroup c
                                  , px17_associate a
                             WHERE
                                  c.useroid=a.oid
                                  AND c.department_access IS NULL
                                  AND c.co_code = fv_cocode
                                  AND UPPER(a.name)= UPPER(v_users)
                                  AND a.active=1
                                  AND ROWNUM = 1 ;
                             RETURN 1 ;
                        EXCEPTION
                             WHEN no_data_found THEN
                                  RETURN 0 ;
                        END ;
              END ;
         END IF ;
         RETURN 0 ;
    END f_check_security_px17 ;
    SELECT SUBSTR(f_type_px17(ap.cidn,ap.currentstatusoid, 'DESC'), 1, 255)
                        from
                             px17_appointment ap,
                             /*context co_ap,*/
                             px17_payrollagreement pa,
                             px17_payrollgroup pg
    CREATE OR REPLACE FUNCTION f_type_px17
    fv_cidn IN px17_type_client.cidn%TYPE,
    fv_oid IN px17_type_client.oid%TYPE,
    fv_col IN VARCHAR2 DEFAULT NULL
    ) RETURN VARCHAR2 AUTHID CURRENT_USER AS
    v_rtn_name px17_type_client.name%TYPE := NULL ;
    v_rtn_desc px17_type_client.description%TYPE := NULL ;
    BEGIN
    IF fv_oid IS NOT NULL THEN
    BEGIN
    IF UPPER(SUBSTR(fv_oid,1,3)) = 'SYS' THEN
    SELECT name, description
    INTO v_rtn_name, v_rtn_desc
    FROM px17_sys_type
    WHERE oid = fv_oid
    AND ROWNUM = 1 ;
    ELSE
    SELECT name, description
    INTO v_rtn_name, v_rtn_desc
    FROM px17_type_client
    WHERE oid = fv_oid
         AND cidn = fv_cidn
    AND ROWNUM = 1 ;
    END IF ;
    EXCEPTION
    WHEN no_data_found THEN
    v_rtn_name := NULL ;
    v_rtn_desc := NULL ;
    END ;
    END IF ;
    IF (UPPER(NVL(fv_col, '~')) = 'NAME') THEN
    RETURN v_rtn_name ;
    ELSIF ((UPPER(NVL(fv_col, '~')) = 'DESC')
    OR (UPPER(NVL(fv_col, '~')) = 'DESCRIPTION')) THEN
    RETURN v_rtn_desc ;
    ELSE
    RETURN (NVL(v_rtn_name, CHR(155)) || '~' || NVL(v_rtn_desc, CHR(155))) ;
    END IF ;
    END f_type_px17 ;
    *************************

  • Need help in writing SQL query

    Hi,
    I have a rquirement where I need to convert columns into records.
    Table1:
    (note: I have around 150 columns i.e period1 to period150)
    unit startdate Period1 period2 period3 period4 period5
    A 1/1/2011 100 200 300 400 500
    Now I need data in below format
    UNIT startdate period
    A 1/1/2011 100
    A 1/1/2011 200
    A 1/1/2011 300
    A 1/1/2011 500
    Please suggest me...
    Thanks.

    With UNPIVOT you can "name" each of your rows using as.
    One trick is to "name" the rows to a number - then you can add that number of weeks to your start date - like this:
    with t as (
       select 'A' unit, DATE '2011-01-01' startdate, 100 period1, 200 period2 ,300 period3, 400 period4 ,500 period5 from dual
    select
    unit,
    startdate + numtodsinterval(week*7,'day') startdate,
    qty
    from t
    unpivot(
       qty for week in (
          period1 as 0,
          period2 as 1,
          period3 as 2,
          period4 as 3,
          period5 as 4
    ;Theoretically the above should work - but somehow I get a NULL date on my 11.1 instance? And I don't have another version to test with, alas.
    But try it and see if it works on your instance ;-)

  • Hi need help in writing this query

    A table has three columns - id (primary key), categoryID (numeric) and
    amount (numeric). The goal is to provide a query that would show categoryID
    and sum of amounts within that category where categoryID is an odd number
    and sum of amounts is greater than 100,000

    select categoryid,sum(amount) from table where mod(categoryid,2)=1
    group by categoryid
    having sum(amount) > 100000
    Regards,
    Yuri
    [http://it.toolbox.com/blogs/living-happy-oracle]

  • Help with writing the query

    Hi,
    I have a table which has columns that records the logon and logoff times of the users every thime they do so. here are the column descriptions
    MSG DATE - DATE of the Logon\logoff
    MSG TIME - TIME of the logon\log off
    Log Type -  Type of activity ( Logon or Logoff.)
    User NAme
    Q)My requirement is to find the users that haven't logged on in the past 6 months. Can you please help?
    Many Thanks,
    Bhanu

    select [User Name] from LogActivity
    GROUP BY [User Name]
    HAVING MAX([MSG Date]) < DATEADD(month, -6, CURRENT_TIMESTAMP) 
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles

Maybe you are looking for