Analytical query question

Hi all
I'm kinda new in the analytical work with query and have a moment difficult to achieve what I wanted.
Here is the script to create the table and insert data.
create table temp_ar1 (lkey NUMBER,
amount NUMBER
)
;
Insert the Script-
Insert into temp_ar1 values (1, -2.5);
Insert into temp_ar1 values (1, -10);
Insert into temp_ar1 values (1, 5);
Insert into temp_ar1 values (2, 0);
Insert into temp_ar1 values (2, -5);
Insert into temp_ar1 values (2, 2);
Insert into temp_ar1 values (2, -1);


With main_table as 
(
select lkey,amount
      ,case when amount > 0 then
        'positive'
        else
        'negative'
        end col1
from temp_ar1
)
,Temp_A as
(
Select O.*
     ,Sum ( case when col1 = 'negative' then amount
                else null
                end ) Over (partition by col1,lkey) as sumcol1
                
      ,Sum ( case when col1 = 'positive' then amount
                else null
                end ) Over (partition by col1,lkey) as sumcol2
               
  from main_table  O
 )
  select  lkey,amount,col1,sumcol1,sumcol2
 from Temp_A order by lkey

      LKEY     AMOUNT COL1        SUMCOL1    SUMCOL2
---------- ---------- -------- ---------- ----------
         1          5 positive                     5
         1       -2.5 negative      -12.5
         1        -10 negative      -12.5
         2         -1 negative         -6
         2          2 positive                     2
         2          0 negative         -6
         2         -5 negative         -6

7 rows selected.
I'm trying to get the result that resembles
lkey , sumcol1 , sumcol2 
1       -12.5    5
2       -6      2 
So for each separate key, I should get only a line that has sumcol1 (which is the sum of a negative amount for this key) and
sumcol2 (which is the sum of the positive amount for this key)

Really appreciate your help on this matter.

or, if you do not want to group by;

SQL> with Temp_A as
  2  (
  3  select O.*
  4    ,sum ( case when amount < 0 then amount
  5                  else null
  6           end ) over (partition by lkey) as sumcol1
  7    ,sum ( case when amount > -1 then amount
  8                else null
  9           end ) over (partition by lkey) as sumcol2
 10    ,row_number() over (partition by lkey order by lkey) rn
 11    from temp_ar1  O
 12   )
 13  --
 14  select  lkey,sumcol1,sumcol2
 15  from Temp_A
 16  where rn = 1
 17  /

      LKEY    SUMCOL1    SUMCOL2
---------- ---------- ----------
         1      -12.5          5
         2         -6          2

Tags: Database

Similar Questions

  • XML PIVOT QUERY QUESTIONS

    I'm new to PIVOT queries.  I was making reference to the discussion that follows, but ran into a problem.  https://forums.Oracle.com/message/9393302

    When I added the XML to format data, I started to have NULL values rather than what I was looking for.

    WITH (AS PIVOT_DATA)

    SELECT * FROM)

    SELECT THE REGION, FCST_PERIOD, PRIME_PART, FINAL_FORECAST

    OF XYZ WHERE FCST_PERIOD > = last_day (trunc (sysdate)) + 1 and FCST_PERIOD < = last_day (add_months(sysdate,12)) AND PRIME_PART IN ('BLAH')

    )

    PIVOT XML (SUM (FINAL_FORECAST) FOR FCST_PERIOD IN (SELECT DISTINCT FCST_PERIOD OF XYZ))

    )

    SELECT PRIME_PART, REGION,

    EXTRACTVALUE(FCST_PERIOD_XML,'/PIVOTSET/ITEM[1]/COLUMN[2]') FCST_PERIOD1,

    EXTRACTVALUE(FCST_PERIOD_XML,'/PIVOTSET/ITEM[2]/COLUMN[2]') FCST_PERIOD2,

    EXTRACTVALUE(FCST_PERIOD_XML,'/PIVOTSET/ITEM[3]/COLUMN[2]') FCST_PERIOD3

    OF PIVOT_DATA;

    RESULTS IN:

    PRIME_PARTREGIONFCST_PERIOD1FCST_PERIOD2FCST_PERIOD3
    BLAMIDWEST(NULL)(NULL)(NULL)
    BLAWEST-NV(NULL)(NULL)(NULL)
    BLASOUTH-EAST(NULL)(NULL)(NULL)
    BLAWEST-CA(NULL)(NULL)(NULL)
    BLASOUTHWEST(NULL)(NULL)(NULL)
    BLAEAST(NULL)(NULL)(NULL)

    The second part of my question is how would I do a group when you use a pivot query.  When I finished, I've has several parts and would like to group by REGION.

    Thank you!

    Names of elements and attributes in XML are case-sensitive.

    Try with:

    EXTRACTVALUE (FCST_PERIOD_XML, ' / PivotSet/item [1] /column [2]') FCST_PERIOD1

    There is also a good chance that EXTRACTVALUE is frowned upon in your version. Instead, use XMLQuery or XMLTable.

    That being said, using XML PIVOT just to grind it later into a number known columns is pretty useless.

    The same can be achieved much more efficiently with regular PIVOT operator (and if necessary analytical function).

    Post some examples of data in the table of your database and your final result expected to get useful assistance.

  • Progressive, Question on the fields of dynamic/DocID/Query Question of profiling

    This may seem like a newb question but someone has a logical division of the BOM designation DocID?

    In the course, we are responsible to use DocID = 1 and DocID = 2, but when/why would you use DocID = 3 or higher?

    Any help would be greatly appreciated with the portion of dynamic fields/query string of the progressive profiling course.

    Thank you.

    The DocID is a complete example. You can actually assign the query string to be what you want. I recently use cmpID for the campaign ID, where I spent to map identity of the campaign and then sent the URL to redirect dynamically the campaign ID field. In my case, we worked with 10 different docs or goods, so we had to actually 10 unique codes. In this case we would have, for example cmpID = 483h76d; cmpID = 9j872hd, etc, etc. If you want to learn more about how you can use the query string values, see this post: how Progressive E10 profiling using the component of cloud - scroll down to step 6 and you'll see how to set up dynamic rules using query strings.

  • Query does not work on my iPad (was: media query question)

    I am a site for mobile/tablet/Office of coding and while my office and the iphone respond to the CSS, my ipad does not work. The changes that I do take the tablet in dreamweaver, but when they go live the ipad does not. Of course, my media query must be incorrect, but I can't understand the question. Any help is very appreciated.

    http://gbetza.mydomain.com/WebService2/test/KathrynFee/2014/profile.html

    Thank you.

    Yes.  It is inevitable because smart phones are getting bigger while tablets are smaller & more.  He's always going to be some crossover.   Think of it this way, when a Tablet is behaving as a mobile device, he takes over the mobile network.  When the shelf behaves more like a desktop computer, you need the layout of office.

    Similarly, a great smartphone in landscape mode is likely to resemble the layout of your tablet.

    See the FluidGrid example below:

    http://ALT-Web.com/FluidGrid/Fluid2.html

    Office = 4-col layout

    Tablet = 2-col layout

    Mobile = 1-col layout

    For purposes of illustration, I added a thick border of gold at the disposal of the Tablet only.

    / * Tablet Layout: 481px to 768px. Inherits the styles of: Mobile layout. */

    @media only screen and (min-width: 481px) and (max-width: 768px) {}

    .gridContainer {width: 92%; border: 16px solid gold}

    }

    Nancy O.

  • BI Publisher data model / query question

    Hello

    I'm designing a report of BI Publisher where I need to show negative numbers in red. I'm perfectly fine with the help of the conditional formatting to do this (just using the default value is conditional formatting in the add-in in MS Word, that generates code like this:)

    <? If: number(Cost_-_MTD) < 0? > <? attribute@InContext:color;' Red '? > <? end if? >

    BUT...

    I have columns returning who sometimes have a NULL value in them, and when they have the value NULL, it causes the foregoing to the pipe upwards. So I thought, 'I'll just add IfNull() in queries defined in the model data, and who will take care of the problem... "

    that works - a little. Now, my problem is if I have to go back and change the query for some reason, I get an error "query cannot be parsed, please modify the query manually. I really don't want to do that, I wish that other developers do NOT have to manually change the queries.

    So my question is - is it possible to add in the IfNull() directly in the query designer, so it should not be changed manually from that moment?

    p.s. using IfNull OBIEE server as our data source.

    Thanks in advance,
    Scott

    I don't think there is a way to add the ifnull condition in the query designer. So, you will need to manually change the query...

    In your model so you can add additional control like this:
    <0?>...

    Thank you!

    Published by: BIPuser on Sep 17, 2010 09:20

  • Another simple case of analytic query.

    Hello world
    I need help to write a query, probably using the analytical functions, which I am new to.
    Seems to be easy for experienced people.

    In my table I have:

    user_id varchar2,
    vistitor_id varchar2,
    Server_id varchar2,
    date of server_hit_date

    If server_id, B then old_system
    Server_id in C, D, then new_system if

    How to write a qury to get:

    user_id. visitor_id, last_date_old_system, last_date_new_system

    Not sure if I have to use analytical functions?

    Thanks a lot for your help.
    LSY.

    lsy_nn wrote:
    Hello world
    I need help to write a query, probably using the analytical functions, which I am new to.
    Seems to be easy for experienced people.

    In my table I have:

    user_id varchar2,
    vistitor_id varchar2,
    Server_id varchar2,
    date of server_hit_date

    If server_id, B then old_system
    Server_id in C, D, then new_system if

    How to write a qury to get:

    user_id. visitor_id, last_date_old_system, last_date_new_system

    Not sure if I have to use analytical functions?

    Thanks a lot for your help.
    LSY.

    select
       user_id,
       visitor_id,
       max
       (
          case
             when server_id in ('A', 'B')
             then
                server_hit_date
          end
       )  as last_date_old_system,
       max(
          case
             when server_id in ('C', 'D')
             then
                server_hit_date
          end
       )  as last_date_new_system
    from
       YOUR_TABLE
    group by
       user_id,
       visitor_id
    

    Should do?

  • analytical query to select the next record

    Hi all
    I would like to ask the two table below correspond to the output.
    1.dev_wt 2.dev_map 3. output result

    To help more clearance, please see this image link: http://lh6.ggpht.com/_xL6eBqjW6Yo/TEqnSvlF_FI/AAAAAAAAB0U/i2sclnnaj6g/Untitled-3.jpg

    1 dev_wt
    PMS_COMP     PMS_I       PMS_PERF_D   PMS_WT   PMS_CREATION_D
    BBOARD     GICEQGROSS    04/01/2001     30     04/05/2001
    BBOARD     GICST_B       04/01/2001     5      04/05/2001
    BBOARD     SBGS_B        04/01/2001     65     04/05/2001
    
    BBOARD     GICEQGROSS    04/11/2001     30     04/15/2001
    BBOARD     GICST_B       04/11/2001     5      04/15/2001
    BBOARD     SBGS_B        04/11/2001     65     04/15/2001
    2 dev_map
    GS_CODE     GS_I_CODE    GS_I_ID  MD_ID   GS_START_DT    GS_END_DT
    GICEQGROSS   CIWL        304       15     01/04/1998     31/03/2004
    GICEQGROSS   CIWL        304       2     01/04/2004      31/03/9998
    GICST_B      GICST_B     3707      15     01/04/2000     31/12/9998
    SBGS_B       SBGS_B      2231      15     01/04/1992     30/09/2003
    SBGS_B       SBGS_B      564       15     01/10/2003     31/12/9998
    I would like to match PMS_I = GS_CODE to retrieve the GS_I_CODE and analytical assistance

    query to search for the next record.
    Because I need to select record GICEQGROSS PMS_PERF_D date and following
    GICEQGROSS save PMS_PERF_D of dev_wt table and put the result GS_WT_FR and GS_WT_TO output.

    The date is in the format YYYYMMDD
    PMS_WT is divided by 100

    * 3. Output result *.
    GS_I_ID    PMS_COMP     GS_I_CODE     GS_WT_FR     GS_WT_TO     GS_I_CALC
    304       BBOARD        CIWL          20010401     20010410     0.3
    3707      BBOARD        GICST_B       20010401     20010410     0.05
    5209      BBOARD        SBGS_B        20010401     20010410     0.65
    PMS_COMP is of dev_wt table
    GS_I_CODE is dev_map join of tables with dev_wt
    GS_WT_FR is dev_wt GS_START_DT table
    GS_WT_TO's dev_wt record table according GS_START_DT where PMS_I = 'GICEQGROSS. '

    Now my challenge is to select the next record in PMS_PERF_D using the query analysis. Below
    is my query...
    SELECT GS_I_ID, PMS_COMP, GS_I_CODE, GS_WT_FR, GS_WT_TO, GS_I_CALC
    FROM dev_wt (
     SELECT lead(PMS_PERF_D) over(partition by PMS_I order by PMS_PERF_D) as GS_WT_TO        
     FROM dev_wt where PMS_I ='GICEQGROSS')
    left join dev_map on PMS_I = GS_CODE ;
    Thank you

    Edited by: WinZone 24 July 2010 16:46

    Edited by: WinZone 24 July 2010 16:50

    Hello

    This should be good:

    SELECT DISTINCT t2.gs_i_id, pms_comp, t2.gs_i_code,
                    TO_CHAR
                       (MIN (pms_perf_d) OVER (PARTITION BY pms_comp, pms_i),
                        'yyyymmdd'
                       ) gs_wt_fr,
                    TO_CHAR
                       (MAX (pms_perf_d) OVER (PARTITION BY pms_comp, pms_i) - 1,
                        'yyyymmdd'
                       ) gs_wt_to,
                    pms_wt / 100 gs_i_calc
               FROM dev_wt t1, dev_map t2
              WHERE t2.gs_code = t1.pms_i
    

    REM note even as odie: should be "2231" instead...

  • SQL Query Question campaign

    I have a table that has at least 40 values that I need to use as variables in CCA. My question is. If I write the query as follows:

    SELECT AS $Var1 of the value1, value2 AS $Var2, ETC... I do for 40 fields. Would it work if I just do a query where I

    SELECT * FROM DBNAME WHERE CALLER = "$ANI."

    and then simply use the column as variables names?

    If you need 40 variables in your campaign to act on and use in logic, then you must list and assign all 40 as your first example.

  • SQL query question, how to improve it?

    Hello everyone, I want to create a query with result in the following output. Each first-person Department starts with the letter A and goes to the top like this:

    Name Department
    A Person1 3
    B Person2 3
    C channels3 3
    D Individu4 3
    Person10 E 3

    A 10 Person6
    Person7 10 B
    Person8 C 10

    An Individu4 13
    B Person9 13


    It must be a SQL query, unfortunately no PL/SQL

    I was able to create this query, but its lack. Department_id will be a variable, a person can choose a Department or several departments, so the request varies in size.

    Select department_id, chr (64 + rownum)
    from (select name, department_id
    employees
    where department_id = 3
    order of id_but, naam
    )
    Union
    Select department_id, chr (64 + rownum)
    from (select name, department_id
    employees
    where department_id = 10
    order of id_but, naam
    )
    Union
    Select department_id, chr (64 + rownum)
    from (select name, department_id
    employees
    where department_id = 13
    order of id_but, naam
    )
    order of id_but, naam



    The employees table has the following columns:
    ID, name, department_id

    Can someone help me with this query? Perhaps with the With clause?

    Hello

    You can use the analytic ROW_NUMBER function instead of ROWNUM. Like all analytical functions, ROW_NUMBER can do a calculation separated for each unique value in the PARTITION BY clause:

    select        chr ( 64
               + ROW_NUMBER () OVER ( PARTITION BY  department_id
                             ORDER BY        id_but
                             ,             naam
                           )
               )     AS r_num
    ,        name
    ,       department_id
    from       employees
    where       department_id     IN (3, 10, 13)
    order by  id_but
    ,       naam
    ;
    

    If you do this in SQL * Plus, you can save for the departemnts user input (if it is a list separated by commas as ' 3, 10, 13', or a single value as "99") in a substitution variable (let's call it deptartment_id_list) and use the following WHERE clause:

    where       department_id     IN (&department_id_list)
    
  • PTC connector query questions

    I develop a connector and had some questions about the presentation of the petition. I tried to read the documentation and help, but saw no reference to these issues. I know that some of these things may not still have public API, but any help would be appreciated.

    (1) is it possible to disable the entry blank for a ComboBox and set one of the values? I am referring to when you click on the button '+' on a query and selecting a comboBox field. I tried the 'enableEmptyEntry' attribute but could do not seem to work.

    (2) is there a way to prevent several lines for the same domain? I'm trying to match a web interface that allows values separated by commas in certain areas and not others. Trying to decipher the intent of the user if they use multiple lines for the same field becomes confused.

    (3) is there a way to disable 'Match All' or 'match any '?

    Thank you

    After further review, the removal of the empty the box entry drop-down list on the query page is not possible.

    One feature that we've added in the last release to support the case where the user needs to find records with a null or empty value for this combobox field. For example, finding bugs without test status. We have generic for all drop-down list boxes.

    The 'enableEmptyEntry' attribute is intended to work with the attribute "emptyEntryValue" as a pair. The user can do something like this in def.xml:
    enableEmptyEntry = 'true '.
    "emptyEntryValue =""
    Our framework poster in the drop-down list instead of empty or the default value .

    We recorded a feature request for this for a future version. Currently, writer connector is not a way to customize our application page, including the MatchAll, MatchAny option.

  • where query question

    Ive HI you try to run a query that uses an apex link var to add a where clause per month on a date field

    Select print_month, to_char (PRINT_MONTH, 'Month') of print_schedule

    This query returns

    "PRINT_MONTH", "M".

    ' 01/12/2015', 'December '.

    ' 01/12/2015', 'December '.

    ' 01/10/2015', 'October '.

    ' 01/10/2015', 'October '.

    ' 01/08/2015', 'August '.

    Now then I try this query

    Select print_month, to_char (trunc (PRINT_MONTH), 'Month') of print_schedule where to_char (trunc (PRINT_MONTH), 'Month') = 'December '.

    I have some found data.

    The print_month field is a date column.

    Oracle 12 c

    HI, I fixed that I noticed when I opened the csv file in a text editor so that I could paste the result in the post that there are spaces at the end of month names string, so I added in the trunk as I thought that would fix it, but it didn't so I added now in a PLATE and it works

    Select print_month, RTRIM (to_char (trunc (PRINT_MONTH), 'Month')) from print_schedule where RTRIM (to_char (trunc (PRINT_MONTH), 'Month')) = "December".

    So I have some might explain why this is the case, why should I cut the tank a date version?

    Measure out why add white space on the end?

  • SQL Query Question of multiple criteria

    Hi all

    I currently have a query that provides me with a count for a criterion (C), but I also want to include the number of criteria (A) and (E) within the same query if possible. Here's what I have:

    SELECT B.MDA_LEV3_DEPT_NAME AS "TEU Area", AS COUNT (Z.MDA_EPAF_STATUS) PROJECT

    PS_MDA_EPAF_RQST Z, PS_MDA_EMPLOYEES B, PS_MDA_EPAF_WFXREF T

    WHERE MDA_EPAF_STATUS = 'C '.

    AND T.ORIGINATORID = Z.USERID

    AND Z.USERID = B.EMPLID

    AND Z.TRANSACTION_NBR = T.TRANSACTION_NBR

    B.MDA_LEV3_DEPT_NAME GROUP

    ORDER BY B.MDA_LEV3_DEPT_NAME;

    Which produces:

    Area of EVPProject Manager
    Office of VP University Affairs Sr5
    Cancer medicine30
    Cancer medicine - research49
    Cancer Prev & Pop science3
    Cancer Prev & science-Res Pop1
    Diagnostic Imaging2
    Information services16
    Internal medicine5
    Pathology/laboratory medicine2
    Pathology/laboratory medicine - Resch1
    Pediatrics13
    Provost and EVP departments1
    Radio-Oncology26
    Radio-Oncology - search2
    Regulatory Affairs1
    Surgery4
    Vice Provost research clinical1
    Vice Provost Science42

    But I want to understand (A) approved County and (E) which is pending County so it would look like:

    Area of EVPProject ManagerCounty approvedCounty pending
    Office of VP University Affairs Sr5
    Cancer medicine30
    Cancer medicine - research49
    Cancer Prev & Pop science3
    Cancer Prev & science-Res Pop1
    Diagnostic Imaging2
    Information services16
    Internal medicine5
    Pathology/laboratory medicine2
    Pathology/laboratory medicine - Resch1
    Pediatrics13
    Provost and EVP departments1
    Radio-Oncology26
    Radio-Oncology - search2
    Regulatory Affairs1
    Surgery4
    Vice Provost research clinical1
    Vice Provost Science42

    Maybe (guessing)

    SELECT B.MDA_LEV3_DEPT_NAME AS 'TEU Area. "

    PROJECT COUNT(CASE Z.MDA_EPAF_STATUS WHEN 'C' THEN 1 END) AS,

    COUNT(CASE Z.MDA_EPAF_STATUS WHEN 'A' THEN 1 END) APPROVED,

    AS COUNT(CASE Z.MDA_EPAF_STATUS WHEN 'P' THEN 1 END) PENDING

    OF PS_MDA_EPAF_RQST Z.

    B PS_MDA_EMPLOYEES,

    PS_MDA_EPAF_WFXREF T

    WHERE MDA_EPAF_STATUS ('C', 'A', 'P')

    AND T.ORIGINATORID = Z.USERID

    AND Z.USERID = B.EMPLID

    AND Z.TRANSACTION_NBR = T.TRANSACTION_NBR

    B.MDA_LEV3_DEPT_NAME GROUP

    ORDER BY B.MDA_LEV3_DEPT_NAME

    Concerning

    Etbin

  • SQL query question

    I have a table named Test, data inserted as below

    col1 Val
    There3
    x1
    z2

    We write a query to output below:

    col1 Val
    x1
    There2
    z3

    Please suggest the best way to reach above result.

    It is not clear what you're trying to do, but you can use something like:

    Select col1,

    ROW_NUMBER() (any order of col1) val

    of the test

    /

    For example:

    with the trial as)

    Select 'x' col1, 3 val union double all the

    Select 'y', 2 double Union all

    Select 'z', 1 double

    )

    Select col1,

    ROW_NUMBER() (any order of col1) val

    of the test

    /

    C VAL
    - ----------
    x          1
    y          2
    z          3

    SQL >

    SY.

  • Hierarchical query question

    Hi all

    I'm working on the database version (please avoid comments like "you should upgrade to a newer version," it's not me):

    Select * from version of v$.

    BANNER

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

    Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64 bit Production

    PL/SQL Release 9.2.0.8.0 - Production

    CORE Production 9.2.0.8.0

    AMT for HP - UX: 9.2.0.8.0 - Production Version

    NLSRTL Version 9.2.0.8.0 - Production

    I have the following hierarchical data:

    with mydata (emp_id, mgr_id, RESP) as

    (

    Select 1, null, 'Y' from dual union all

    Select 2, 1, "n" of all the double union

    Select option 3, 2, "n" of all the double union

    Select 4, 1, 'Y' from dual union all

    Select 5, 4, the double "n" of all union

    Select 6, 5, 'Y' from dual union all

    Select 7, 6, the double "n" of all union

    Select 8, 1, the double "n" of all union

    Select 9, 8, the double "n" of all union

    Select 10, 9, "Y" of the double

    )

    Select emp_id, mgr_id, RESP

    of mydata

    connect mgr_id = emp_id prior

    Start by emp_id = 1;

    EMP_ID MGR_ID RESP

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

    1            Y

    2 1 N

    3 2 N

    4 1

    5 4 N

    6 5

    7 6 N

    8 1 N

    9 8 N

    10 9 Y

    10 selected lines.

    Each employee has a Manager (self or the following in the upper hierarchy with RESP = 'Y')

    The root must be always charged.

    To find who is responsible for a certain employee ID, I can run the following query:

    I... responsible e: emp_id = 7 is 6

    Select emp_id, mgr_id, RESP

    of mydata

    When resp = 'Y '.

    and rownum < = 1

    Connect prior mgr_id = emp_id

    Start by emp_id = 7;

    EMP_ID

    ----------

    6

    1 selected line.

    i.e.: head = emp_id is 1/9

    Select emp_id

    of mydata

    When resp = 'Y '.

    and rownum < = 1

    Connect prior mgr_id = emp_id

    Start by emp_id = 9;

    EMP_ID

    ----------

    1

    1 selected line.

    i.e.: Manager emp_id = 4 is auto

    Select emp_id

    of mydata

    When resp = 'Y '.

    and rownum < = 1

    Connect prior mgr_id = emp_id

    Start by emp_id = 4;

    EMP_ID

    ----------

    4

    1 selected line.

    What query is the fastest to get all employees to a certain level of responsibility?

    That is to say: all employees not responsible according to 1 should give:

    EMP_ID MGR_ID RESP

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

    2 1 N

    3 2 N

    8 1 N

    9 8 N



    Kind regards.

    Alberto

    Hi, Alberto.

    So, you want to quit looking more far down the tree when you come to a node with an RESP = 'Y', right?

    Just include an RESP = ' don't (or resp. <> 'Y' or NVL (RESP, 'Y' <> 'Y') in the CONNECT BY State, like this:)

    Select emp_id, mgr_id, RESP

    of mydata

    connect mgr_id = emp_id prior

    and RESP = "n"-* NEW *.

    Start by emp_id = 1;

  • pivot query question

    I have a query in Oracle 11:
     select to_char(da,'DY-DD') days
    from (
            select rownum -1 + to_date('2012-10-15','yyyy-mm-dd') da, l
            from   (select level l
                    from   dual  connect by level <= to_date('2012-10-21','yyyy-mm-dd') - to_date('2012-10-15','yyyy-mm-dd')+1)
            order by 1) 
    Result
    "DAYS"
    MON-15
    TUE-16
    WED-17
    THU-18
    FRI-19
    SAT-20
    SUN-21
    What I need which is present in a row instead of a column:
     MON-15  TUE-16  WED-17  THU-18  FRI-19  SAT-20  SUN-21
    This should be done with pivot requests? Any suggestion please!
    Thanks in advance.

    Hello

    user564819 wrote:
    I need in 7 different columns.
    Oracle version is 11.2.0.1.0. Could you give an example of how to do this with pivot query?

    Here's one way:

    WITH     all_days     AS
    (
         SELECT     LEVEL  AS n
         ,     TO_CHAR ( TO_DATE ('2012-10-15','yyyy-mm-dd') + LEVEL - 1
                   , 'DY-DD'
                   )     AS dy_dd
         FROM    dual
         CONNECT BY  LEVEL <= TO_DATE ('2012-10-21','yyyy-mm-dd') + 1
                            - TO_DATE ('2012-10-15','yyyy-mm-dd')
    )
    SELECT     *
    FROM     all_days
    PIVOT     ( MIN (dy_dd)
           FOR  n  IN (  1  AS col_1
                          ,  2  AS col_2
                          ,  3  AS col_3
                          ,  4  AS col_4
                          ,  5  AS col_5
                          ,  6  AS col_6
                          ,  7  AS col_7
                          ,  8  AS col_8
                          ,  9  AS col_9
                          , 10  AS col_10
                   )
          )
    ;
    

    This always displays exactly 10 columns.
    If the subquery produces less than 10 dates, then the last columns will be NULL.
    If the subquery produces more than 10 dates, will be not shown the 11th (and later versions).

    There is nothing special about the number 10; You can have any number of columns you want. The number of columns and their names is fixed when you write the query, regardless of what is in the subquery. If you want a dynamic number of columns, or names derived from data, see {message identifier: = 3527823}

Maybe you are looking for