Query result set...

I can't determine a good word in my question. So, I think that my pseudocode below will be sufficient for this purpose.

Oracle version: 11.2 g

Data set
WITH temp AS
(
 SELECT 1 col1, 1 day FROM dual UNION ALL
 SELECT 2 col1, 1 day FROM dual UNION ALL
 SELECT 3 col1, 1 day FROM dual UNION ALL
 SELECT 4 col1, 1 day FROM dual UNION ALL
 SELECT 5 col1, 1 day FROM dual UNION ALL
 SELECT 6 col1, 1 day FROM dual UNION ALL
 SELECT 7 col1, 2 day FROM dual UNION ALL
 SELECT 8 col1, 2 day FROM dual UNION ALL
 SELECT 9 col1, 2 day FROM dual UNION ALL
 SELECT 10 col1,2 day FROM dual 
)
SELECT *
  FROM temp
The result of the above query:
COL1     DAY
1     1
2     1
3     1
4     1
5     1
6     1
7     2
8     2
9     2
10     2
You the WISH result set:
DAY COL_TOTAL
1   21  
2   55   -- 21 + 33 (the sum of day 1 + the sum of day 2)
I realize that I can get the desired result, set above the code below. However, for my I have several other data points and 100 separate days I need to do for their actual issue. In doing so, a statement of the UNION 100 isn't exactly going to work.
WITH temp AS
(
 SELECT 1 col1, 1 day FROM dual UNION ALL
 SELECT 2 col1, 1 day FROM dual UNION ALL
 SELECT 3 col1, 1 day FROM dual UNION ALL
 SELECT 4 col1, 1 day FROM dual UNION ALL
 SELECT 5 col1, 1 day FROM dual UNION ALL
 SELECT 6 col1, 1 day FROM dual UNION ALL
 SELECT 7 col1, 2 day FROM dual UNION ALL
 SELECT 8 col1, 2 day FROM dual UNION ALL
 SELECT 9 col1, 2 day FROM dual UNION ALL
 SELECT 10 col1,2 day FROM dual 
)
SELECT SUM(col1) AS col_total
  FROM temp
 WHERE day <= 1
UNION ALL
SELECT SUM(col1) AS col_total
  FROM temp
 WHERE day <= 2
Published by: user652714 on August 13, 2012 21:55

Hello

I now see what you want. You can use this:

select *
from tt
order by col_1;

     COL_1        DAY
---------- ----------
         1          1
         2          1
         3          1
         4          1
         5          1
         6          1
         7          2
         8          2
         9          2
        10          2

10 rows selected.

select day, sum(total) over  (order by  day) COL_TOTAL
from
(select day, sum(col_1) TOTAL
from tt
group by day
);

    DAY  COL_TOTAL
---------- ----------
         1         21
         2         55

2 rows selected.

Tags: Database

Similar Questions

  • Problem with the query result set * STILL a QUESTION *.

    Summary

    What I am tring to do is to return output to the data points that currently have no values.

    * Here it is sample data for reasons explaining my question (my data set is MUCH bigger)

    xTable
    YEAR    PEOPLE    ITEMS    TYPE_NUMBER TYPE_DESC    CLASS
    2010       1                 1              REG              1 
    2010        2        3         2             MISC             1
    2010        5        4         3             WEEK           1
    2010                             1             REG              2
    2010                             2             MISC             2
    2010                             3             WEEK           2
    2009       1                 1              REG              1 
    2009        2        3         2             MISC             1
    2009        5        4         3             WEEK           1
    2009                             1             REG              2
    2009                             2             MISC             2
    2009                             3             WEEK           2
    
    ... (there's over 100 other unique CLASS values)
    Desired output
    YR     PEOPLE   ITEMS   TOTAL PEOPLE  TOTAL_ITEMS
    2010         -            -            -                        -                         --  (Class 2 result set)
    
    /* FYI, If i wanted the class 1 result set it would look like this:
    YR     PEOPLE   ITEMS   TOTAL PEOPLE  TOTAL_ITEMS
    2010        8       7             16                    14                       -- (Class 1 result set)*/
    Oracle: 10.2 G
            select 2010 as yr,
                         nvl(f.people,'-') as people
                         nvl(f.items,'-') as items  
                         nvl(to_char(sum(f.people)),'-') as total_people,
                         nvl(to_char(sum(f.items)),'-') as total_items,
                         from Xtable,
                              (2010 as yr,
                              sum(items)as items
                              sum(people) as people
                              from xTable
                              where person_id = 99999
                              and   type_number in (1,2,3)
                              and year = 2010
                              and class = 2 
                              or class = 1
                              ) f
                         where person_id = 99999
                         and type_number in (1,2,3)
                         and yr = f.yr
                         and (year <= 2010 or year = 2010)
                         and (class = 2 or class = 1)
                         group by 
                         f.people,
                         f.items
    Currently, the query above will return no rows.

    Published by: user652714 on February 2, 2010 13:04

    How about this?

    SQL> WITH    xTable AS
      2  (
      3          SELECT 2010 AS YEAR, 1 AS PEOPLE, 0 AS ITEMS, 1 AS TYPE_NUMBER, 'REG' AS TYPE_DESC, 1 AS CLASS FROM DUAL UNION ALL
      4          SELECT 2010 AS YEAR, 2 AS PEOPLE, 3 AS ITEMS, 2 AS TYPE_NUMBER, 'MISC' AS TYPE_DESC, 1 AS CLASS FROM DUAL UNION ALL
      5          SELECT 2010 AS YEAR, 5 AS PEOPLE, 4 AS ITEMS, 3 AS TYPE_NUMBER, 'WEEK' AS TYPE_DESC, 1 AS CLASS FROM DUAL UNION ALL
      6          SELECT 2010 AS YEAR, NULL AS PEOPLE, NULL AS ITEMS, 1 AS TYPE_NUMBER, 'REG' AS TYPE_DESC, 2 AS CLASS FROM DUAL UNION ALL
      7          SELECT 2010 AS YEAR, NULL AS PEOPLE, NULL AS ITEMS, 2 AS TYPE_NUMBER, 'MISC' AS TYPE_DESC, 2 AS CLASS FROM DUAL UNION ALL
      8          SELECT 2010 AS YEAR, NULL AS PEOPLE, NULL AS ITEMS, 3 AS TYPE_NUMBER, 'WEEK' AS TYPE_DESC, 2 AS CLASS FROM DUAL
      9  )
     10  -- END SAMPLE DATA
     11  SELECT  YEAR
     12  ,       NVL(TO_CHAR(SUM(PEOPLE)),'-')    AS PEOPLE
     13  ,       NVL(TO_CHAR(SUM(ITEMS)),'-')     AS ITEMS
     14  ,       CLASS
     15  FROM    xTable
     16  WHERE   CLASS IN (1,2)
     17  AND     YEAR = 2010
     18  GROUP BY YEAR
     19  ,       CLASS
     20  ORDER BY CLASS DESC;
    
                    YEAR PEOPLE ITEMS CLASS
    -------------------- ------ ----- -----
                    2010 -      -         2
                    2010 8      7         1
    
  • How to clear the search query result set

    I dropped a search with the table query. When I search a value it is the display of the result, but when I click on the reset button, only the query Panel is reset to zero and not the table. What can I do to reset the table component associated with the query?
    When I click on the reset button, only the query panel goes to zero and no table.

    It will be reset nt table.

    What can I do to reset the table component

    Use of the component range reset. to do so.

    http://jobinesh.blogspot.in/2009/10/reset-content-of-Web-page.html
    http://myadfnotebook.blogspot.in/2011/05/making-reset-button-in-afquery.html trickles you want
    http://andrejusb.blogspot.in/2009/09/programmatical-reset-for-query-results.html

    -edited lately

    Published by: ADF7 on February 22, 2012 12:58 AM

  • Keep the query results set all paging

    I have a query back thouands of records. For do not impact performance display and paging, which would be the best way without use of query caching? Is it possible to keep the first recordset query time she and her use in Exchange?

    Load balancing definitely throws a wrinkle by using shared memory.  There are ways to address these problems, but they all come with the pros and cons, just basically choose what pros like you and what the disadvantages that you can live with.

    I.E. you can use sessions.  So when a user first access your application, all future queries are sent to the same server that processes requests for first.  But this means that if this server will see, all users currently glued to it are s.o.l.

    I saw other people using databases for the persistent memory.  But then, you have the LAG involved in obtaining data from the database on every request, although it should be easy fast queries to do this.  Side pro, it does not matter which server processes the request.

    It would be different then just re - run the original query every time, but rather store the original query results into a temporary table space so that any complexity which may exist should not be repeated.

  • Help with the query to select only one record from the result set in double

    Hello

    Please help with the query. Version of Oracle database we use is 10g R2.

    I have a vision that is duplicated IDS, but they are used across the different functions. See below examples of data. Please help me with a query to select only one record (based on ID regardless of the area) from the bottom of the result set of duplicate records. For what is the point of view is there unique records, given the combination of the fields ID, Org, DF, dry, Sub-Sec

    ID
    Org
    DF
    Sec Sub-Sec

    (163)CQCPDMCPDMHD(163)PCENGENGENG(163)CQASICASICIS8888TSTACTACTAC(163)TSHEHESW6789CQINFOINFOFOS6789PCSECSYSSECSYSINFO16789TSSECSYSSECSYSINFO29009PCBMSBMSBMS1

    My result set must eliminate the duplicate identifiers regardless of whoever we choose of the result set. (I mean without distinction Org, DF, s, Sub-s). My expected result set should be.

    ID
    DSB

    DF
    SEC
    Sub-Sec
    (163)CQCPDMCPDMHD8888TSTACTACTAC6789CQINFOINFOFOS9009PCBMSBMSBMS1


    Thank you

    Orton

    Hello

    This sounds like a job for ROW_NUMBER:

    WITH got_r_num AS

    (

    SELECT id, DSB, df, s, sub_sec org

    ROW_NUMBER () OVER (PARTITION BY ID.

    ORDER BY org

    ) AS r_num

    OF view_x

    )

    SELECT id, DSB, df, sub_sec s,

    OF got_r_num

    WHERE r_num = 1

    ;

    He is a Top - N query example, where you choose the elements of N (N = 1 in this case) from the top of an ordered list.

    I hope that answers your question.
    If not, post a small example of data (CREATE TABLE and INSERT, only relevant columns instructions) to your sample data and the results desired from these data.  (I know that you said that you were a view selection.  Just for this thread, pretending it is a picture and post simple CREATE TABLE and INSERT statements to simulate your point of view).
    Point where the above query is to produce erroneous results, and explain, using specific examples, how you get the right results from data provided in these places.  (I didn't quite understand the explanation above.  I don't know why you want to

    ID ORG DF DRY SUB_SEC

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

    1234 CQ DPRK DPRK HD

    and is not

    1234 IS CQ ASIC, ASIC

    or

    TS 1234 IT IT SW

    or

    1234 CQ ASIC ASIC HD

    )
    If you change the query at all, post your modified version.
    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002

  • Unable to loop result set of a query in Site Studio component

    Hi all

    I write a custom component to display the records in the custom table.

    I created a (docservice) service called 'GET_DATA_VIEW' and selected scriptable. In action, I've assigned a "Quser" query and the query "select type query cache. I gave the name of result as GET_USER set.

    Now, the "Quser" query is a simple select statement as follows:

    Select * from temp_user where dName =?.

    Added dName as a parameter.

    The studio site page when I run the service and the result set, the loop it does not work. The code is as below:

    <!-$dName = 'test'->
    <!-$ executeService ("GET_DATA_VIEW")->
    <! - $loop GET_USER - >
    <!-$dName->: <!-$dDepartment->
    <!-$endloop->

    It does not, so that if I create a page template in the component that works.

    Help, please.

    Leo30 wrote: my query is "select country countryname".

    Where COUNTRYNAME is a column in the country table.

    When trying to loop through the result set, it does not print the result as below:




    >

    Leo30 wrote: but when I print it gives me the correct number of rows in the table.

    Hoping that the "USER_LIST" is a typo. The result set name is 'COUNTRY_LIST '.

    In any case, assuming that the fault of typo, it is likely that you have the incorrect column name or the name of the column is in the wrong case. The names are case-sensitive, and you can not return the column name, you expect from the result set.

    Try to use this code to print the actual column names and values of the result set.






    =


  • Through a set of query results, replacing one string by another.

    I want to write a function that replaces the occurrence of a string with another string different.  I need to be a CF fuction which can be called from another function CF.  I want to 'hand' this function (a string) SQL statement like this: (Please note, don't bother commenting "there are ways of writing this SQL try..., I made this simple example to get to the point where I need help.)  I have to use a 'sub_optimal' SQL syntax just to demonstrate the situation)

    Here's the string I want to pass to the function:

    SELECT

    [VERYLONGTABLENAME]. FIRST NAME,

    [VERYLONGTABLENAME]. LAST_NAME,

    [VERYLONGTABLENAME]. ADDRESS

    Of

    LONGTABLENAME [VERYLONGTABLENAME]

    Here are the contents of the ABRV table:

    TBL_NM, ABRV <! - header line - >

    VERYLONGTABLENAME, VLTN

    SOMEWHATLONGTALBENAME, SLTN

    MYTABLENAME, MTN

    ATABLENAME, ATN

    The function returns the string origin, but with the abbreviations in place names of the long table, example:

    SELECT

    VLTN. FIRST NAME,

    VLTN. LAST_NAME,

    VLTN. ADDRESS

    Of

    LONGTABLENAME VLTN

    Notice that only the table brackets and names that match a value in the table ABRV were replaced.  The LONGTABLENAME immediately following the FROM is left as is.

    Now, here's my attempt at amateur said written dum function: Please look at the comment lines for where I need help.

    < name cffunction output = "AbrvTblNms" = "false" access = "remote" returntype = "string" >
    < name cfargument = "txt" type = "string" required = "true" / >

    < cfset var qAbrvs = "" > <! - variable to hold the results of the query - >

    < cfset var output_str = "#txt #" > <!-I create a local variable so I can manipulate the data returned by the parameter TXT.  Is it necessary or can I use the parameter txt? ->


    < cfquery name = "qAbrvs" datasource = result "cfBAA_odbc" = "rsltAbrvs" >
    SELECT TBL_NM, ABRV FROM BAA_TBL_ABRV ORDER BY 1
    < / cfquery >

    <!-I'm assuming that at this point, the query is executed and records in the result-> set

    < cfloop index = list "idx_str" = "#qAbrvs #" > <! - is - right?  I do not. ->
    < cfset output_str = Replace (output_str, "#idx_str #",) <! - is - right?  I do not. ->

    < / cfloop > <!-who am I loop on?  What is the index? How to do the replacement of the chain? ->

    <!-the chunck, below is a partial of my Object Pascal from Delphi function that does the same thing

    I need to know how to write this part in CF9
    So what not of folklore
    Start
    s: = StringReplace (s, ' [' + FieldByName('TBL_NM').]) [AsString + ']', FieldByName ('ABRV'). AsString, [rfReplaceAll]);
    Following;
    end;
    ->

    < cfreturn output_txt >

    < / cffunction >

    I am mostly struggling with the syntax here.  I know what I want to do, I know how to get there in a different programming language, just not CF9.  Thanks for any help you can provide.

    RedOctober57 wrote:

    ...
    Thanks for any help you can provide.

    One:

     

    No you need not create a local variable that is a copy of the variable arguments that the scope of the arguments is already local to the function, but you are not referencing correctly the scope of the arguments, then you may be using a variable 'txt' in another scope.  So best practice would be to refer to "arguments.txt" where you must.

    Two:

    I know what I want to do, I know how to get there in a different programming language, just not CF9.

    Then a best start would be to descirbe what you want to happen and give a simple example in the other programming language.  Most of us is muti-lingual and can analyze code in any clear and clean syntax.

    Three:

         

    I think you want to be a loop on your recordset returned by the previous query, perhaps 'qAbrvs '.

    
    

    Four:

    Continuing on this assumption I suppose you want to replace each instance of the long chain with the short form string that record together.

    
    

    Five:

                  

    If this is true, then you loop through the set of records from tablenames and abbreviations that you want to replace the chain.

  • Looping over a query and return of all result sets

    Hello

    I'm a loop on a query by passing a list of ID numbers, I want to be able to return all results as a result. How can I find all these results as a whole. Here's how I do it now:

    Wow, what a strange concept...
    How about first creating a complete listOfBooks and then by running the query of searchSelect only once?
    now what you're doing, starting with an empty list (listOfBooks), you add an element to it with each loop and run your searchSelect again and again on the expansion list, query by returning the same values as you are returned in the previous step more new.
    what you need to do is:
    (1) create your list of listOfBooks and fill with searchContents as query results. This will create your complete list of book IDs.
    (2) run your searchSelect of query only once with tblMaterials.MaterialID IN (' #listOfBooks # ').
    No loops, no. wasted the resources of the CPU and bandwidth...

  • Group by result set divergence

    Hi all

    Please help me on below.

    When I group by result set is as below.

    SQL > select count (*), the Group of the stg_famis_equip, by attribut2, attribut2 having count (*) > 1;

    COUNT (*) ATTRIBUT2

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

    2 FM023566                                                            

    2 FM021481                                                            

    2 FM025833                                                            

    2 FM058806                                                            

    Where, if I query with WHERE clause for certain values as below.

    Please suggest me on, why it is show as count = 2 and when questioning with only WHERE clause (some time no record).  I do one any error... Please suggest me.

    SQL > select attribut2, attribut3 from stg_famis_Equip where attribut2 = 'FM023566';

    ATTRIBUT2 ATTRIBUT3

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

    FM023566 12

    SQL > select attribut2, attribut3 from stg_famis_Equip where attribut2 = 'FM021481';

    ATTRIBUT2 ATTRIBUT3

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

    FM021481 11

    FM021481 12

    SQL > select attribut2, attribut3 from stg_famis_Equip where attribut2 = 'FM025833';

    ATTRIBUT2 ATTRIBUT3

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

    FM025833 07

    FM025833 12

    SQL > select attribut2, attribut3 from stg_famis_Equip where attribut2 = 'FM058806';

    no selected line

    I'm on the Oracle 11 g Enterprise Edition Release Database 11.2.0.2.0 - 64 bit Production

    Thank you

    This means that:

    FM023566 has the new line and return at the end of the string (13/10)

    FM021481 is clean

    FM025833 is also clean

    FM058806 has the new line and carriage return to then end of the string

    Trim will replace spaces when not specified what should be trimmed. If you could do a RTRIM (attribute2, CHR (13): 10 to clean the data.)

    HTH

  • Query result cache

    Today I test the query result cache, but the result is not as I expected. Please give a few advices.thank you in advance.

    SQL > create table (qrc_tab)

    Number 2);

    Table created.

    SQL > insert into qrc_tab values (1);

    1 line of creation.

    SQL > insert into qrc_tab values (2);

    1 line of creation.

    SQL > insert into qrc_tab values (3);

    1 line of creation.

    SQL > insert into qrc_tab 4;

    1 line of creation.

    SQL > insert into qrc_tab values (5);

    1 line of creation.

    SQL > create or replace function slow_function (p_id in qrc_tab.id%TYPE)

    2 back qrc_tab.id%TYPE DETERMINISTIC AS

    3 BEGIN

    4 DBMS_LOCK.sleep (1);

    5 return p_id;

    6 end;

    7.

    The function is created.

    SQL > set timing on

    SQL > select slow_function (id) in the qrc_tab;

    SLOW_FUNCTION (ID)

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

    1

    2

    3

    4

    5

    Elapsed time: 00:00:05.01

    SQL > select / * + result_cache * / slow_function (id) of qrc_tab;

    SLOW_FUNCTION (ID)

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

    1

    2

    3

    4

    5

    Elapsed time: 00:00:05.00

    SQL > select / * + result_cache * / slow_function (id) of qrc_tab;

    SLOW_FUNCTION (ID)

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

    1

    2

    3

    4

    5

    Elapsed time: 00:00:05.00

    SQL > select / * + result_cache * / slow_function (id) of qrc_tab;

    SLOW_FUNCTION (ID)

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

    1

    2

    3

    4

    5

    Elapsed time: 00:00:05.00

    I wonder why time is not fast? Help, please

    create or replace function slow_function (p_id in qrc_tab.id%TYPE)

    return qrc_tab.id%TYPE result_cache DETERMINISTIC AS

    Start

    DBMS_LOCK. Sleep (1);

    return p_id;

    end;

    /

    --

    John Watson

    Oracle Certified Master s/n

  • Ask help - please help me get the expected defined in a query result

    Dear all,

    Help with your suggestions and advice. Please see the bottom of the screenshots for the planned result sets.

    create table shift_dt 
    (name varchar2(20),
    shift_start date,
    shift_end date);
    
    insert into shift_dt values ('Brian',to_date('01-01-2015 09:10:00','dd-mm-yyyy hh24:mi:ss'), to_date('01-01-2015 22:10:00','dd-mm-yyyy hh24:mi:ss'));
    insert into shift_dt values ('Brian',to_date('02-01-2015 09:10:00','dd-mm-yyyy hh24:mi:ss'), to_date('04-01-2015 00:00:00','dd-mm-yyyy hh24:mi:ss'));
    insert into shift_dt values ('Brian',to_date('04-01-2015 00:00:00','dd-mm-yyyy hh24:mi:ss'), to_date('05-01-2015 15:20:00','dd-mm-yyyy hh24:mi:ss'));
    insert into shift_dt values ('Brian',to_date('06-01-2015 10:00:00','dd-mm-yyyy hh24:mi:ss'), to_date('06-01-2015 23:20:00','dd-mm-yyyy hh24:mi:ss'));
    insert into shift_dt values ('Brian',to_date('07-01-2015 11:00:00','dd-mm-yyyy hh24:mi:ss'), to_date('09-01-2015 00:00:00','dd-mm-yyyy hh24:mi:ss'));
    insert into shift_dt values ('Brian',to_date('09-01-2015 00:00:00','dd-mm-yyyy hh24:mi:ss'), to_date('10-01-2015 22:00:00','dd-mm-yyyy hh24:mi:ss'));
    
    select * from shift_dt;
    
    
    

    Select * from shift_dt;

    1.jpg

    Expected result set: -.

    1.jpg

    I compare date of shift_end with the next date of shift_start of the day. If the dates made 12 am (IE 00:00:00), then I ignore and display the next shift end date available as displayed in the screen game result. Please see the screen set outcome turned for more details.

    Please help get the results you want in the SQL query

    Kind regards

    Souls

    Hello

    Here's one way:

    WITH got_new_grp AS

    (

    SELECT name, shift_start, shift_end

    CASE

    WHEN shift_start = TRUNC (shift_start)

    AND shift_start = LAG (shift_end) OVER (ORDER BY shift_start)

    THEN 0

    1. OTHER

    END AS new_grp

    OF shift_dt

    )

    got_grp AS

    (

    SELECT name, shift_start, shift_end

    SUM (new_grp) OVER (ORDER BY shift_start) AS grp

    OF got_new_grp

    )

    SELECT MIN (name) DUNGEON (DENSE_RANK FIRST ORDER BY shift_start)

    As a name

    MIN (shift_start) AS shift_start

    MAX (shift_end) AS shift_end

    OF got_grp

    GROUP BY grp

    ORDER BY shift_start

    ;

    Output (from the full sample data):

    NAME SHIFT_START SHIFT_END

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

    01/01/2015 Brian 09:10 01/01/2015 22:10

    Brian 02/01/2015 09:10 01/05/2015 15:20

    Brian 06/01/2015 10:00 01/06/2015 23:20

    Brian 07/01/2015 11:00 10/01/2015 22:00

    Brian 01/02/2015 15:00 02/02/2015 00:00

    Brian 02/02/2015 11:00 02/02/2015 17:00

    Brian 03/02/2015 00:00 03/02/2015 08:00

    Brian 02/05/2015 15:30 06/02/2015 07:30

    Brian 07/02/2015 23:30 10/02/2015 00:30

    I guess that shift_start is unique.

    It's finally a GROUP BY problem: we want to show the first shift_start and the last shfit_end of a group of 1 or more lines.  The tricky part is to identify the groups.  If I understand the problem, line X is grouped with the previous line X-1 if shift_start on the X line is midnight and is equal to shift_end on line X-1.  The above query uses the LAG analytic to see if each line begins a new group or not and the analytic function SUM to see how many groups have already begun (and therefore, which group each line entry belongs.)

    I still don't understand why you 'Corrected name of Anne in Brian' and not vice versa.  I guess the name to display in each group is the name of the first row in the Group (i.e. the line with the shift_start earlier).

  • Duplicates in hierarchical query results

    Please use the suite of applications:

    CREATE TABLE EMP

    (NUMBER OF EMP_ID,

    EMP_NAME VARCHAR2 (50).

    NUMBER OF MANAGER_ID

    );

    INSERT INTO EMP VALUES(1,'SCOTT',);

    INSERT INTO EMP VALUES(2,'JOHN',1);

    INSERT INTO EMP VALUES(3,'ROB',2);

    INSERT INTO EMP VALUES(4,'MIKE',2);

    INSERT INTO EMP VALUES(5,'BOB',);

    INSERT INTO EMP VALUES(6,'JEFF',5);

    COMMIT;

    SELECT * FROM EMP, (SELECT EMP_ID FROM EMP WHERE EMP_ID IN (1.5)) TEMP

    START BY EMP. EMP_ID = TEMP. EMP_ID

    CONNECT BY PRIOR EMP. EMP_ID = EMP. MANAGER_ID;

    Basically, I first spend a few emp_ids a subquery and then want to see all records in the table emp which are direct or indirect of those emp_ids children.

    But I get a lot of duplicate records. Can someone explain what is the reason behind this? I can use SEPARATE and spend, but there could be a problem with my request in the first place.

    Help, please!

    Thank you

    RN

    Hello

    RN says:

    Please use the suite of applications:

    CREATE TABLE EMP

    (NUMBER OF EMP_ID,

    EMP_NAME VARCHAR2 (50).

    NUMBER OF MANAGER_ID

    );

    INSERT INTO EMP VALUES(1,'SCOTT',);

    INSERT INTO EMP VALUES(2,'JOHN',1);

    INSERT INTO EMP VALUES(3,'ROB',2);

    INSERT INTO EMP VALUES(4,'MIKE',2);

    INSERT INTO EMP VALUES(5,'BOB',);

    INSERT INTO EMP VALUES(6,'JEFF',5);

    COMMIT;

    SELECT * FROM EMP, (SELECT EMP_ID FROM EMP WHERE EMP_ID IN (1.5)) TEMP

    START BY EMP. EMP_ID = TEMP. EMP_ID

    CONNECT BY PRIOR EMP. EMP_ID = EMP. MANAGER_ID;

    Basically, I first spend a few emp_ids a subquery and then want to see all records in the table emp which are direct or indirect of those emp_ids children.

    But I get a lot of duplicate records. Can someone explain what is the reason behind this? I can use SEPARATE and spend, but there could be a problem with my request in the first place.

    Help, please!

    Thank you

    RN

    Thanks for posting the CREATE TABLE and INSERT statements; It is very useful.

    When you do CONNECT BY and joins in the same query (sub-), joins are made first, and then CONNECT BY is performed on all results of the joints.  In this example, you make a cross of PMCs and this join called temp.  Let's see what the result set looks like:

    SELECT *.

    WCP

    ,       (

    SELECT emp_ID

    WCP

    WHERE emp_id IN (1.5)

    ) temp

    ;

    Output:

    EMP_ID, EMP_NAME MANAGER_ID EMP_ID

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

    1 SCOTT 1

    2 1 1 JOHN

    3 ROB                 2          1

    4 1 2 MIKE

    5 BOB                            1

    JEFF 6 5 1

    1 SCOTT 5

    2 1 5 JOHN

    3 ROB                 2          5

    4 2 5 MIKE

    5 BOB                            5

    6 5 5 JEFF

    Now, you do a CONNECT BY query on the result set.  As you can see, there are 2 lines with emp_id = 2 in the set of results above, which explains why there are 2 lines in your query results with emp_id = 2.  Similarly, the output defined above, there are 2 lines with emp_id = 3, and they all have two manager_id = 2, each of them is considered to be a child of each of each of the 2 rows with emp_id = 2, and therefore, you have 2 * 2 = 4 lines with emp_id = 3 in the output CONNECT BY.

    What is a cross here join?  Why must you temp?  Why not just a CONNECT BY questioning one emp, like this:

    SELECT *.

    WCP

    START WITH emp_id IN (1, 5)

    CONNECT BY PRIOR Emp_id = manager_id

    ;

    ?

  • new to 4.02, no grid or column names in the query results

    It is a bit strange, I have SQL Developer 4.02 just installed, and I don't see grid or column names in the query results.  Don't see anything useful in tools > Preferences, what don't get me?

    The worksheet gives you a couple of different ways to run a query...

    1. instruction execute sheet icon toolbar (large green arrow, or Ctrl + Enter).

    This produces a results tab of the query with the data displayed in a grid.

    2. worksheet icon toolbar Execute the Script (the small tip of the green arrow on lined paper, or F5).

    This produces a tab out of the Script with the data displayed on the printer.

    If execution of the statement to Execute the Script, using some SQL * most orders will remove the column headers:

    1. set the position

    2. set the pagesize 0 (or 1 or 2).

    Pagesize and linesize default is - 1.  By default, the spreadsheet is free for formatting output script as he wishes.  If you have not changed these settings in the spreadsheet, then see if you point to a startup script in Tools > Preferences > Database > name of the connection startup script file

    Kind regards

    Gary

    SQL DeveloperTeam

  • UNION and UNION ALL giving multiple result sets even if INTERSECT does not lines.

    Hello

    I have a set of two queries. I used the UNION to join the result set. When I used UNION ALL, I get a different result set.

    BT when I use INTERSECT, I m not getting all the lines.

    SO, which I guess is, as operation INTERSECT isn't Govind all the lines, then the UNION and UNION ALL of the result sets must be simliar.

    But I m getting different result sets.

    Please guide me.

    Thank you.

    Hello

    UNION returns separate lines; UNION ALL returns all rows that produce queries.

    INTERSECT has nothing to do with it.  You can have the lines in a single query.  For example

    Job SELECTION

    FROM scott.emp

    UNION - ALL THE

    SELECT 'FUBAR '.

    DOUBLE;

    In this example, there is no overlap between the 2 queries (INTERSECT would have 0 rows).

    UNION produces 6 lines, because the query at the top of the page produces 5 distinct lines (of 14 total ranks) and the background query 1.

    UNION ALL product lines 15: 14 of the request from top and 1 of the request from the lower part.

    I hope that answers your question.

    If not, post a test script (if necessary) and complete, including some UNION, UNION ALL queries INTERSECT.  Post of the CREATE TABLE and INSERT statements for all tables using those queries (with the exception of the commonly available rtables, such as those of the scott schema) and a specific question, such as "the UNION query all product...» I expect the UNION query to produce... because... but instead, it produces... Why is this?  It seems contractict... manual which says that... ».

  • Hotkey of the worksheet area of writing in the result set

    Y at - it a keyboard shortcut to switch between writing in the SQL worksheet and navigate (or copy,...) in the result set of a query run?

    That would make my job easier everyday!

    @Chris

    ALT + PagegDown in version 4.0

    No no not a nav of Ko to this v3.2 and earlier versions, has been saved as a bug.

Maybe you are looking for