[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.

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 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 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
    
  • Group By clause in oracle 10g need help

    Hello
    We have a requirement that get the AR details of aging at the customer level. I wrote the following query to retrieve the correct rows at the invoice level. But now I need calculate the sum of the amounts and I show you the invoice and customer level. Could you please help me how can I group by the client.

    Here's the query I used

    Select ps.org_id
    sobbed. SET_OF_BOOKS_ID
    sobbed. CHART_OF_ACCOUNTS_ID
    gcc. Company of SEGMENT1
    gcc. SEGMENT2 location
    gcc. Department of SEGMENT3
    gcc. SEGMENT4 account
    gcc. Future_1 SEGMENT5
    gcc. SEGMENT6 Future_2
    gcc. SEGMENT7 Future_3
    gcc. CONCATENATED_SEGMENTS gl_cc_concat_kff
    ps.trx_number
    ps.trx_date
    ps.due_date
    ps.invoice_currency_code
    sob.currency_code SOB_Currency_Code
    ps.class
    ps.amount_due_original
    , ps.amount_due_original * nvl (ps.exchange_rate, 1) acctd_amount_due_original
    ps.amount_due_remaining
    ps.acctd_amount_due_remaining
    ps.status
    ps.cust_trx_type_id
    ps.customer_site_use_id
    ps.customer_trx_id
    ps.cash_receipt_id
    ps.gl_date
    rctlda. CODE_COMBINATION_ID
    ps.customer_id
    nvl (ATCM. ATTRIBUTE5, ps. CUSTOMER_ID) End_Customer_Id
    rc.customer_number
    rc2. CUSTOMER_NUMBER Brand_Cust_no
    , round ((sysdate-ps.due_date))
    of gl_sets_of_books ob
    , hr_operating_units or
    ar_payment_schedules_all ps
    ra_customers rc
    ra_cust_trx_line_gl_dist_all rctlda
    gl_code_combinations_kfv gcc
    ra_customer_trx_all ATCM
    ra_customers rc2
    where sob.set_of_books_id = ou.set_of_books_id
    and ou.organization_id = ps.org_id
    and ps.status = 'OP '.
    and ps.org_id is not null
    and ps. CUSTOMER_ID = rc. CUSTOMER_ID
    and ps. CUSTOMER_TRX_ID = rctlda. CUSTOMER_TRX_ID
    and rctlda. ACCOUNT_CLASS = "REC".
    and rctlda.latest_rec_flag = 'Y '.
    and rctlda. CODE_COMBINATION_ID = gcc. CODE_COMBINATION_ID
    and ps. CUSTOMER_TRX_ID = ATCM. CUSTOMER_TRX_ID
    and gcc. CODE_COMBINATION_ID = 39446
    - and ps.trx_number = 1-15O0A8O'
    - and rc. CUSTOMER_NUMBER = 1-10PA5KX'
    and nvl (ATCM. ATTRIBUTE5, ps. CUSTOMER_ID) = rc2. CUSTOMER_ID

    Could someone help me how to recover the same columns with sum (ps.amount_due_original) for each client. I tried to use the group by clause, but it gives to new level of the invoice.
    But my req's for each customer the amount of the invoice must be added and it should give the total

    Thank you
    THERE

    If you need to have the amount of the invoice for each customer may also need to check the
    CUBE
    http://download.Oracle.com/docs/CD/B28359_01/server.111/b28286/statements_10002.htm#sthref9448
    and example here
    http://download.Oracle.com/docs/CD/B28359_01/server.111/b28286/statements_10002.htm#i2066443

    and ROLLUP
    http://download.Oracle.com/docs/CD/B28359_01/server.111/b28286/statements_10002.htm#sthref9445

    I couldn't keep up with all your SQL statement, or I could rewrite for you once again
    Thank you

    Published by: user9532576 on July 21, 2009 09:24

  • 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 in building a query

    Hello

    using oracle 11 g.
    I have 2 tables.
    One is tableA which is having all the information of the user and the primary key is the useremailid.
    Another table is tableB is having info in case users so the primary key is caseid and the username is the foreign key.
    And ago created other fields as what has been the case, and as there are other fields.

    Now I'm trying
    to retrieve all users in tableA to tableB monthly, even if they did not create case
    as if tableA has
    userA who created the case in tableB on jan - 10 Feb-10
    UserB who created the case in tableB on feb - 10 mar-10

    So those who want to y
    User - Date added - cases created
    Output of query results
    userA - Jan-10 - 10
    userA - Feb-10-20
    userA Mar - 10 - 0
    UserB - Jan-10 - 0
    UserB - Feb-10-200
    UserB Mar-10 - 90

    create stmts are like this->
    Create table TableA
    (
    Username varchar (120) (PK)
    )

    Create table TableB
    (
    case_id varchar (15), (PK)
    cases_created timestamp (6).
    Username varchar (120) (FK)
    )

    SQL query
    SELECT count (case_id), to_char (cases_created,'MON-YYYY ""), username
    of right outer join tableB, tableA on tableA.username = TableB.username
    I tried using left/right/full, but I don't get what I want.

    Please help me on this.
    Thank you

    Hope that this application solves your condition

    select username,to_char(to_date('01'||months,'dd-mm-yy'),'mon-yy') "Cases created",sum(cnt) "Count of cases" from
    (
    select username,months,1 as cnt from (select lpad(rownum||'-'||yr,5,0) as months from ( select to_char(cases_created,'yy') yr from tableb where rownum<2)
    connect by level<=12) months_tab, tableb
    where to_char(cases_created,'mm-yy')=months
    union all
    select username,months,0 from (select lpad(rownum||'-'||yr,5,0) as months from ( select to_char(cases_created,'yy') yr from tableb where rownum<2)
    connect by level<=12) months_tab, tableb
    where to_char(cases_created,'mm-yy')!=months
    )
    group by username,months
    having months<=(select max(to_char(cases_created,'mm-yy')) from tableb)
    order by username,months asc
    ;
    
    USERNAME      Cases created Count of cases
    
    abc.abc@com   jan-10           1
    abc.abc@com   feb-10           2
    abc.abc@com   mar-10          0
    def.def@com   jan-10            0
    def.def@com   feb-10           1
    def.def@com   mar-10          1    
    

    Published by: Nina Prabhu Sep 29, 2011 23:55
    changed the code to numbers diaplat months on words

    months 'Case created' ===> to_char (to_date ('01' | month, "dd-mm-yy"), 'Mon - yy') 'created '.

  • Need help to define a query

    I have a table that contains three fields: IdEmp, IdRole, Description. I would get a line by IdEmp with his descriptions of role concatenated, separated by commas. Something similar to:

    IdEmp, role descriptions
    1, CQ, supervisor, account manager, administrator
    2, administrator, CQ, user input
    3, account Manager, HR user, user input
    4, user input

    Where the look of the table like this with its data

    IdEmp, IdRole, Description
    1, 1, 'administrator '.
    1, 2, "account Manager."
    1, 3, 'controller '.
    1, 4, "CQ".
    2, 1, 'administrator '.
    2, 4, 'CQ.
    2, 5, 'user input '.
    3, 2, 'account Manager '.
    3, 6, ' HR User.
    3, 5, 'user input '.
    4, 5, 'user input '.

    Thanks in advance for your help.

    Hello

    This is called String aggregation . Exactly how do depends on your version of Oracle and exactly to your needs. (For example, the order of the concatenated strings is important?)
    This page shows several ways to string aggregation:
    http://www.Oracle-base.com/articles/10G/StringAggregationTechniques.php

    My favorite is the aggregate function defined by the user, called STRING_AGG on the page above, but STRAGG at most other places. Once you have installed, cn you get the results you want like this:

    SELECT    idemp
    ,         STRAGG (description)    AS description_list
    FROM      table_x
    GROUP BY  idemp;
    

    It's hard to beat for convenience.
    STRAGG will work in Oracle 9 or higher.

  • Need help to form a query SQL/PLSQL

    Hello
    I'm not an expert in Oracle SQL. I need to have this query to improve the performance of my product. I have a table T that has a collar of column that is of type varchar. The neck is unique in the table (no two rows have the same value for the collar). Given a set S1 with 100 channels, the query should return me all channels in S1 who are not present in the neck.
    SQL query or PLSQL would be perfect. The query can have strings (elements of S1) hardcoded into it.

    Thanks in advance,
    Madhu

    user13290641 wrote:
    They are separate strings. They will be most likely hard-coded.

    In this case to pass them on in the form of record set as shown below, so that you can use operator LESS to get your result.

    with static_result_set as
    (
    select  'aaa' col from dual
    UNION ALL
    select  'xxx' from dual
    UNION ALL
    select  'ccc' from dual
    UNION ALL
    select  'zzz' from dual
    )
    select col from static_result_set
    minus
    select col from t
    
  • 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

  • From Oracle Lite, need help

    Hello world

    I have an application written in Java that connects to an Oracle 10g server and I want to use the same application on netbooks devices under WinXP. These devices use the application offline and then will synchronize with the DB.

    As far as I know, I need Oracle Lite database on mobile devices to get snapshots of database and track changes made in offline mode. I want to know if I need to install the Mobile Server to manage the synchronization of data between the mobile devices and DB.

    What I have to develop a web - app to manage the synchronization of the data, or there is an easier way to sinchronize in this case using only the desktop application?

    Thanks in advance for your help and sorry for my bad English.

    You want to watch the Win32 applications. There are the API in several programming languages which use the Sync Lite Oracle API to synchronize, or you can simply use the mSync.

Maybe you are looking for

  • MacBook does not illuminate

    13 in macbook with lion and a removable battery. Has had a failed for something fun but not dead battery for about 30 minutes, so I mainly use it on the charger. Wify disconnected the mac without the ' down to use the socket without knowing me, and a

  • MacBook does not load?

    Hello and happy holidays. If someone consults this forum and could give me a help that would be amazing. Student, trying to connect in his online class. But my macbook 2010 runs the last apple program (do not know if you need that) does not charge. N

  • HP Pavilion Entertainment PC: laptop power on password after 3 failed tent and says system disable 15908722.

    laptop power on password after 3 failed attempts and says system disable 15908722.

  • Windows Live e-mail address is not set correctly.

    Original title: live id email address a letter. My email address which was initially implemented on my xbox phase identification is spelled correctly. It's a letter off then the link windows live sends to reset the password does not go to my correct

  • Can not recover my system after a hard disk crash.

    Hello Hard drive of my hp DV6-3123tx crashed due to a collision The steps I did: Got a new hard drive and put it on. Via recovery DVD tried to format it. Everything was good, but when you restart the system to install the drivers it gets stuck only o