Help to write the query

Hello
I have created 2 user defined fields
Field 1 must be changed by the user before they save the sales order, it's a numeric field, value is 0 or 1. Field is called 'SPRECPTE'
The second field is a field to block the user as long as the above field is not changed. This field is alphanumeric and mandatory but the problem is when you create the UDF you must define a default value.
Here comes the query to remove the default value and make the control possible when you select the cardcode from OCRD.
Then the field 2 is blank and it must be field up to allow booking when you change UDF1 from 0 to 1. But the query work to empty the field 2 but it does not when SPRECPTE is equal to 1
Declare @U_SPRECPTE as numeric (1,0)
Set @U_SPRECPTE = $[$U_SPRECPTE.1.0]
IF U_SPRECPTE=0
Select '' ELSE 'A'

During order entry, the system checks freight cost based on formated search to add sales tax on freight fields.It was calculated for every line.
Now we are in live condition the formated search slows down order entry as every time an item is keyed in it recalculates every item again....
Then I decide to stop the fms based on item feild is modified and to valide the calculation before saving the order
To secure a sales order is saved without calculating the whole order I created 2 UDF to block users....
So one UDF must be mandatory and without default value, the first field launch the calculation through FMS of the whole order
I hope I am clear I am not familair with SBO store procedure but if you think it may help please explain
B Regards

Similar Messages

  • Need a help to write the query

    Hi,
    I need a small help to write the query.
    I have a table contains couponid,coupon,createdate,expirationdate,assigndate from couponday table
    i want to write the query to select the coupon whose Expiry date in the table is NOT within 30 days.
    Thanks in advance

    Hi,
    user586 wrote:
    i want to write the query to select the coupon whose Expiry date in the table is NOT within 30 days.If you mean expirationdate (datatype: DATE) is not within 30 days (past or future) of run time, then:
    SELECT  coupon          -- or whatever columns you want
    FROM    table_x
    WHERE   expirationdate  NOT BETWEEN  SYSDATE - 30
                                AND      SYSDATE + 30
    ;

  • I need a help to write the query

    Id
    Child
    Parent
    Is Sub Parent
    Is Parent
    1
    chld1
    y
    2
    chld2
    chld1
    y
    3
    chld3
    chld1
    y
    4
    chld4
    chld1
    5
    chld5
    chld2
    y
    6
    chld6
    chld2
    7
    chld7
    chld5
    8
    chld8
    Q: please check the above table, id no 1-7 are linked with parent and id no 8 is independent child, i need to write a query to display the below result based on the below scenarios
    Scenario 1: if user passes the child 1 the result should be as showed below table all 7 rows need to show.
    Scenario 2: if user passes the child in between child 1 - child 7 then also the result should be as showed below table all 7 rows.
    RESULT
    Id
    Child
    Parent
    1
    chld1
    2
    chld2
    chld1
    3
    chld3
    chld1
    4
    chld4
    chld1
    5
    chld5
    chld2
    6
    chld6
    chld2
    7
    chld7
    chld5
    Thanks in advance

    Hi,
    next time please post sample data as CREATE TABLE and INSERT statements or with clause like this:
    with mychildren as
       select 1 id, 'chld1' chld, null    prnt, null is_sub_parent, 'Y'  is_parent from dual union all 
       select 2 id, 'chld2' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 3 id, 'chld3' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 4 id, 'chld4' chld, 'chld1' prnt, null is_sub_parent, null is_parent from dual union all 
       select 5 id, 'chld5' chld, 'chld2' prnt,'Y'   is_sub_parent, null is_parent from dual union all 
       select 6 id, 'chld6' chld, 'chld2' prnt, null is_sub_parent, null is_parent from dual union all 
       select 7 id, 'chld7' chld, 'chld5' prnt, null is_sub_parent, null is_parent from dual union all 
       select 8 id, 'chld8' chld, null    prnt, null is_sub_parent, null is_parent from dual
    Here a couple of test with chld7 and chld1
    with mychildren as
       select 1 id, 'chld1' chld, null    prnt, null is_sub_parent, 'Y'  is_parent from dual union all 
       select 2 id, 'chld2' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 3 id, 'chld3' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 4 id, 'chld4' chld, 'chld1' prnt, null is_sub_parent, null is_parent from dual union all 
       select 5 id, 'chld5' chld, 'chld2' prnt,'Y'   is_sub_parent, null is_parent from dual union all 
       select 6 id, 'chld6' chld, 'chld2' prnt, null is_sub_parent, null is_parent from dual union all 
       select 7 id, 'chld7' chld, 'chld5' prnt, null is_sub_parent, null is_parent from dual union all 
       select 8 id, 'chld8' chld, null    prnt, null is_sub_parent, null is_parent from dual
    , find_root as
        select chld
          from mychildren
         where prnt is null
         start with chld='chld1'  --- this is your input
       connect by prior prnt=chld
    select id, chld, prnt
       from mychildren     
      start with chld=(select chld from find_root)
    connect by prior chld=prnt
            ID CHLD  PRNT
             1 chld1     
             2 chld2 chld1
             5 chld5 chld2
             7 chld7 chld5
             6 chld6 chld2
             3 chld3 chld1
             4 chld4 chld1
    Changing it to chld7 does not change the output.
    Output is now sorted according hierarchical query. If you need a different sort order you can specify what you want.
    Regards.
    Al

  • 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 )

  • Help Required(Rewriting the query to reduce steps)

    With and Without Group ID,I am populating a value into a column,Can anyone please help to write the same query in an other way.This is a sample column.Likewise,there are 5 more columns to be populated.So that I have to write 2 left outer joins for a single column with a little difference.
    SELECT
    CASE
         WHEN T6.ACTUAL_FUNCTION IS NOT NULL
         THEN T6.ACTUAL_FUNCTION
         WHEN T10.ACTUAL_FUNCTION IS NOT NULL
         THEN T10.ACTUAL_FUCNTION
              ELSE T5.ACTUAL_FUCNTION
    END AF_NOM_PRE_GTEES
    FROM
    ABC_WT17 T1
         INNER JOIN
         ABC_CUSTOMER_WT01 T3
         ON T1.ORIGINAL_SDS_PARTY_IDR=T3.ORIGINAL_SDS_PARTY_IDR
         AND T1.IDR_TYPE_CODE=T3.IDR_TYPE_CODE
         LEFT OUTER JOIN /* This is the condition with group ID*/
         RESIDUAL_CONTROL T6
         ON
         T1.NOM_PRE_GTEES = T6.VALUE_MAX
         OR
         T1.NOM_PRE_GTEES > T6.VALUE_GREATER_THAN
         AND
         T1.NOM_PRE_GTEES < T6.VALUE_MAX
         AND
    _     T1.GROUP_ID = T6.GROUP_ID_
         AND
         T6.FALLBACK_IND = ‘N’
    LEFT OUTER JOIN /* this is the condition without Group ID*/
         RESIDUAL_CONTROL T10
         ON
         T1.NOM_PRE_GTEES = T10.VALUE_MAX
         OR
         T1.NOM_PRE_GTEES > T10.VALUE_GREATER_THAN
         AND
         T1.NOM_PRE_GTEES < T10.VALUE_MAX
         AND
         T10.FALLBACK_IND = ‘N’
         CROSS JOIN
         RESIDUAL_CONTROL T5
         ON T5.FALLBACK_IND = ‘Y’
    Thanks in advance.

    KVB wrote:
    With and Without Group ID,I am populating a value into a column,Can anyone please help to write the same query in an other way.This is a sample column.Likewise,there are 5 more columns to be populated.So that I have to write 2 left outer joins for a single column with a little difference.
    SELECT
    CASE
         WHEN T6.ACTUAL_FUNCTION IS NOT NULL
         THEN T6.ACTUAL_FUNCTION
         WHEN T10.ACTUAL_FUNCTION IS NOT NULL
         THEN T10.ACTUAL_FUCNTION
              ELSE T5.ACTUAL_FUCNTION
    END AF_NOM_PRE_GTEES
    FROM
    ABC_WT17 T1
         INNER JOIN
         ABC_CUSTOMER_WT01 T3
         ON T1.ORIGINAL_SDS_PARTY_IDR=T3.ORIGINAL_SDS_PARTY_IDR
         AND T1.IDR_TYPE_CODE=T3.IDR_TYPE_CODE
         LEFT OUTER JOIN /* This is the condition with group ID*/
         RESIDUAL_CONTROL T6
         ON
         T1.NOM_PRE_GTEES = T6.VALUE_MAX
         OR
         T1.NOM_PRE_GTEES > T6.VALUE_GREATER_THAN
         AND
         T1.NOM_PRE_GTEES < T6.VALUE_MAX
         AND
    _     T1.GROUP_ID = T6.GROUP_ID_
         AND
         T6.FALLBACK_IND = ‘N’
    LEFT OUTER JOIN /* this is the condition without Group ID*/
         RESIDUAL_CONTROL T10
         ON
         T1.NOM_PRE_GTEES = T10.VALUE_MAX
         OR
         T1.NOM_PRE_GTEES > T10.VALUE_GREATER_THAN
         AND
         T1.NOM_PRE_GTEES < T10.VALUE_MAX
         AND
         T10.FALLBACK_IND = ‘N’
         CROSS JOIN
         RESIDUAL_CONTROL T5
         ON T5.FALLBACK_IND = ‘Y’
    Thanks in advance.Thread: HOW TO: Post a SQL statement tuning request - template posting
    HOW TO: Post a SQL statement tuning request - template posting

  • How to write the query using Index

    Hi All,
    I have to fetch the records from Database table using Index, how can i write the query using Index. Can any body plz send me the sample code.
    Help Me,
    Balu.

    Hi,
    See the below Example.
    select * from vbak up to 100 rows
    into table t_vbak
    where vbeln > v_vbeln.
    sort t_vbak by vbeln descending.
    read table t_vbak index 1.
    Regards,
    Ram
    Pls reward points if helpful.

  • Help to rewrite the query --performance issue

    Hi ,
    Please help to rewrite the query since it's performance is not good.Especially second inline query(CASE statements are therein select caluse ..)is taking more cost.
    Database Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - 64bi
    SELECT *
    FROM
      (SELECT q.*,
        COUNT(*) OVER() AS record_count,
        ROWNUM          AS row_num
      FROM
        (SELECT ExName.examiner_code,
          examiner_name,
          :v_year,
          :v_month,
          count_fb,
          NVL(count_entered_fb, 0) count_entered_fb,
          NVL(count_sent_fb, 0) count_sent_fb,
          NVL(count_edited_fb, 0) count_edited_fb,
          NVL(count_complete_fb, 0) count_complete_fb,
          NVL(count_withibcardiff_fb, 0) count_withibcardiff_fb
        FROM
          (SELECT examiner_code,
            COUNT(*) AS count_fb
          FROM
            (SELECT
                        examiner_code,
              paper_code,
              assessment_school
            FROM
              ( SELECT DISTINCT ce.examiner_code,
                ce.paper_code,
                ce.assessment_school
              FROM
                (SELECT
                  DISTINCT assessment_school,
                  paper_code,
                  examiner_code
                FROM candidate_examiner_allocation cea
                WHERE cea.element = 'Moderation of IA'
                AND cea.year      = :v_year
                AND cea.month     = :v_month
                ) ce,
                subject_group sg,
                subject_component sc
              WHERE (:v_padded_examiner_code IS NULL
              OR ce.examiner_code            = :v_padded_examiner_code)
              AND (:v_subject_group          IS NULL
              OR sg.group_number             = :v_subject_group)
              AND sg.year                    = :v_year
              AND sg.month                   = :v_month
              AND sc.year                    = :v_year
              AND sc.month                   = :v_month
              AND sc.paper_code              = ce.paper_code
              AND sc.subject                 = sg.subject
              AND sc.lvl                     = sg.lvl
              AND (:v_subject                IS NULL
              OR sc.subject                  = :v_subject)
              AND (:v_lvl                    IS NULL
              OR sc.lvl                      = :v_lvl)
              ) ea
          GROUP BY examiner_code
          ) ExName,
          (SELECT examiner_code,
            COUNT(
            CASE
              WHEN UPPER(wfi.status) = 'ENTERED'
              THEN 1
              ELSE NULL
            END) AS count_entered_fb,
            COUNT(
            CASE
              WHEN UPPER(wfi.status) = 'SENT'
              THEN 1
              ELSE NULL
            END) AS count_sent_fb,
            COUNT(
            CASE
              WHEN UPPER(wfi.status) = 'EDITED'
              THEN 1
              ELSE NULL
            END) AS count_edited_fb,
            COUNT(
            CASE
              WHEN UPPER(wfi.status) = 'COMPLETE'
              THEN 1
              ELSE NULL
            END) AS count_complete_fb,
            COUNT(
            CASE
              WHEN UPPER(wfi.status) = 'WITH IBCARDIFF'
              THEN 1
              ELSE NULL
            END) AS count_withibcardiff_fb
          FROM ia_instances ia1,
            workflow_instance wfi
          WHERE wfi.instance_id = ia1.workflow_instance_id
          AND ia1.year          = :v_year
          AND ia1.month         = :v_month
          GROUP BY ia1.year,
            ia1.month,
            examiner_code
          ) iaF,
          (SELECT person_code,
            title
            || ' '
            || firstname
            || ' '
            || lastname AS examiner_name
          FROM person
          WHERE :v_examiner_name IS NULL
          OR UPPER(title
            || ' '
            || firstname
            || ' '
            || lastname) LIKE :v_search_examiner_name
          ) P
        WHERE ExName.examiner_code = iaF.examiner_code (+)
        AND ExName.examiner_code   = p.person_code
        ORDER BY ExName.examiner_code
        ) q
      ) rc
    WHERE row_num >= :v_start_row
    AND row_num   <= (:v_start_row+(:v_max_row-1));explain plan
    line 1: SQLPLUS Command Skipped: set linesize 130
    line 2: SQLPLUS Command Skipped: set pagesize 0
    PLAN_TABLE_OUTPUT                                                                                                                                                                                                                                                                                           
    Plan hash value: 1581970599                                                                                                                                                                                                                                                                                 
    | Id  | Operation                                 | Name                           | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |                                                                                                                                                                        
    |   0 | SELECT STATEMENT                          |                                |     1 |   276 |       |  2187   (6)| 00:00:34 |                                                                                                                                                                        
    |*  1 |  FILTER                                   |                                |       |       |       |            |          |                                                                                                                                                                        
    |*  2 |   VIEW                                    |                                |     1 |   276 |       |  2187   (6)| 00:00:34 |                                                                                                                                                                        
    |   3 |    WINDOW BUFFER                          |                                |     1 |   250 |       |  2187   (6)| 00:00:34 |                                                                                                                                                                        
    |   4 |     COUNT                                 |                                |       |       |       |            |          |                                                                                                                                                                        
    |   5 |      VIEW                                 |                                |     1 |   250 |       |  2187   (6)| 00:00:34 |                                                                                                                                                                        
    |   6 |       SORT ORDER BY                       |                                |     1 |   119 |       |  2187   (6)| 00:00:34 |                                                                                                                                                                        
    |   7 |        NESTED LOOPS                       |                                |     1 |   119 |       |  2186   (6)| 00:00:34 |                                                                                                                                                                        
    |*  8 |         HASH JOIN OUTER                   |                                |     1 |    92 |       |  2185   (6)| 00:00:34 |                                                                                                                                                                        
    |   9 |          VIEW                             |                                |     1 |    20 |       |    51   (4)| 00:00:01 |                                                                                                                                                                        
    |  10 |           SORT GROUP BY                   |                                |     1 |     7 |       |    51   (4)| 00:00:01 |                                                                                                                                                                        
    |  11 |            VIEW                           |                                |     1 |     7 |       |    51   (4)| 00:00:01 |                                                                                                                                                                        
    |  12 |             SORT UNIQUE                   |                                |     1 |   127 |       |    51   (4)| 00:00:01 |                                                                                                                                                                        
    |  13 |              NESTED LOOPS                 |                                |     1 |   127 |       |    50   (2)| 00:00:01 |                                                                                                                                                                        
    |* 14 |               HASH JOIN                   |                                |     1 |    68 |       |    44   (3)| 00:00:01 |                                                                                                                                                                        
    |* 15 |                TABLE ACCESS BY INDEX ROWID| SUBJECT_COMPONENT              |    13 |   520 |       |    40   (0)| 00:00:01 |                                                                                                                                                                        
    |* 16 |                 INDEX RANGE SCAN          | SUBJECT_COMPONENT_ASSESS_TYPE  |  1059 |       |       |     9   (0)| 00:00:01 |                                                                                                                                                                        
    |* 17 |                INDEX RANGE SCAN           | SUBJECT_GROUP_PK               |    41 |  1148 |       |     3   (0)| 00:00:01 |                                                                                                                                                                        
    |* 18 |               INDEX RANGE SCAN            | CEA_AUTOMATIC_ALLOCATION_STATS |     5 |   295 |       |     6   (0)| 00:00:01 |                                                                                                                                                                        
    |  19 |          VIEW                             |                                |   679 | 48888 |       |  2133   (6)| 00:00:33 |                                                                                                                                                                        
    |  20 |           SORT GROUP BY                   |                                |   679 | 25123 |       |  2133   (6)| 00:00:33 |                                                                                                                                                                        
    |* 21 |            HASH JOIN                      |                                | 52408 |  1893K|  1744K|  2126   (6)| 00:00:33 |                                                                                                                                                                        
    |  22 |             TABLE ACCESS BY INDEX ROWID   | IA_INSTANCES                   | 52408 |  1125K|       |   688   (1)| 00:00:11 |                                                                                                                                                                        
    |* 23 |              INDEX RANGE SCAN             | IND_IA_INSTANCES               | 49077 |       |       |   137   (2)| 00:00:03 |                                                                                                                                                                        
    |  24 |             TABLE ACCESS FULL             | WORKFLOW_INSTANCE              |  1075K|    15M|       |   960   (7)| 00:00:15 |                                                                                                                                                                        
    |* 25 |         TABLE ACCESS BY INDEX ROWID       | PERSON                         |     1 |    27 |       |     1   (0)| 00:00:01 |                                                                                                                                                                        
    |* 26 |          INDEX UNIQUE SCAN                | PERSON_PK                      |     1 |       |       |     0   (0)| 00:00:01 |                                                                                                                                                                        
    Predicate Information (identified by operation id):                                                                                                                                                                                                                                                         
       1 - filter(TO_NUMBER(:V_START_ROW)<=TO_NUMBER(:V_START_ROW)+(TO_NUMBER(:V_MAX_ROW)-1))                                                                                                                                                                                                                   
       2 - filter("ROW_NUM">=TO_NUMBER(:V_START_ROW) AND "ROW_NUM"<=TO_NUMBER(:V_START_ROW)+(TO_NUMBER(:V_MAX_ROW)-1))                                                                                                                                                                                          
       8 - access("EXNAME"."EXAMINER_CODE"="IAF"."EXAMINER_CODE"(+))                                                                                                                                                                                                                                            
      14 - access("SC"."SUBJECT"="SG"."SUBJECT" AND "SC"."LVL"="SG"."LVL")                                                                                                                                                                                                                                      
      15 - filter((:V_SUBJECT IS NULL OR "SC"."SUBJECT"=:V_SUBJECT) AND ("SC"."LVL"=:V_LVL OR :V_LVL IS NULL))                                                                                                                                                                                                  
      16 - access("SC"."YEAR"=TO_NUMBER(:V_YEAR) AND "SC"."MONTH"=:V_MONTH)                                                                                                                                                                                                                                     
      17 - access("SG"."YEAR"=TO_NUMBER(:V_YEAR) AND "SG"."MONTH"=:V_MONTH)                                                                                                                                                                                                                                     
           filter(:V_SUBJECT_GROUP IS NULL OR "SG"."GROUP_NUMBER"=TO_NUMBER(:V_SUBJECT_GROUP))                                                                                                                                                                                                                  
      18 - access("CEA"."YEAR"=TO_NUMBER(:V_YEAR) AND "CEA"."MONTH"=:V_MONTH AND "SC"."PAPER_CODE"="PAPER_CODE" AND                                                                                                                                                                                             
                  "CEA"."ELEMENT"='Moderation of IA')                                                                                                                                                                                                                                                           
           filter("CEA"."ELEMENT"='Moderation of IA' AND (:V_PADDED_EXAMINER_CODE IS NULL OR                                                                                                                                                                                                                    
                  "EXAMINER_CODE"=:V_PADDED_EXAMINER_CODE))                                                                                                                                                                                                                                                     
      21 - access("WFI"."INSTANCE_ID"="IA1"."WORKFLOW_INSTANCE_ID")                                                                                                                                                                                                                                             
      23 - access("IA1"."YEAR"=TO_NUMBER(:V_YEAR) AND "IA1"."MONTH"=:V_MONTH)                                                                                                                                                                                                                                   
      25 - filter(:V_EXAMINER_NAME IS NULL OR UPPER("TITLE"||' '||"FIRSTNAME"||' '||"LASTNAME") LIKE :V_SEARCH_EXAMINER_NAME)                                                                                                                                                                                   
      26 - access("EXNAME"."EXAMINER_CODE"="PERSON_CODE")                                                                                                                                                                                                                                                       
    53 rows selected

    Hi,
    please find the below rigjt explan paln.
    PLAN_TABLE_OUTPUT                                                                                                                                                                                                                                                                                           
    SQL_ID  2ct41vyyzqyh7, child number 0                                                                                                                                                                                                                                                                       
    SELECT *  FROM    (SELECT q.*,      COUNT(*) OVER() AS record_count,      ROWNUM          AS row_num    FROM      (SELECT                                                                                                                                                                                   
    ExName.examiner_code,        examiner_name,        :v_year,        :v_month,        count_fb,        NVL(count_entered_fb,                                                                                                                                                                                  
    0) count_entered_fb,        NVL(count_sent_fb, 0) count_sent_fb,        NVL(count_edited_fb, 0) count_edited_fb,                                                                                                                                                                                            
    NVL(count_complete_fb, 0) count_complete_fb,        NVL(count_withibcardiff_fb, 0) count_withibcardiff_fb      FROM                                                                                                                                                                                         
    (SELECT examiner_code,          COUNT(*) AS count_fb        FROM          (SELECT                                                                                                                                                                                                 
           examiner_code,            paper_code,            assessment_school          FROM            ( SELECT DISTINCT                                                                                                                                                                                        
    ce.examiner_code,              ce.paper_code,              ce.assessment_school            FROM              (SELECT                                                                                                                                                                                        
                            DISTINCT assessment_school,                                                                                                                                                                                                          
    paper_code,                examiner                                                                                                                                                                                                                                                                         
    Plan hash value: 651311258                                                                                                                                                                                                                                                                                  
    | Id  | Operation                                | Name                           | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |                                                                                                                                                                         
    |   0 | SELECT STATEMENT                         |                                |       |       |       |  2785 (100)|          |                                                                                                                                                                         
    |*  1 |  FILTER                                  |                                |       |       |       |            |          |                                                                                                                                                                         
    |*  2 |   VIEW                                   |                                |     4 |  1104 |       |  2785   (7)| 00:00:43 |                                                                                                                                                                         
    |   3 |    WINDOW BUFFER                         |                                |     4 |  1000 |       |  2785   (7)| 00:00:43 |                                                                                                                                                                         
    |   4 |     COUNT                                |                                |       |       |       |            |          |                                                                                                                                                                         
    |   5 |      VIEW                                |                                |     4 |  1000 |       |  2785   (7)| 00:00:43 |                                                                                                                                                                         
    |   6 |       NESTED LOOPS                       |                                |     4 |   476 |       |  2785   (7)| 00:00:43 |                                                                                                                                                                         
    |   7 |        MERGE JOIN OUTER                  |                                |     4 |   368 |       |  2781   (7)| 00:00:43 |                                                                                                                                                                         
    |   8 |         VIEW                             |                                |     4 |    80 |       |    72   (3)| 00:00:02 |                                                                                                                                                                         
    |   9 |          SORT GROUP BY                   |                                |     4 |    28 |       |    72   (3)| 00:00:02 |                                                                                                                                                                         
    |  10 |           VIEW                           |                                |     4 |    28 |       |    72   (3)| 00:00:02 |                                                                                                                                                                         
    |  11 |            SORT UNIQUE                   |                                |     4 |   508 |       |    72   (3)| 00:00:02 |                                                                                                                                                                         
    |  12 |             NESTED LOOPS                 |                                |     4 |   508 |       |    71   (2)| 00:00:02 |                                                                                                                                                                         
    |* 13 |              HASH JOIN                   |                                |     1 |    68 |       |    44   (3)| 00:00:01 |                                                                                                                                                                         
    |* 14 |               TABLE ACCESS BY INDEX ROWID| SUBJECT_COMPONENT              |    13 |   520 |       |    40   (0)| 00:00:01 |                                                                                                                                                                         
    |* 15 |                INDEX RANGE SCAN          | SUBJECT_COMPONENT_ASSESS_TYPE  |  1059 |       |       |     9   (0)| 00:00:01 |                                                                                                                                                                         
    |* 16 |               INDEX RANGE SCAN           | SUBJECT_GROUP_PK               |    41 |  1148 |       |     3   (0)| 00:00:01 |                                                                                                                                                                         
    |* 17 |              INDEX RANGE SCAN            | CEA_AUTOMATIC_ALLOCATION_STATS |    30 |  1770 |       |    27   (0)| 00:00:01 |                                                                                                                                                                         
    |* 18 |         SORT JOIN                        |                                |   576 | 41472 |       |  2709   (7)| 00:00:42 |                                                                                                                                                                         
    |  19 |          VIEW                            |                                |   576 | 41472 |       |  2708   (7)| 00:00:42 |                                                                                                                                                                         
    |  20 |           SORT GROUP BY                  |                                |   576 | 21312 |       |  2708   (7)| 00:00:42 |                                                                                                                                                                         
    |* 21 |            HASH JOIN                     |                                | 52408 |  1893K|  1744K|  2701   (7)| 00:00:41 |                                                                                                                                                                         
    |* 22 |             TABLE ACCESS FULL            | IA_INSTANCES                   | 52408 |  1125K|       |  1263   (6)| 00:00:20 |                                                                                                                                                                         
    |  23 |             TABLE ACCESS FULL            | WORKFLOW_INSTANCE              |  1075K|    15M|       |   960   (7)| 00:00:15 |                                                                                                                                                                         
    |* 24 |        TABLE ACCESS BY INDEX ROWID       | PERSON                         |     1 |    27 |       |     1   (0)| 00:00:01 |                                                                                                                                                                         
    |* 25 |         INDEX UNIQUE SCAN                | PERSON_PK                      |     1 |       |       |     0   (0)|          |                                                                                                                                                                         
    Predicate Information (identified by operation id):                                                                                                                                                                                                                                                         
       1 - filter(TO_NUMBER(:V_START_ROW)<=TO_NUMBER(:V_START_ROW)+(TO_NUMBER(:V_MAX_ROW)-1))                                                                                                                                                                                                                   
       2 - filter(("ROW_NUM">=TO_NUMBER(:V_START_ROW) AND "ROW_NUM"<=TO_NUMBER(:V_START_ROW)+(TO_NUMBER(:V_MAX_ROW)-1)))                                                                                                                                                                                        
      13 - access("SC"."SUBJECT"="SG"."SUBJECT" AND "SC"."LVL"="SG"."LVL")                                                                                                                                                                                                                                      
      14 - filter(((:V_SUBJECT IS NULL OR "SC"."SUBJECT"=:V_SUBJECT) AND ("SC"."LVL"=:V_LVL OR :V_LVL IS NULL)))                                                                                                                                                                                                
      15 - access("SC"."YEAR"=TO_NUMBER(:V_YEAR) AND "SC"."MONTH"=:V_MONTH)                                                                                                                                                                                                                                     
      16 - access("SG"."YEAR"=TO_NUMBER(:V_YEAR) AND "SG"."MONTH"=:V_MONTH)                                                                                                                                                                                                                                     
           filter((:V_SUBJECT_GROUP IS NULL OR "SG"."GROUP_NUMBER"=TO_NUMBER(:V_SUBJECT_GROUP)))                                                                                                                                                                                                                
      17 - access("CEA"."YEAR"=TO_NUMBER(:V_YEAR) AND "CEA"."MONTH"=:V_MONTH AND "SC"."PAPER_CODE"="PAPER_CODE" AND                                                                                                                                                                                             
                  "CEA"."ELEMENT"='Moderation of IA')                                                                                                                                                                                                                                                           
           filter(("CEA"."ELEMENT"='Moderation of IA' AND (:V_PADDED_EXAMINER_CODE IS NULL OR                                                                                                                                                                                                                   
                  "EXAMINER_CODE"=:V_PADDED_EXAMINER_CODE)))                                                                                                                                                                                                                                                    
      18 - access("EXNAME"."EXAMINER_CODE"="IAF"."EXAMINER_CODE")                                                                                                                                                                                                                                               
           filter("EXNAME"."EXAMINER_CODE"="IAF"."EXAMINER_CODE")                                                                                                                                                                                                                                               
      21 - access("WFI"."INSTANCE_ID"="IA1"."WORKFLOW_INSTANCE_ID")                                                                                                                                                                                                                                             
      22 - filter(("IA1"."MONTH"=:V_MONTH AND "IA1"."YEAR"=TO_NUMBER(:V_YEAR)))                                                                                                                                                                                                                                 
      24 - filter((:V_EXAMINER_NAME IS NULL OR UPPER("TITLE"||' '||"FIRSTNAME"||' '||"LASTNAME") LIKE :V_SEARCH_EXAMINER_NAME))                                                                                                                                                                                 
      25 - access("EXNAME"."EXAMINER_CODE"="PERSON_CODE")                                                                                                                                                                                                                                                       
    66 rows selected

  • Need help th tuning query or re write the query--

    Hi,
    Need help to tune the below query or rewrite th query for reducing the execution time Please find the query and explain plan.
    QUERY
    explain plan FOR SELECT consumer_key,product_key,days_in_product,20100201 period_key FROM
    (SELECT consumer_key,
      product_key,
      days_in_product,
      row_number() over ( Partition BY consumer_key order by Days_in_product DESC) row_num
    FROM
      (SELECT consumer_key,
        product_key,
        SUM(no_ofdays) days_in_product
      FROM
        (SELECT pcv.consumer_key,
          pcv.product_key,
          pcv.product_consumer_valid_from,
          pcv.product_consumer_valid_to,
          DECODE (SIGN(20100201000000-product_consumer_valid_from),1,20100201000000,product_consumer_valid_from) period_start,
          DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959) period_end,
          CASE
            WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) BETWEEN 20100201000000 AND 20100228235959
            AND activation_date > to_Date(product_consumer_valid_to,'YYYYMMDDHH24MISS')
            THEN 0
            WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) BETWEEN 20100201000000 AND 20100228235959
            AND activation_date BETWEEN to_Date(product_consumer_valid_from,'YYYYMMDDHH24MISS') AND to_Date(product_consumer_valid_to,'YYYYMMDDHH24MISS')
            THEN
              --to_char(activation_date,'MON-YYYY')='PERIOD_ACTIVE'  and activation_date >= to_Date(product_consumer_valid_from,'YYYYMMDDHH24MISS') then
              (to_date(DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959),'YYYYMMDDHH24MISS') - to_date(TO_CHAR(activation_date,'YYYYMMDDHH24MISS'),'YYYYMMDDHH24MISS') )
            WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) < 20100201000000
            THEN (to_date(DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959),'YYYYMMDDHH24MISS') - to_Date(DECODE (SIGN(20100201000000-product_consumer_valid_from),1,20100201000000,product_consumer_valid_from),'YYYYMMDDHH24MISS') )
            WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) > 20100228235959
            THEN 0
            ELSE
              --unusual situation
              (to_date(DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959),'YYYYMMDDHH24MISS') - to_Date(DECODE (SIGN(20100201000000-product_consumer_valid_from),1,20100201000000,product_consumer_valid_from),'YYYYMMDDHH24MISS') )
          END No_ofDays
        FROM cimtran.product_consumer_validity pcv,
          consumer_dimension cd
        WHERE pcv.consumer_key           =cd.consumer_key
        AND product_consumer_valid_to   >= 20100201000000
        AND product_consumer_valid_from <= 20100228235959
          --and product_consumer_valid_from > '20090801000000'
        ORDER BY consumer_key,
          product_key,
          product_consumer_valid_from
        ) a
      GROUP BY consumer_key,
        product_key
      ORDER BY consumer_key,
        product_key
    ) WHERE row_num=1 ;EXPLAIN PLAN
    "PLAN_TABLE_OUTPUT"
    "Plan hash value: 3823907703"
    "| Id  | Operation                | Name                      | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |"
    "|   0 | SELECT STATEMENT         |                           |  4665K|   231M|       |   133K  (1)| 00:31:08 |"
    "|*  1 |  VIEW                    |                           |  4665K|   231M|       |   133K  (1)| 00:31:08 |"
    "|*  2 |   WINDOW SORT PUSHED RANK|                           |  4665K|   173M|   232M|   133K  (1)| 00:31:08 |"
    "|   3 |    VIEW                  |                           |  4665K|   173M|       |   104K  (1)| 00:24:18 |"
    "|   4 |     SORT GROUP BY        |                           |  4665K|   182M|   729M|   104K  (1)| 00:24:18 |"
    "|*  5 |      HASH JOIN           |                           |    13M|   533M|    65M| 44241   (1)| 00:10:20 |"
    "|   6 |       TABLE ACCESS FULL  | CONSUMER_DIMENSION        |  2657K|    35M|       |  4337   (1)| 00:01:01 |"
    "|*  7 |       TABLE ACCESS FULL  | PRODUCT_CONSUMER_VALIDITY |    13M|   351M|       | 15340   (2)| 00:03:35 |"
    "Predicate Information (identified by operation id):"
    "   1 - filter(""ROW_NUM""=1)"
    "   2 - filter(ROW_NUMBER() OVER ( PARTITION BY ""CONSUMER_KEY"" ORDER BY "
    "              INTERNAL_FUNCTION(""DAYS_IN_PRODUCT"") DESC )<=1)"
    "   5 - access(""PCV"".""CONSUMER_KEY""=""CD"".""CONSUMER_KEY"")"
    "   7 - filter(""PRODUCT_CONSUMER_VALID_FROM""<=20100228235959 AND "
    "              ""PRODUCT_CONSUMER_VALID_TO"">=20100201000000)"

    I doubt that this query can be tuned without using indexes. There is a lot of unnecessary work specified in your query, like unnecessary intermediate sorting and selecting unused columns. The cost based optimizer recognized it and skips some of that unnecessary work, it seems. For clarity's sake, I would rewrite your query like below. Note that the query is untested:
    select consumer_key
         , max(product_key) keep (dense_rank last order by days_in_product) product_key
         , max(days_in_product) days_in_product
         , 20100201 period_key
      from ( select pcv.consumer_key
                  , pcv.product_key
                  , sum
                    ( case
                      when to_number(to_char(cd.activation_date,'yyyymmddhh24miss')) between 20100201000000 and 20100228235959
                      then
                        case
                        when cd.activation_date > to_date(pcv.product_consumer_valid_to,'yyyymmddhh24miss')
                        then
                          0
                        when cd.activation_date between to_date(pcv.product_consumer_valid_from,'yyyymmddhh24miss') and to_date(product_consumer_valid_to,'yyyymmddhh24miss')
                        then
                          to_date(to_char(pcv.product_consumer_valid_to),'yyyymmddhh24miss'))
                          - to_date(to_char(activation_date,'yyyymmddhh24miss'),'yyyymmddhh24miss')
                        end
                      when to_number(to_char(cd.activation_date,'yyyymmddhh24miss')) < 20100201000000
                      then
                        to_date(to_char(pcv.product_consumer_valid_to),'yyyymmddhh24miss'))
                        - to_date(to_char(pcv.product_consumer_valid_from),'yyyymmddhh24miss'))
                      when to_number(to_char(cd.activation_date,'yyyymmddhh24miss')) > 20100228235959
                      then
                        0
                      end
                    ) days_in_product
               from cimtran.product_consumer_validity pcv
                  , consumer_dimension cd
              where pcv.consumer_key             = cd.consumer_key
                and product_consumer_valid_to   >= 20100201000000
                and product_consumer_valid_from <= 20100228235959
              group by consumer_key
                  , product_key
    group by consumer_keyRegards,
    Rob.

  • Help to write the sql server 2012(business intelligence ) certification.

    Hi,
    I am planning to write the SQL SERVER 2012 certification exam.
    i want few more details...
    1. Examination code.
    2. how to prepare
    3. is there any dumps available.
    4. is there any books available for preparation.
    Please pass the details, it would be very help full for me..

    The Business Intelligence exams are:
    70-461: Querying Microsoft SQL Server 2012
    70-462: Administering Microsoft SQL Server 2012 Databases
    70-463: Implementing a Data Warehouse with Microsoft SQL Server 2012
    70-466: Implementing Data Models and Reports with Microsoft SQL Server 2012
    70-467: Designing Business Intelligence Solutions with Microsoft SQL Server 2012
    Ramesh Babu Vavilla MCTS,MSBI

  • 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;

  • 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>

  • Help in generating the query

    Hi All,
    I have the below requirement.
    Master table:
    Product ID      Product Name
    1               TV
    2               Cellphone
    3               Laptop
    4               DVD Player
    Details table:
    Product ID     Spec type     Spec Detail
    1               Color          Blue
    1               Weight          20
    2               Color          Blue
    2               Weight          20
    3               Color          Blue
    3                Weight          25
    4               Color          Blue
    4               Weight          20
    4               Height          10Im looking for a way to identify the products that has exact same specifications.
    ie., in the above example TV and Cellphone has the exact specifications.
    So the output of the query should be TV and Cellphone here.
    Is it possible to do in a query or we need to have some sql block to perform this action.
    Any help would be greatly appreciated.
    Thanks,
    Agathya

    Hi agathya,
    We don't know how you would like to handle the situation where more than two products have the same specifications, so I extended your sample data a bit to reflect that situation as well:
    SQL> create table products (id,name)
      2  as
      3  select 1, 'TV' from dual union all
      4  select 2, 'Cellphone' from dual union all
      5  select 3, 'Laptop' from dual union all
      6  select 4, 'DVD Player' from dual union all
      7  select 5, 'Radio' from dual union all
      8  select 6, 'VCR' from dual
      9  /
    Tabel is aangemaakt.
    SQL> create table product_specifications (product_id,spec_type,spec_detail)
      2  as
      3  select 1, 'Color' , 'Blue' from dual union all
      4  select 1, 'Weight', '20'   from dual union all
      5  select 2, 'Color' , 'Blue' from dual union all
      6  select 2, 'Weight', '20'   from dual union all
      7  select 3, 'Color' , 'Blue' from dual union all
      8  select 3, 'Weight', '25'   from dual union all
      9  select 4, 'Color' , 'Blue' from dual union all
    10  select 4, 'Weight', '20'   from dual union all
    11  select 4, 'Height', '10'   from dual union all
    12  select 5, 'Color' , 'Blue' from dual union all
    13  select 5, 'Weight', '20'   from dual union all
    14  select 6, 'Color' , 'Blue' from dual union all
    15  select 6, 'Weight', '20'   from dual union all
    16  select 6, 'Height', '10'   from dual
    17  /
    Tabel is aangemaakt.
    SQL> select p1.name
      2       , p2.name
      3    from products p1
      4       , products p2
      5       , product_specifications ps1
      6       , product_specifications ps2
      7   where p1.id = ps1.product_id
      8     and p2.id = ps2.product_id
      9     and p1.id < p2.id
    10   group by p1.id
    11       , p1.name
    12       , p2.id
    13       , p2.name
    14  having sqrt(count(*)) =
    15         count(case when ps1.spec_type = ps2.spec_type and ps1.spec_detail = ps2.spec_detail then 1 end)
    16  /
    NAME       NAME
    TV         Cellphone
    TV         Radio
    Cellphone  Radio
    DVD Player VCR
    4 rijen zijn geselecteerd.But why on earth did you or your predecessor model product specifications like this? You are making it far more complicated than it needs to be. And the datatype of height and weight should not be the same as for color. But now they are. And how do you restrict the allowable values for colors? Color, weight and height are attributes of your products so they should have been modeled like that.
    See how easy your questions becomes after remodeling it:
    SQL> drop table product_specifications purge
      2  /
    Tabel is verwijderd.
    SQL> drop table products purge
      2  /
    Tabel is verwijderd.
    SQL> create table products (id,name,color,weight,height)
      2  as
      3  select 1, 'TV', 'Blue', 20, null from dual union all
      4  select 2, 'Cellphone', 'Blue', 20, null from dual union all
      5  select 3, 'Laptop', 'Blue', 25, null from dual union all
      6  select 4, 'DVD Player', 'Blue', 20, 10 from dual union all
      7  select 5, 'Radio', 'Blue', 20, null from dual union all
      8  select 6, 'VCR', 'Blue', 20, 10 from dual
      9  /
    Tabel is aangemaakt.
    SQL> select * from products
      2  /
            ID NAME       COLO     WEIGHT     HEIGHT
             1 TV         Blue         20
             2 Cellphone  Blue         20
             3 Laptop     Blue         25
             4 DVD Player Blue         20         10
             5 Radio      Blue         20
             6 VCR        Blue         20         10
    6 rijen zijn geselecteerd.
    SQL> select p1.name
      2       , p2.name
      3    from products p1
      4       , products p2
      5   where p1.id < p2.id
      6     and ( p1.color = p2.color or (p1.color is null and p2.color is null))
      7     and ( p1.weight = p2.weight or (p1.weight is null and p2.weight is null))
      8     and ( p1.height = p2.height or (p1.height is null and p2.height is null))
      9  /
    NAME       NAME
    TV         Cellphone
    TV         Radio
    Cellphone  Radio
    DVD Player VCR
    4 rijen zijn geselecteerd.Hope this helps.
    Regards,
    Rob.

  • Help in correcting the query

    Hi,
    I have below query:
    select u.v_um_code,v_um_name,count(f.v_fc_code), count(s.n_sl_no), count(distinct s.n_agent_no)
    from subm_buss_um_master u,subm_buss_fc_master f,submitted_business_raw s
    where u.v_um_code=f.v_um_code and f.v_fc_code=s.n_agent_no
    and f.v_status='A' and u.v_status='A'
    and trunc(d_submitted) between '01-JUL-2004' AND '31-JUL-2004'
    group by u.v_um_code,v_um_name;
    the output should be such that first all the records should be fetched from subm_buss_um_master table (where u.v_status='A')--> then count(f.v_fc_code) then -->count(s.n_sl_no), count(distinct s.n_agent_no)
    the data/columns in these tables is large, I cannot paste here.
    Thanks in advance
    Tariq.

    Hi,
    If SET operator UNION helps then great!!!
    For example: As per my requirement, first all employees and sum of salary.....is the query.
    SELECT ename, sal FROM emp
    UNION
    SELECT 'Grand Total ', SUM(sal) FROM emp
    ORDER BY sal;
    Regards,
    Sailaja

  • Help in rewrite the Query

    Hello
    I have one query taking time to fetch the records. Table contains just 40,000 thousands records but lots of case statement in the query, I just remove lots of case statement there are lot more...
    Can you please help me out to rewrite the query to fetch the records faster. Also Right now there are no indexes on the table....
    SELECT O.cn,
           O.BEN_LAST_NAME,
           O.BEN_FIRST_NAME,
           O.BEN_MI,
           O.SSN,
           O.DOB,
           O.SEX,
           CASE
              WHEN O.SEX = 'F' AND O.AGE BETWEEN 00 AND 34 THEN
               1
              ELSE
               0
           END AS AGE_GROUP0_34_F,
           CASE
              WHEN O.SEX = 'F' AND O.AGE BETWEEN 35 AND 44 THEN
               1
              ELSE
               0
           END AS AGE_GROUP35_44_F,
           CASE
              WHEN O.SEX = 'F' AND O.AGE BETWEEN 45 AND 54 THEN
               1
              ELSE
               0
           END AS AGE_GROUP45_54_F,
           CASE
              WHEN O.SEX = 'F' AND O.AGE BETWEEN 55 AND 59 THEN
               1
              ELSE
               0
           END AS AGE_GROUP55_59_F,
           CASE
              WHEN O.SEX = 'F' AND O.AGE BETWEEN 60 AND 64 THEN
               1
              ELSE
               0
           END AS AGE_GROUP60_64_F,
           CASE
              WHEN O.SEX = 'F' AND O.AGE BETWEEN 65 AND 69 THEN
               1
              ELSE
               0
           END AS AGE_GROUP65_69_F,
           CASE
              WHEN O.SEX = 'F' AND O.AGE BETWEEN 70 AND 74 THEN
               1
              ELSE
               0
           END AS AGE_GROUP70_74_F,
           CASE
              WHEN O.SEX = 'F' AND O.AGE BETWEEN 75 AND 79 THEN
               1
              ELSE
               0
           END AS AGE_GROUP75_79_F,
           CASE
              WHEN O.SEX = 'F' AND O.AGE BETWEEN 80 AND 84 THEN
               1
              ELSE
               0
           END AS AGE_GROUP80_84_F,
           CASE
              WHEN O.SEX = 'F' AND O.AGE BETWEEN 85 AND 89 THEN
               1
              ELSE
               0
           END AS AGE_GROUP85_89_F,
           CASE
              WHEN O.SEX = 'F' AND O.AGE BETWEEN 90 AND 94 THEN
               1
              ELSE
               0
           END AS AGE_GROUP90_94_F,
           CASE
              WHEN O.SEX = 'F' AND O.AGE BETWEEN 95 AND 00 THEN
               1
              ELSE
               0
           END AS AGE_GROUP95_GT_F,
           CASE
              WHEN O.SEX = 'M' AND O.AGE BETWEEN 00 AND 34 THEN
               1
              ELSE
               0
           END AS AGE_GROUP0_34_M,
           CASE
              WHEN O.SEX = 'M' AND O.AGE BETWEEN 35 AND 44 THEN
               1
              ELSE
               0
           END AS AGE_GROUP35_44_M,
           CASE
              WHEN O.SEX = 'M' AND O.AGE BETWEEN 45 AND 54 THEN
               1
              ELSE
               0
           END AS AGE_GROUP45_54_M,
           CASE
              WHEN O.SEX = 'M' AND O.AGE BETWEEN 45 AND 59 THEN
               1
              ELSE
               0
           END AS AGE_GROUP55_59_M,
           CASE
              WHEN O.SEX = 'M' AND O.AGE BETWEEN 60 AND 64 THEN
               1
              ELSE
               0
           END AS AGE_GROUP60_64_M,
           CASE
              WHEN O.SEX = 'M' AND O.AGE BETWEEN 65 AND 69 THEN
               1
              ELSE
               0
           END AS AGE_GROUP65_69_M,
           CASE
              WHEN O.SEX = 'M' AND O.AGE BETWEEN 70 AND 74 THEN
               1
              ELSE
               0
           END AS AGE_GROUP70_74_M,
           CASE
              WHEN O.SEX = 'M' AND O.AGE BETWEEN 75 AND 79 THEN
               1
              ELSE
               0
           END AS AGE_GROUP75_79_M,
           CASE
              WHEN O.SEX = 'M' AND O.AGE BETWEEN 80 AND 84 THEN
               1
              ELSE
               0
           END AS AGE_GROUP80_84_M,
           CASE
              WHEN O.SEX = 'M' AND O.AGE BETWEEN 85 AND 89 THEN
               1
              ELSE
               0
           END AS AGE_GROUP85_89_M,
           CASE
              WHEN O.SEX = 'M' AND O.AGE BETWEEN 90 AND 94 THEN
               1
              ELSE
               0
           END AS AGE_GROUP90_94_M,
           CASE
              WHEN O.SEX = 'M' AND O.AGE BETWEEN 95 AND 00 THEN
               1
              ELSE
               0
           END AS AGE_GROUP95_GT_M,
           O.YEAR,
          CASE WHEN EXISTS (SELECT m1.hcc_model_category1 FROM HCC_MODEL_DIAGNOSIS M1 , HCC_MODEL_DIAGNOSIS_MAPPING HM
                            ,mor222 m2 WHERE m1.map_id = hm.map_id_ref
                            AND m2.YEAR = hm.YEAR
                            AND o.cn = m2.cn
                            AND o.YEAR = hm.YEAR
                            AND m1.hcc_model_category1 = 1
                            AND m2.diag_code = m1.icd9cmscode) THEN 1 ELSE 0 END AS DISEASE_COEF_HCC1,
          CASE WHEN EXISTS (SELECT m1.hcc_model_category1 FROM HCC_MODEL_DIAGNOSIS M1 , HCC_MODEL_DIAGNOSIS_MAPPING HM
                            ,mor222 m2 WHERE m1.map_id = hm.map_id_ref
                            AND m2.YEAR = hm.YEAR
                            AND o.cn = m2.cn
                            AND o.YEAR = hm.YEAR
                            AND m1.hcc_model_category1 = 2
                            AND m2.diag_code = m1.icd9cmscode) THEN 1 ELSE 0 END AS DISEASE_COEF_HCC2,
           CASE WHEN EXISTS (SELECT m1.hcc_model_category1 FROM HCC_MODEL_DIAGNOSIS M1 , HCC_MODEL_DIAGNOSIS_MAPPING HM
                            ,mor222 m2 WHERE m1.map_id = hm.map_id_ref
                            AND m2.YEAR = hm.YEAR
                            AND o.cn = m2.cn
                            AND o.YEAR = hm.YEAR
                            AND m1.hcc_model_category1 = 5
                             AND m2.diag_code = m1.icd9cmscode) THEN 1 ELSE 0 END AS       DISEASE_COEF_HCC5,
            CASE WHEN EXISTS (SELECT m1.hcc_model_category1 FROM HCC_MODEL_DIAGNOSIS M1 , HCC_MODEL_DIAGNOSIS_MAPPING HM
                            ,mor222 m2 WHERE m1.map_id = hm.map_id_ref
                            AND m2.YEAR = hm.YEAR
                            AND o.cn = m2.cn
                            AND o.YEAR = hm.YEAR
                            AND m1.hcc_model_category1 = 7
                            AND m2.diag_code = m1.icd9cmscode) THEN 1 ELSE 0 END AS      DISEASE_COEF_HCC7,
           CASE WHEN EXISTS (SELECT m1.hcc_model_category1 FROM HCC_MODEL_DIAGNOSIS M1 , HCC_MODEL_DIAGNOSIS_MAPPING HM
                            ,mor222 m2 WHERE m1.map_id = hm.map_id_ref
                            AND m2.YEAR = hm.YEAR
                            AND o.cn = m2.cn
                            AND o.YEAR = hm.YEAR
                            AND m1.hcc_model_category1 = 8
                            AND m2.diag_code = m1.icd9cmscode) THEN 1 ELSE 0 END AS       DISEASE_COEF_HCC8                   
      FROM (SELECT T.cn,
                   T.BEN_LAST_NAME,
                   T.BEN_FIRST_NAME,
                   T.BEN_MI,
                   T.SSN,
                   TO_DATE(T.DOB, 'YYYYMMDD') AS DOB,
                   T.SEX,
                   TRUNC(MONTHS_BETWEEN(TO_DATE('01/01/' || T.YEAR, 'MM/DD/YYYY'),
                                        TO_DATE(T.DOB, 'YYYYMMDD')) / 12) AS AGE,
                   T.YEAR
              FROM mor111 T ) O Thanks in advance!
    Kind regards,
    Nik

    Ok, so I am going to be a little bit kinder then your first reply, but not much.
    From what can tell you are doing medical management and are trying to select by AGE/GENDER CELLS and this is common when trying to deal with RBRVS.
    This is by far some of the worst sql I have ever seen, sorry but it just is.
    So please report this either as a new post or as a reply and:
    1. At least post the table definitions and if you can some sample data.
    2. Get rid of almost all the case selectors.
    3. Describe the output you need.
    As it is this is unreadable.
    After that we can talk more.
    Edited by: FlyingGuy on Oct 11, 2011 5:13 PM

Maybe you are looking for