Need help tuninng a dynamic query

Hello

We have following question

Select s.* s bose_sites.registration_crm_serial_number

R2 bose_sites.registration_crm_serial_number left on join

s.serialnr = r2.shipserialnr

where s.shipserialnr is null

and (r2.serialnr = ' 052378332000138AE' or (s.serialnr =)

(( "052378332000138AE" and s.shipserialnr is null))

which takes about 40 seconds to execute with the values highlighted in bold being dynamic values

If anyone can help reduce the run time on this request.

There are indexes created on table

INDEX_NAME INDEX_TYPE

------------------------------ ---------------------------

IDX_REG_CRM_SER_NUM_CPC NORMAL

IDX_REG_CRM_SER_NUM_SHIPDATE NORMAL

IDX_REG_CRM_SER_NUM_PARENT_ID NORMAL

IDX_REG_CRM_SER_NUM_ID NORMAL

is it possible that we can capture a dynamic query using DBMS_SQLTUNE

all suggestions will be useful.

Hello

You have one table here

-------------------------------------------------------------------------------

Schema name: SYS

SQL ID: cf2875zz4q4nd

SQL text: select s.* bose_sites.registration_crm_serial_number s

R2 bose_sites.registration_crm_serial_number left on join

s.serialnr = r2.shipserialnr

where s.shipserialnr is null

and (r2.serialnr = '052378332000138AE' or (s.serialnr =

S.shipserialnr and "052378332000138AE" is null))

-------------------------------------------------------------------------------

then why don't you go for a simple query

HTH

Tags: Database

Similar Questions

  • Need help with a SQL query

    Hello

    I have a data in table (raj_table) with columns (char11) raj_id, raj_number (varchar2 (15)), raj_format (NUMBER), Primary_ID (identity with the values of the primary key column)

    Primary_ID raj_id Raj_number Raj_format

    1                            raj                 rajvend                      1

    2                            raj                 rajvend                      1

    3                            raj                 rajvendor1                 2

    4                            raj                 rajvendor1                 2

    5                            raj                 rajvendor1                 2

    6                            raj                 rajvendor2                 3

    I used under SQL to get query output as below, but has not achieved the required result:

    Select client_id vendor_number, vendor_format, primary_id, row_number() on sl_no (client_id partition, primary_id, vendor_format order of client_id primary_id, vendor_format, vendor_number, vendor_number)

    from raj_table by sl_no asc

    SL_NO raj_id raj_number raj_format primary_id

    1                   1                   raj              rajvendor                 1

    1                   2                  raj              rajvendor                 1

    2                   3                   raj              rajvendor1                2

    2                   4                   raj              rajvendor1                2

    2                   5                  raj               rajvendor1                2

    3                   6                    raj              rajvendor2                3

    I need help with a SQL query to get the result as above without using the group by clause. I want to bring together the combination of separate line of the three columns (raj_id, raj_number, raj_format) and add a unique serial number for each online game (SL_NO column below). So, above there are 3 unique set of (raj_id, raj_number, raj_format) I can get in a group by clause, but I can not add prmiary_id, SL_NO values if I group by clause. I used the analytical functions like row_number() but no luck. Need solution for this.

    with t as)

    Select 'raj' raj_id, 'rajvend' raj_number, 1 raj_format, 1 primary_id Union double all the

    Select option 2, 'raj', 'rajvend', 1 double Union all

    Select 3, 'raj', 'rajvendor1', 2 double Union all

    Select 4, 'raj', 'rajvendor1', 2 double Union all

    Select 5, 'raj', 'rajvendor1', 2 double Union all

    Select 6, 'raj', 'rajvendor2', 3 double

    )

    Select dense_rank() over (order of raj_id, raj_number, raj_format) sl_no,

    t.*

    t

    order by primary_id

    /

    PRIMARY_ID RAJ RAJ_NUMBER RAJ_FORMAT SL_NO
    ---------- ---------- --- ---------- ----------
    1 1 raj rajvend 1
    1 2 raj rajvend 1
    2 3 raj rajvendor1 2
    2 4 raj rajvendor1 2
    2 5 raj rajvendor1 2
    3 6 raj rajvendor2 3

    6 selected lines.

    SQL >

    SY.

  • Need help with writing a query

    Hi all

    Can someone help please write a query for the following scenario

    Data in the table are in the format below

    student_nameSub1sub1_marksSub2sub2_marksSUB3sub3_marks
    JohnMath90Science80lang85

    Need to write a query to get it as

    student_nameObjectbrands of
    JohnMath90
    JohnScience80
    Johnlang85


    Thank you

    I'm looking so unpivoting the mulitple columns option is there. how it will be used

    I certainly learned something new today.

    create table student_marks(
      student_name varchar2(20),
      sub1 varchar2(20),
      sub1_marks number,
      sub2 varchar2(20),
      sub2_marks number,
      sub3 varchar2(20),
      sub3_marks number
    )
    ;
    insert into student_marks values(
      'john',
      'math',
      90,
      'science',
      80,
      'lang',
      85
    )
    ;
    select
      student_name, sub subject, marks
    from student_marks
    unpivot include nulls (
      (sub, marks)
      for subject in (
        (sub1, sub1_marks),
        (sub2, sub2_marks),
        (sub3, sub3_marks)
        )
    )
    ;
    drop table student_marks purge
    ;
    
    table STUDENT_MARKS created.
    1 rows inserted.
    STUDENT_NAME         SUBJECT                   MARKS
    -------------------- -------------------- ----------
    john                 math                         90
    john                 science                      80
    john                 lang                         85 
    
    table STUDENT_MARKS dropped.
    
  • need help for a conditional query

    guys this is just. as an extension of this post that Frank was helping me. IM reposting because my needs change slightly and im having a hell of a time trying to change the query.
    Here are the previous post.
    need help with query can find data back please help.
    CREATE TABLE "FGL"
      (
        "FGL_GRNT_CODE" VARCHAR2(60),
        "FGL_FUND_CODE" VARCHAR2(60),
        "FGL_ACCT_CODE" VARCHAR2(60),
        "FGL_ORGN_CODE" VARCHAR2(60),
        "FGL_PROG_CODE" VARCHAR2(60),
        "FGL_GRNT_YEAR" VARCHAR2(60),
        "FGL_PERIOD"    VARCHAR2(60),
        "FGL_BUDGET"    VARCHAR2(60)
      )
    data
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','11','00','400');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','1','100');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','1','0');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','11','1','400');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('360055','360055','7200','4730','02','10','1','400');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('360055','360055','7600','4730','02','10','1','400');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','14','200');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','10','14','100');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','14','200');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','2','100');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','11','2','600');
     
     
    I need to find the year of greater subsidy for the granting by a period setting.
    Once I found the great year, I need to check the value of the period 14 this grant for the previous year and add it to the amount of the budget for this grant. However if there is an entry in the biggest year for period 00 so I need to ignore the period 14 of the previous year and the current calculation period + (current period - more great year 00)

    hope that makes sense, so in other words, with the new data above. If I asking me a second period of the grant year 11. I'd end up with $800

    because the greatest year is 11 it contains one point 0 with $ 400, so my total should be
    amount of period $2,600
    period $0 400 - period $2 600 = $200

    600 + 200 = $800

    If I have question period 1 grant 360055 I'd end up just with 800 grnt year 10.

    I tried to edit this query you have provided me with no luck. I tried for several embarrassed day but I'm saying I can just do what im trying to do.
    can you please help me.


    Here's the query provided by frank kulash who graciously put it together for me.
    WITH     got_greatest_year     AS
    (
         SELECT     fgl.*     -- or whatever columns are needed
         ,     MAX ( CASE 
                     WHEN  fgl_period = :given_period 
                     THEN  fgl_grnt_year
                    END
                  ) OVER ()     AS greatest_year
         FROM     fgl
    )
    SELECT     SUM (fgl_budget)     AS total_budget     -- or SELECT *
    FROM     got_greatest_year
    WHERE     (     fgl_grnt_year     = greatest_year
         AND     fgl_period     = :given_period
         )
    OR     (     fgl_grnt_year     = greatest_year - 1
         AND     fgl_period     = 14
         )
    ;
    Miguel

    Hi, Miguel.

    You are waying that when larger year who has: given_period also a period = '00' (or '0', or whatever it is you want to use), then you want to double the budget of the given_period (subtract the '00' budget and do not count '14' year pevious)? If so, add another condition to the CASE statement that decides what you've summarized:

    WITH     got_greatest_year     AS
    (
         SELECT       TO_NUMBER (fgl_grnt_year)     AS grnt_year
         ,       fgl_period
         ,       TO_NUMBER (fgl_budget)     AS budget
         ,       MAX ( CASE
                       WHEN  fgl_period = :given_period
                       THEN  TO_NUMBER (fgl_grnt_year)
                      END
                    ) OVER ()     AS greatest_year
         FROM       fgl
    )
    ,     got_cnt_00     AS
    (
         SELECT     grnt_year
         ,     fgl_period
         ,     budget
         ,     greatest_year
         ,     COUNT ( CASE
                       WHEN  grnt_year     = greatest_year
                       AND       fgl_period     = '00'
                       THEN  1
                         END
                    ) OVER ()          AS cnt_00
         FROM    got_greatest_year
    )
    SELECT       SUM ( CASE
                        WHEN  grnt_year     = greatest_year                    -- New
                  AND       fgl_period     = :given_period                    -- New
                  AND       cnt_00     > 0            THEN  budget * 2     -- New
                        WHEN  grnt_year     = greatest_year
                  AND       fgl_period     = :given_period       THEN  budget
                        WHEN  grnt_year     = greatest_year
                  AND       fgl_period     = '00'            THEN -budget
                        WHEN  grnt_year     = greatest_year - 1
                  AND       fgl_period     = '14'
                  AND       cnt_00     = 0            THEN  budget
                    END
               )          AS total_budget
    FROM       got_cnt_00
    ;
    

    You will notice it is the same as the previous query, I posted, with the exception of 3 lines marked 'new '.

  • Need help with PL/SQL query complex

    I need help with a query that need access to data from 3 tables. That's what I did

    I created 3 tables

    CREATE TABLE post_table
    (
    post_id varchar (20),
    datepost DATE,
    KEY (post_id) elementary SCHOOL
    ) ;

    CREATE TABLE topic
    (
    TOPIC_ID varchar (20),
    name varchar (20),
    PRIMARY KEY (topic_id)
    );

    CREATE TABLE blogpost_table
    (
    TOPIC_ID varchar (20),
    post_id varchar (20),
    PRIMARY KEY (topic_id, post_id);
    FOREIGN KEY (topic_id) REFERENCES topic (topic_id) ON DELETE CASCADE,
    FOREIGN KEY (post_id) REFERENCES post_table (post_id) ON DELETE CASCADE
    );


    Now, I inserted a few values in these tables as

    INSERT INTO post_table VALUES ('p1', to_date ('2009-09-14 18:00 "," MM/DD/YYYY mi:ss'));))
    INSERT INTO post_table VALUES ('p2', to_date ('2009-07-18 18:00 "," MM/DD/YYYY mi:ss'));))
    INSERT INTO post_table VALUES ('p3', to_date ('2009-07-11 18:00 "," MM/DD/YYYY mi:ss'));))
    INSERT INTO post_table VALUES ('p4', to_date ('2009-03-11 18:00 "," MM/DD/YYYY mi:ss'));))
    INSERT INTO post_table VALUES ('p5', to_date ('2009-07-13 18:00 "," MM/DD/YYYY mi:ss'));))
    INSERT INTO post_table VALUES ('p6', to_date ('2009-06-12 18:00 "," MM/DD/YYYY mi:ss'));))
    INSERT INTO post_table VALUES ('p7', to_date ('2009-07-11 18:00 "," MM/DD/YYYY mi:ss'));))

    INSERT INTO VALUES subject ("t1", "baseball");
    INSERT INTO category VALUES ('t2', 'football');

    INSERT INTO blogpost_table VALUES ("t1", "p1");
    INSERT INTO blogpost_table VALUES ('t1', 'p3');
    INSERT INTO blogpost_table VALUES ("t1", "p4");
    INSERT INTO blogpost_table VALUES ('t1', 'p5');
    INSERT INTO blogpost_table VALUES ('t2', 'p2');
    INSERT INTO blogpost_table VALUES ('t2', 'p6');
    INSERT INTO blogpost_table VALUES ("t2", "p7");


    I'm launching SQL queries on the table in this topic.

    I want to write a SQL query that returns me the name of a topic (s) and the number of blog_post (s) associated with the topic in descending order of the number of blog posts created in July.

    Can someone please help me to write this query?

    Thank you

    Published by: user11994430 on October 9, 2009 07:24

    Thanks for the test of the configuration!

    SQL>SELECT   t.NAME, COUNT(*)
      2      FROM topic t, blogpost_table b, post_table p
      3     WHERE b.topic_id = t.topic_id
      4       AND p.post_id = b.post_id
      5       AND p.datepost >= DATE '2009-07-01'
      6       AND p.datepost < DATE '2009-08-01'
      7  GROUP BY t.NAME
      8  ORDER BY COUNT(*) desc;
    
    NAME                   COUNT(*)
    -------------------- ----------
    baseball                      2
    soccer                        2
    

    HTH, Urs

  • Need help to create dynamic VO

    Hi experts,
    Need code to create dynamic VO

    --
    MD

    Hi MD,.
    use following code

    String sql1 = "SELECT col1, col2, col3 from table1 where ';"
    SQL1 = sql1 + "col1 =: 1";
    ViewObject vo = am.findViewObject (">");
    If (vo is nothing)
    VO = am.createViewObjectFromQueryStmt (">", sql1 ' ");
    vo.setWhereClauseParams (null);
    vo.setWhereClauseParam (0, >);
    vo.executeQuery ();

    --
    Thank you
    Shrikant

  • Need help to resolve the query by using analytic functions

    Hello

    I need help to solve this problem, I tried an analytical function but could not solve the problem.

    I have three table as illustrated below the table is filled with a flat file. The records are arranged sequentailly based on the name of the file.

    The first record of the game based on EIN goes to TAB_RCE
    the following records then goes to TAB_RCW
    and last save of the game based on EIN goes to the RCT table

    How can I make groups and
    assign a

    EIN * 12345 * line number * 02, 03, 04 * in the table TAB_RCW and * 05 * in the table TAB_RCT
    EIN * 67890 * line number * 07, 08, 09,10 * in the table TAB_RCW and * 11 * in the table TAB_RCT
    and so on...

    Thank you

    Rajesh

    TAB RCE_--------------------------------------------------------------
    LineNumber EIN FILENAME TYPE

    -----

    01 12345 ABC NCE. TXT
    06 67890 ABC NCE. TXT
    12 76777 ABC NCE. TXT

    -----
    TAB_RCW
    -----
    LineNumber TYPE SSN FILENAME
    -----
    02 22222 ABC RCW. TXT
    03 33333 ABC RCW. TXT
    04 44444 ABC RCW. TXT
    07 55555 ABC RCW. TXT
    08 66666 ABC RCW. TXT
    09 77777 ABC RCW. TXT
    10 88888 ABC RCW. TXT
    13 99998 ABC RCW. TXT
    14 99999 ABC RCW. TXT

    -----
    TAB_RCT
    -----
    NAME OF THE FILE OF TYPE LINENUMBER
    -----
    RCT 05 ABC. TXT
    RCT 11 ABC. TXT
    RCT 15 ABC. TXT
    -----
    SQL> with TAB_RCE as (
      2                   select 'RCE' rtype,'01' linenumber, '12345' EIN,'ABC.TXT' FILENAME from dual union all
      3                   select 'RCE','06','67890','ABC.TXT' from dual union all
      4                   select 'RCE','12','76777','ABC.TXT' from dual
      5                  ),
      6       TAB_RCW as (
      7                   select 'RCW' rtype,'02' linenumber,'22222' ssn,'ABC.TXT' FILENAME from dual union all
      8                   select 'RCW','03','33333','ABC.TXT' from dual union all
      9                   select 'RCW','04','44444','ABC.TXT' from dual union all
     10                   select 'RCW','07','55555','ABC.TXT' from dual union all
     11                   select 'RCW','08','66666','ABC.TXT' from dual union all
     12                   select 'RCW','09','77777','ABC.TXT' from dual union all
     13                   select 'RCW','10','88888','ABC.TXT' from dual union all
     14                   select 'RCW','13','99998','ABC.TXT' from dual union all
     15                   select 'RCW','14','99999','ABC.TXT' from dual
     16                  ),
     17       TAB_RCT as (
     18                   select 'RCT' rtype,'05' linenumber,'ABC.TXT' FILENAME from dual union all
     19                   select 'RCT','11','ABC.TXT' from dual union all
     20                   select 'RCT','15','ABC.TXT' from dual
     21                  )
     22  select  rtype,
     23          last_value(ein ignore nulls) over(partition by filename order by linenumber) ein,
     24          linenumber,
     25          ssn
     26    from  (
     27            select  rtype,
     28                    linenumber,
     29                    ein,
     30                    to_char(null) ssn,
     31                    filename
     32              from  TAB_RCE
     33            union all
     34            select  rtype,
     35                    linenumber,
     36                    to_char(null) ein,
     37                    ssn,
     38                    filename
     39              from  TAB_RCW
     40            union all
     41            select  rtype,
     42                    linenumber,
     43                    to_char(null) ein,
     44                    to_char(null) ssn,
     45                    filename
     46              from  TAB_RCt
     47          )
     48    order by linenumber
     49  /
    
    RTY EIN   LI SSN
    --- ----- -- -----
    RCE 12345 01
    RCW 12345 02 22222
    RCW 12345 03 33333
    RCW 12345 04 44444
    RCT 12345 05
    RCE 67890 06
    RCW 67890 07 55555
    RCW 67890 08 66666
    RCW 67890 09 77777
    RCW 67890 10 88888
    RCT 67890 11
    
    RTY EIN   LI SSN
    --- ----- -- -----
    RCE 76777 12
    RCW 76777 13 99998
    RCW 76777 14 99999
    RCT 76777 15
    
    15 rows selected.
    
    SQL> 
    

    SY.

  • Need help to build the query/pl-sql block to get the query result and the name of column from DB table in the form of key-value pairs.

    Hi Experts,

    I have a DB table has columns of more than 50.

    I question this table, it should only return one line at any time. as sqldeveloper below image.

    here, I need to build block pl/sql-query, Discover the column in the table as a key and query result as values.

    Eg:     Key                         -  Value

    TASK_EVENT_ID - 1765

    EVENT_TYPE - ASR_UPDATE

    ... etc until all of the columns in my table.

    Experts please comment on that point, appreciate your help on this.

    Thank you

    -Vincent.

    Here is an approach using DBMS_SQL to iterate over the columns of key / value to assign... (Little code snipped for brevity)

    create or replace procedure (task_expired)

    v_store_id in full,

    v_task_action_id in full,

    v_job_id in full

    )

    as

    -[SNIP code...]

    v_sql VARCHAR2 (4000): = ' select * from my_table where PK = 123';  -Your SQL here!

    v_v_val VARCHAR2 (4000);

    v_n_val NUMBER;

    v_d_val DATE;

    v_ret NUMBER;

    c NUMBER;

    d NUMBER;

    col_cnt INTEGER.

    f BOOLEAN;

    rec_tab DBMS_SQL. DESC_TAB;

    col_num NUMBER;

    vAsString VARCHAR2 (4000);

    BEGIN

    -[SNIP code...]

    Message_properties. CORRELATION: = "EDF_EVENT";

    MSG: = SYS. AQ$ _JMS_BYTES_MESSAGE. Construct();

    Msg.set_string_property ('queueName', ' shipping/csi_cth');

    Msg.set_string_property ('MODE', 'CR8');

    c: = DBMS_SQL. OPEN_CURSOR;

    DBMS_SQL. PARSE (c, v_sql, DBMS_SQL. NATIVE);

    d: = DBMS_SQL. Execute (c);

    DBMS_SQL. DESCRIBE_COLUMNS (c, col_cnt, rec_tab);

    1.col_cnt J

    LOOP

    CASE rec_tab (j) .col_type

    WHEN 2 THEN

    DBMS_SQL. DEFINE_COLUMN (c, j, v_n_val);      -Number

    WHEN 12 CAN

    DBMS_SQL. DEFINE_COLUMN (c, j, v_d_val);      -Date

    ON THE OTHER

    DBMS_SQL. DEFINE_COLUMN (c, j, v_v_val, 2000);   -Else treat as varchar2

    END CASE;

    END LOOP;

    LOOP

    v_ret: = DBMS_SQL. FETCH_ROWS (c);

    WHEN OUTPUT v_ret = 0;

    1.col_cnt J

    LOOP

    -Fetch each column to the correct data type based on coltype

    CASE rec_tab (j) .col_type

    WHEN 2 THEN

    DBMS_SQL. COLUMN_VALUE (c, j, v_n_val);

    vAsString: = to_char (v_n_val);

    WHEN 12 CAN

    DBMS_SQL. COLUMN_VALUE (c, j, v_d_val);

    vAsString: = to_char (v_d_val, ' DD/MM/YYYY HH24:MI:SS');

    ON THE OTHER

    DBMS_SQL. COLUMN_VALUE (c, j, v_v_val);

    vAsString: = v_v_val;

    END CASE;

    Msg.set_string_property (rec_tab (j) .col_name, vAsString);

    END LOOP;

    END LOOP;

    DBMS_SQL. CLOSE_CURSOR (c);

    DBMS_AQ. ENQUEUE (queue_name-online 'cbus.aqjms_common',

    Enqueue_options => Enqueue_options,

    Message_properties => Message_properties,

    Payload-online msg,

    Msgid => Message_handle);

    dbms_output.put_line ('00 Msgid =' |) Message_handle);

    dbms_output.put_line('===Done=');

    -[SNIP code...]

    END;

    /

  • Need help to build the query to group the lines

    Hello all, hope someone can help me with this. I am working in Oracle 11 g PL/SQL 11.2.0.2.0.

    I have the following query, which does exactly what I need, but I have been invited to consolidate the results in a specific way and I can't do that. First of all the existing request:
    SELECT facility_id
    ,facility_type
    ,COUNT (CASE WHEN component_type = 'ATTACH2NDPARTY' THEN 1 END) AS ATTACH2NDPARTY
    ,COUNT (CASE WHEN component_type = 'ATTACH3RDPARTY' THEN 1 END) AS ATTACH3RDPARTY
    ,COUNT (CASE WHEN component_type = 'BREAKERGT' THEN 1 END) AS BREAKERGT
    ,COUNT (CASE WHEN component_type = 'BREAKERUSS' THEN 1 END) AS BREAKERUSS
    ,COUNT (CASE WHEN component_type = 'CAPACIFORMER' THEN 1 END) AS CAPACIFORMER
    ,COUNT (CASE WHEN component_type = 'CAPACITOR' THEN 1 END) AS CAPACITOR
    ,COUNT (CASE WHEN component_type = 'CEFIBEROPTIC' THEN 1 END) AS CEFIBEROPTIC
    ,COUNT (CASE WHEN component_type = 'ELASTIDISCONN' THEN 1 END) AS ELASTIDISCONN
    ,COUNT (CASE WHEN component_type = 'NETWORKPROTECT' THEN 1 END) AS NETWORKPROTECT
    ,COUNT (CASE WHEN component_type = 'POLE' THEN 1 END) AS POLE
    ,COUNT (CASE WHEN component_type = 'REACTOR' THEN 1 END) AS REACTOR
    ,COUNT (CASE WHEN component_type = 'RECLOSER3PUPOIL' THEN 1 END) AS RECLOSER3PUPOIL
    ,COUNT (CASE WHEN component_type = 'RECLOSERTSUP' THEN 1 END) AS RECLOSERTSUP
    ,COUNT (CASE WHEN component_type = 'RECLSR3PCNTRL3A' THEN 1 END) AS RECLSR3PCNTRL3A
    ,COUNT (CASE WHEN component_type = 'RECLSR3PCNTRLNT' THEN 1 END) AS RECLSR3PCNTRLNT
    ,COUNT (CASE WHEN component_type = 'RECLSRTSCNTRL' THEN 1 END) AS RECLSRTSCNTRL
    ,COUNT (CASE WHEN component_type = 'RECTIFIERUSS' THEN 1 END) AS RECTIFIERUSS
    ,COUNT (CASE WHEN component_type = 'REGULATORJOINT' THEN 1 END) AS REGULATORJOINT
    ,COUNT (CASE WHEN component_type = 'REGULATOROH' THEN 1 END) AS REGULATOROH
    ,COUNT (CASE WHEN component_type = 'REGULATORUSS' THEN 1 END) AS REGULATORUSS
    ,COUNT (CASE WHEN component_type = 'RMS' THEN 1 END) AS RMS
    ,COUNT (CASE WHEN component_type = 'SPLICEPRIMARY' THEN 1 END) AS SPLICEPRIMARY
    ,COUNT (CASE WHEN component_type = 'STREETLIGHT' THEN 1 END) AS STREETLIGHT
    ,COUNT (CASE WHEN component_type = 'SWITCHSCADA' THEN 1 END) AS SWITCHSCADA
    ,COUNT (CASE WHEN component_type = 'TAPCHANGER' THEN 1 END) AS TAPCHANGER
    ,COUNT (CASE WHEN component_type = 'TRAFFICSIGNAL' THEN 1 END) AS TRAFFICSIGNAL
    ,COUNT (CASE WHEN component_type = 'TRANSF480' THEN 1 END) AS TRANSF480
    ,COUNT (CASE WHEN component_type = 'TRANSFGROUND' THEN 1 END) AS TRANSFGROUND
    ,COUNT (CASE WHEN component_type = 'TRANSFISOLATION' THEN 1 END) AS TRANSFISOLATION
    ,COUNT (CASE WHEN component_type = 'TRANSFOH' THEN 1 END) AS TRANSFOH
    ,COUNT (CASE WHEN component_type = 'TRANSFUG' THEN 1 END) AS TRANSFUG
    ,COUNT (CASE WHEN component_type = 'TRANSFURD' THEN 1 END) AS TRANSFURD
    ,COUNT (CASE WHEN component_type = 'TRANSFUSS' THEN 1 END) AS TRANSFUSS
    FROM (SELECT fy.id AS facility_id
          ,fy.external_facility_id
          ,fy.flty_facility_type as facility_type
          ,cp.cpty_component_type as component_type
          FROM facilities fy
          LEFT OUTER JOIN components cp
          ON cp.facility_id= fy.id
          ORDER BY fy.id DESC)
    GROUP BY facility_id, facility_type
    I was asked to do, is to display a number of facility_type who have each of the combinations of the 'DEAL' 33 areas. Installation types are strings. So an expected result would look something like:

    FACILITY_TYPE | ATTACH2NDPARTY | ATTACH3RDPARTY | BREAKERGT | ... TRANSFUSS | COUNTY
    MANHOLE X X 10
    MANHOLE X X 22
    POLE X X 5

    and so on. Because there are 33 BUSINESS areas and therefore 33! possible combinations, I was thinking about a sort of loop would achieve the desired result, but I'm stuck and could really use a shot in the arm. Any help will be appreciated and obtained points! Thanks in advance.
    select
     facility_type
    ,ATTACH2NDPARTY
    ,ATTACH3NDPARTY
    -- and so on
    , count(*) cnt
    from (
    --your query
    SELECT facility_id
    ,facility_type
    ,COUNT (CASE WHEN component_type = 'ATTACH2NDPARTY' THEN 1 END) AS ATTACH2NDPARTY
    ,COUNT (CASE WHEN component_type = 'ATTACH3RDPARTY' THEN 1 END) AS ATTACH3RDPARTY
    ,COUNT (CASE WHEN component_type = 'BREAKERGT' THEN 1 END) AS BREAKERGT
    ...
    )
    group by
     facility_type
    ,ATTACH2NDPARTY
    ,ATTACH3NDPARTY
    -- and so on like in projection
    
  • Need help to highlight the query text in the document

    Hi, I'm trying to load files into the blob column and try to create the text index.

    I need to query the blob column in the original glass by a string, which should return the relevant documents with the query string, highlighted with some color.

    Can you please help me with an example of 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
    --------------------------------------------------------------------------------
    It is a document test of demonstration to highlight.

    1 selected line.

    Scott@orcl_11gR2 >

  • Need help with an update query

    Hello

    I am trying to run a query on a table update. Here's an example: I have 2 tables t1 and t2, and these tables have 2 similar columns, c11 and c12 in t1 and c21 and c22 in t2. I now have to execute an update statement for the column c11 in t1 with the values for the c21 in t2 where the c12 in t1 is equal to c22 in t2. Therefore, the request that I made:

    Update t1 set c11 =.
    (select t2.c21 from t1, t2 where t1.c11 = t2.c22)
    where t1.c11 in (select t1.c11 from t1);

    But this query gives me an error: ORA-01427: einreihig subquery returns more than one line.

    Where I'm going wrong? Kindly help.

    Hello

    Try

    UPDATE t1
       SET c11 = (SELECT t2.c21
                    FROM t2
                   WHERE t1.c12 = t2.c22)
     WHERE EXISTS (SELECT 1
                     FROM t2
                    WHERE t1.c12 = t2.c22);
    

    When you say

    c12 in t1 is equal to c22 in t2
    

    your where clause should be like

    WHERE t1.c12 = t2.c22
    

    and you do not need to join the table updated in the subquery that you can refer directly to its columns.

    from t1,t2--not needed.
    Use  FROM t2  
    

    * 009 *.

    Published by: 009 on March 18, 2010 21:04

  • [Oracle 8i] Need help of a hierarchical query pruning branches

    My problem is that my hierarchical query seems only to cut values that do not meet my criteria, but still includes their children. When my query hits a record that does not meet my criteria, I want it to stop there. I tried including the criteria in just the ' ' a where clause of the query and also put the criteria in clause "connect by" as well, but nothing has been set. Will you please keep in mind that I'm using Oracle 8i, so I can't use some of the "nice" statements for hierarchical queries that they introduced in 9. I'm stuck with "Start With... Connect in "."

    I have examples of tables/data I can post if someone needs to see to help me, but to start with, here my current query:
    SELECT     *
    FROM     (
              SELECT 
                   LEVEL
              ,     c_bill.comp_part_nbr                     AS     c_part_nbr
              ,     (select c_part.part_desc 
                   FROM part c_part 
                   WHERE c_part.part_nbr=c_bill.comp_part_nbr)     AS     c_part_desc
              ,     (SELECT c_part.part_type 
                   FROM part c_part 
                   WHERE c_part.part_nbr=c_bill.comp_part_nbr)      AS     c_part_type
              ,     c_bill.qty_per                          AS     c_qty_per_p
              ,     c_bill.qty_per_type                     AS     c_qty_per_type
              ,     (SELECT c_part.qty_on_hand                
                   FROM part c_part 
                   WHERE c_part.part_nbr=c_bill.comp_part_nbr)      AS     c_qty_on_hand
              ,     c_bill.oper_nbr                     AS     rqd_at_op
              ,     c_bill.comp_off_adj                     AS     rqd_offset
              ,     c_bill.bom_doc_nbr                     AS     p_part_nbr
              ,     (SELECT p_part.qty_on_hand 
                   FROM part p_part 
                   WHERE p_part.part_nbr=c_bill.bom_doc_nbr)      AS     p_qty_on_hand
              FROM 
                   BILL c_bill 
              WHERE 
                   (
                             (c_bill.status           =      'RL')           
                        AND     (c_bill.view_code     IN      ('M','G'))     
                        AND     (c_bill.end_eff_dt     >      SYSDATE)       
                        AND     (c_bill.begn_eff_dt     <=      SYSDATE)
                   ) 
              START WITH c_bill.bom_doc_nbr=RPAD(?,25)
              CONNECT BY PRIOR c_bill.comp_part_nbr=c_bill.bom_doc_nbr
              AND     c_bill.view_code     IN     ('M','G')     
              AND     c_bill.status          =     'RL'
              AND      c_bill.end_eff_dt     >     SYSDATE
              AND     c_bill.begn_eff_dt     <=     SYSDATE     
         ) a
    WHERE     c_part_type = 'M'

    Hello

    The criterion in the outer query

    c_part_type = 'M'
    

    applies only to the results; You can still get the descendants of these lines in the output. This seems to be the only thing that is not repeated in the CONNECT BY clause.

    If you don't want to repeat the criteria in the WHERE clause and the CONECT BY clause, you can apply them once in a view online.
    The following example works in Oracle 8.1:

    SELECT     LPAD ( ' '
              , 3 * (LEVEL - 1)
              ) || ename          AS iname
    ,     empno
    ,     mgr
    FROM     (     -- Begin in-line view of employees named C-Z
         SELECT     ename
         ,     empno
         ,     mgr
         FROM     scott.emp
         WHERE     ename     >= 'C'
         )     -- End in-line view of employees named C-Z
    START WITH     mgr    IS NULL
    CONNECT BY     mgr    = PRIOR     empno
    ;
    

    Not only is excluded BLAKE, but the descendants of JAMES BLAKE, MARTIN, etc. are excluded, too, without repeating the condition.

    Please, post CREATE TABLE and INSERT statements for some examples of data - just enough to show what is the problem - and the results desired from these data.

  • Need help to understand the query result

    Hi gurus

    I was reading one of the question here in this forum and its link is below:

    Query required for scenario

    I had some confusion related to this code and don't understand the logic of the out put, see query below:

    Query

    with sub_services as

    (

    Select su_seq 12323, 'HLR1' so_id, 1 seq Union double all the

    Select su_seq 12323, "HLR2' so_id, seq 2 Union double all the

    Select su_seq 12323, "A09" so_id, seq 3 of all the double union

    Select su_seq 12333, "MO1" so_id, seq 4 Union double all the

    Select su_seq 12333, "MO2' so_id, seq 5 Union double all the

    Select su_seq 12333, "A09" so_id, 6 seq in union double all the

    Select su_seq 12333, 'M0CR' so_id, seq 7 Union double all the

    Select su_seq 12999, "LOL1' so_id, seq 8 Union double all the

    Select su_seq 12999, "LOL2' so_id, seq 9 double

    )

    Select *.

    of sub_services b

    where exists (select 1 from sub_services

    where su_seq = b.su_seq

    and so_id = 'A09.

    )

    order by 2;

    The query result

    12323 A09 3

    12333 6 A09

    12323 HLR1 1

    12323 HLR2 2

    12333 M0CR 7

    12333 4 MO1

    12333 5 MO2

    According to my understanding, the above query should return records in red only because of her is below command

    It exists (select 1 from sub_services

    where su_seq = b.su_seq

    and so_id = 'A09.

    but don't know why he's back 7 files, can someone help me understand the result...

    It is query is functionally identical to the PL/SQL block, but much more effective.

    declare

    number of l_res;

    Start

    for line (select *)

    sub_services) loop

    Start

    Select 1 from l_res

    of sub_services

    where su_seq = row.su_seq and

    so_id = "A09" and

    rownum = 1;

    exception when

    NO_DATA_FOUND then

    null;

    end;

    end loop;

    end;

    Essentially every row in the outer query are tested against him exists query.  Given the correlation between two requests is based only on su_seq each line with a su_seq value returned by him is returned in the output.

    Another way to think he uses instead a join condition.  This query is equivalent to the query to exist

    Select the main

    of main sub_services

    Join select (separate su_seq

    of sub_services

    where so_id = "A09") cond

    We main.su_seq = cond.su_seq;

    John

  • Need help with the recursive query

    I have test table

    Create table test (key primary number col_pk,)

    number of fins,

    number of fo_fv

    number of jup_fv

    action varchar2 (10)

    );

    Insert into test values(1,1,1,1,'LOAD');

    Insert into test values(2,2,null,2,'ROLL');

    Insert into test values(3,3,null,3,'ROLL');

    Insert into test values(4,4,null,4,'ROLL');

    Insert into test values(5,5,null,5,'ROLL');

    Insert into test values (6, null, null, 5.1, 'FLATTEN');

    Insert into test values (7, null, null, 5.2, 'FLATTEN');

    Insert into test values(8,null,null,1,'SIGNOFF');

    Insert into test values (9, null, null, 1,001, 'SIGNOFF');

    Insert into test values(10,6,2,6,'LOAD');

    Insert into test values (11, null, null, 6.1, 'FLATTEN');

    Insert into test values(12,7,1,7,'RELOAD');

    Insert into test values(13,null,null,1,'SIGNOFF');

    Select * from test;

    I want the result of the query in the form below:

    COL_PK FINS FO_FV JUP_FV ACTION

    ---------- ---------- ---------- ---------- ----------

    1 1 1 1 LOAD

    --------------------------------------------------

    2 2 1 2 ROLL

    --------------------------------------------------

    3 3 1 3 ROLL

    --------------------------------------------------

    4 4 1 4 ROLL

    --------------------------------------------------

    ROLL 5 5 1 5

    --------------------------------------------------

    5.1 6 5 1 FLATTEN

    --------------------------------------------------

    7 5 1 5.2 FLATTEN

    --------------------------------------------------

    8 5 1 1 APPROVAL

    --------------------------------------------------

    9 5 1 APPROVAL 1,001

    --------------------------------------------------

    10 6 2 6 LOAD

    --------------------------------------------------

    11 6 2 6.1 FLATTEN

    --------------------------------------------------

    12 7 1 7 RELOAD

    --------------------------------------------------

    13 7 1 1 APPROVAL

    --------------------------------------------------

    Help, please

    Hello

    This sounds like a job for the analytical LAST_VALUE function:

    SELECT col_pk

    LAST_VALUE (fins IGNORE NULLS)

    COURSES (ORDER BY col_pk) AS fins2

    LAST_VALUE (fo_fv IGNORE NULLS)

    COURSES (ORDER BY col_pk) AS fo_fv2

    jup_fv

    action

    OF the test

    ORDER BY col_pk

    ;

    Output:

    COL_PK FINS2 FO_FV2 JUP_FV ACTION

    ---------- ---------- ---------- ---------- ----------

    1 1 1 1 LOAD

    2 2 1 2 ROLL

    3 3 1 3 ROLL

    4 4 1 4 ROLL

    ROLL 5 5 1 5

    5.1 6 5 1 FLATTEN

    7 5 1 5.2 FLATTEN

    8 5 1 1 APPROVAL

    9 5 1 APPROVAL 1,001

    10 6 2 6 LOAD

    11 6 2 6.1 FLATTEN

    12 7 1 7 RELOAD

    13 7 1 1 APPROVAL

  • Need help to write the query to extract the value of the previous row - Lag not help


    Hello

    I created follwing table.

    Create table test

    (number of fi,

    number of fo_fv

    number of jup_fv

    action varchar2 (10)

    );

    insert into TEST(1,1,1,'LOAD');

    Insert into TEST (2, NULL, 2, "ROLL");

    insert into TEST(3,,3,'ROLL');

    insert into TEST(4,,4,ROLL);

    insert into TEST (5,2,5,LOAD);

    I want the result of the query as below:

    FI FO_FV JUP_FV ACTION

    -------------------------------------------------------------------

    1          1                    1                    LOAD

    2          1                    2                    ROLL

    3          1                    3                    ROLL

    4          1                    4                    ROLL

    5          2                    5                    LOAD

    Help, please.

    Thank you!

    SQL > select fi
    2, max (fo_fv) on fo_fv (fi control)
    3, jup_fv
    4, action
    5 of the test;

    FI FO_FV JUP_FV ACTION
    ---------- ---------- ---------- ----------
    1 1 1 LOAD
    ROLL OF 2 1 2
    3 1 3 ROLL
    4 1 4 ROLL
    5 2 5 LOAD

    OR

    SQL > select *.
    2 test
    model 3
    Dimension 4 by (fi)
    5 measures (fo_fv, jup_fv, action)
    6 rules
    7   (
    8 fo_fv [any] = case when fo_fv [cv ()] is null
    9. can fo_fv [cv () - 1]
    10 fo_fv [cv () else]
    11 end
    (12);

    FI FO_FV JUP_FV ACTION
    ---------- ---------- ---------- ----------
    1 1 1 LOAD
    ROLL OF 2 1 2
    3 1 3 ROLL
    4 1 4 ROLL
    5 2 5 LOAD

Maybe you are looking for