SQL query help needed in the Clause type

Hello

I'm currently learning Clause type. How can I write in the clause type to get the result below:

I have a table like this

DEPTNO ENAME

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

20 SMITH

CELINE 10

BOND 20

ALLEN 30

WARD 30

20 JONES

30 MARTIN

30 BLAKE

10 CLARK

SCOTT 20

10 KING

30 TURNER

20 ADAMS

30 JAMES

20 FORD

10 MILLER

I want the output should look like this

DEPTNO ENAME

------     -----

CELINE 10, CLARK, KING, MILLER

20 JONES, ADAMS, BOND, FORD, SCOTT, SMITH

30 MARTIN, JAMES BLAKE, WARD, TURNER, ALLEN

Hear is the script I am currently working on.

with emp_group as

(

Select 20 deptno, ename 'SMITH' Union double all the

choose 10, 'CÉLINE' from dual union all

Select 20, "BOND" of all the double union

Select 30, 'ALLEN' from dual union all

Select 30, 'WARD' of all the double union

Select 20, "JONES' from dual union all

Select 30, 'MARTIN' from dual union all

Select 30, "BLAKE" from dual union all

choose 10, 'CLARK' from dual union all

Select 20, 'SCOTT' from dual union all

choose 10, 'KING' of the dual union all

Select 30, 'TURNER' from dual union all

Select 20, 'ADAMS' from dual union all

Select 30, 'JAMES' of the dual union all

Select 20, 'FORD' Union double all the

choose 10, 'MILLER' from dual

)

Select

DEPTNO

A1

of emp_group

model

ignore the nav

partition (deptno)

dimension (ROW_NUMBER() over (PARTITION BY deptno ORDER BY ename DESC) rn)

measures (ename, LPAD(' ', 100) A1)

rules)

A1 [rn > 0] = ename [cv ()],

A1 [0] = ename [cv ()]

)

order by deptno

;

your help is very appreciated.

Thank you in advance.

Try this.

Select

*

Of

(

with emp_group as

(

Select 20 deptno, ename 'SMITH' Union double all the

choose 10, 'CÉLINE' from dual union all

Select 20, "BOND" of all the double union

Select 30, 'ALLEN' from dual union all

Select 30, 'WARD' of all the double union

Select 20, "JONES' from dual union all

Select 30, 'MARTIN' from dual union all

Select 30, "BLAKE" from dual union all

choose 10, 'CLARK' from dual union all

Select 20, 'SCOTT' from dual union all

choose 10, 'KING' of the dual union all

Select 30, 'TURNER' from dual union all

Select 20, 'ADAMS' from dual union all

Select 30, 'JAMES' of the dual union all

Select 20, 'FORD' Union double all the

choose 10, 'MILLER' from dual

)

Select

DEPTNO

names

rn

of emp_group

model

ignore the nav

partition (deptno)

dimension of)

ROW_NUMBER() over (PARTITION BY deptno ORDER BY ename) rn

)

measures (ename, cast (null as names of varchar2 (40)))

rules

(

names [any] order by desc = ename [cv ()] rn | «, » || names [cv () + 1]

)

order by deptno

)

where rn = 1

Best regards, Andrei

Tags: Oracle Development

Similar Questions

  • SQL query help (we connect by clause level)

    Hi all
    I have this application developed with data with the clause.
     With dat As
    (
      select '@AAA @SSS @DDD' col1 from dual union all
      select '@ZZZ @XXX @TTT @RRR @ZZA' col1 from dual 
    )
    Select regexp_substr( col1 , '[^@][A-Z]+',1,level) Show from dat
    connect by level  <= regexp_count(col1, '@');
    Output current: -.
    SHOW
    -----------------------
    AAA
    SSS
    DDD
    RRR
    ZZA
    TTT
    RRR
    ZZA
    XXX
    DDD
    RRR
    
    SHOW
    -----------------------
    ZZA
    TTT
    RRR
    ZZA
    . . .
    . . .
    1st row comes very well, but the next line data copy. And the number of total records = 30. I tried with some, but not worked.
    Expected results: -.
    SHOW
    -----------------------
    AAA
    SSS
    DDD
    ZZZ 
    XXX 
    TTT 
    RRR 
    ZZA
    I need some changes on my request and I am not able to see that. So anyone can add to that or can also provide a different solution also.

    Thank you!
    Ashutosh

    Thanks for providing the loan to the use of query. :)

    Here's a solution :-(tested on 10 g, do not have 11 g at hand)

    For 11g, just use regexp_count instead of functions of the length.

    With dat As
    (
      select '@AAA @SSS @DDD' col from dual union all
      select '@ZZZ @XXX @TTT @RRR @ZZA' col1 from dual
    )
    Select regexp_substr( col, '[^@][A-Z]+',1,level) Show from dat
    connect by nocycle level  <= length(col) - length(translate(col, 'A@', 'A'))
           and col = prior col
           and prior sys_guid() is not null;
    
    SHOW
    ------------------------
    AAA
    SSS
    DDD
    ZZZ
    XXX
    TTT
    RRR
    ZZA                      
    
     8 rows selected
    
  • on the Clause type

    Hi all
    I read some documents oracle and and some examples
    I understand that this model is used to create the table multidimensional and for the prediction of the future.

    but I am not able to write the query using the type clause.
    Please someone help me to understand the clause type base.
    I want everything clause type to start from scratch.

    Please guide me.

    Links: http://rwijk.blogspot.in/2007/10/sql-model-clause-tutorial-part-one.html.
    http://rwijk.blogspot.in/2007/10/SQL-model-clause-tutorial-part-two.html

    There is a post over here in the OTN forum. Please do a search.

    Concerning
    Biju

    Edited by: biju2012 1 Sep 2012 16:02
    (The link for part 2 is provided)

    Published by: biju2012 on Sep 2, 2012 02:45

  • need help on the clause type

    I have a temp table as below

    LOC X CNT

    20 20 C

    80 20 N

    60 30 C

    40 30 N

    100 40 N

    the rule is for each column of the LOC, there are 2 records of C, N and sum (NTC) must be = 100. If there is no value for C for a LOC (LOC = 40) then the value of the CNT corresponding to C should appear as null or 0 if the value of N is > 0.

    I need a model clause to do this, see below,

    LOC X CNT

    20 20 C

    80 20 N

    60 30 C

    40 30 N

    100 40 N

    0 TO 40 C



    Why why why? I am not able to understand the model clause. How did you learn model clause?

  • SQL query for data of the quarter... Help, please

    Dear Experts,

    Please help with this query...

    I have data as below:

    YearQuarterMRRMORR %
    -------------------
    20121ST QUARTER10
    Q230
    Q350
    Q460
    20131ST QUARTER20
    Q230

    Now I need to caluclate MORR % as column values below

    for 2012 T2 MRR % = (Q2MRR-Q1MRR) / (30-10) Q1 * 100 / 10 * 100

    for 2012, T3 MRR % = (Q4MRR-Q3MRR) / Q2 * 100

    for 2012 T4 MRR % = (Q1MRR-Q4MRR) / Q1 * 100 here q1 2013 and the 4th quarter of 2012...

    like this he wll go...

    Please help with me query calculate values in the column % MRR.

    Best regards

    ASP.

    Maybe NOT TESTED! No database at your fingertips

    Select the year, quarter, mrr,.

    ratio_to_report (q_mrr) on pct_mrr (partition by order of the year by quarter)

    (select the year, quarter, mrr,)

    MRR - lag (mrr, 1, 0) on q_mrr (partition by order of the year by quarter)

    t

    )

    Concerning

    Etbin

  • Need HELP to change the NAT type to open on Linksys E2000.

    I was wondering if anyone could tell me how to change my nat from moderate to open so I can play xbox without any problems. But the strange thing is my nat was open, but it changed to moderate and I recently bought the linksys e2000 and it was open at first, but no more. If anyone can help me?

    Follow the below mentioned settings, then check.

    Open an Internet Explorer browser on your computer (desktop) wired page. In the address bar type - 192.168.1.1 and press ENTER...
    Let the empty user name & password use admin lowercase...
    On the Configuration tab change the size of the MTU to 1365, then click on save settings...
    Click the 'Administration' tab and disable the UPnP option and click on save settings...
    Click on the tab "Games and Applications" and then click the sub-tab "Port Range Forwarding"...
    (1) on the first line in the box, type Application in ABC, in the start box, type in 53 and type in 3074 service box, leave the Protocol as and under type 192.168.1.20 ip address and check the box to enable, click on save settings once it's been...
    (2) once you return to the game to the top page, click the Security tab and uncheck block anonymous Internet requests and click on save settings...
    (3) click on the status tab, and then note the DNS1 and DNS2 addresses...
    (4) address IP, Goto settings XBox network settings and assign the following on your Xbox and select manual IP settings
    IP address:-192.168.1.20, subnet mask:-255.255.255.0 default gateway:-192.168.1.1...
    (5) also assign addresses DNS on Xbox
    Use DNS1 and DNS2 addresses you took note of the primary router as secondary DNS & DNS status tab for the xbox...
    (6) turn off your modem, router and Xbox... Wait a minute...
    (7) plug the power to the modem first, wait a minute and plug the router power cable, wait another minute and turn on the Xbox and... test it connects...
    IP address: part 192.168.X. [last intellectual property in your device] for example if static ip given to the unit's 192.168.X.10 get the last part and put it in the empty box.

  • How to use the clause type conditionally create new lines

    This question is just for learning - no object other than to start real world to create versions of some queries known to learn how to measure the performance of the MODEL clause clause of the MODEL versions.

    The question is: How can you write a STANDARD clause that pivots (updates) ONLY the lines that actually need to rotate and do NOT change the lines that have no value and cannot be rotated.

    I found a good site that has about two dozen example articles on different pieces. This first link is the beginning of the series.

    SQL features tutorials - Clause TYPE

    http://www.sqlsnippets.com/en/topic-11663.html

    This link is to a clause of FAQ of BluShadow MODEL version "how to convert rows to columns."

    Line - MODEL method string

    http://www.sqlsnippets.com/en/topic-11987.html

    The solution to this link uses this line of source:

    POSITION KEY VAL

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

    R08 0 v1, v2, v3,.

    and this result set:

    POSITION KEY VAL

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

    R08 1 v1

    R08 2 v2

    R08 3 v3

    The reason why it produces that result set is this clause of the statement of MODEL

    RETURN THE UPDATED LINES

    If you comment on this article, you will see that the original lines are all in position 0. This means that the solution reproduced EACH SOURCE LINE even if there is only one item in the list of values.

    Thus, even a line source with a single value (for example, the r01 'a') will be updated and updated this line is returned.

    For a large number of data sources with only a few lines that actually need to rotate it would be a great performance of infringement.

    This is the sample data source

    with t as (button 1, 'a' value 'abc' col2, col3 'def' of the double
    Select Union all 2, 'b', 'ghi', 'jkl' from dual
    Union all select 3, 'c, d, e, 'mno', 'pqr' from dual.
    Union all select 4, 'f', 'stu', 'vwx' from dual
    )
    Select the key, value, col2, col3
    t

    KEY, VALUE, COL2, COL3
    1, a, abc, def
    2, b, GHI, jkl
    3, "c, d, e, mno, pqr.
    4, f, stu, vwx

    Lines 1, 2 and 4 only have a value of (a, b, c respectively.

    Only line 3 must rotate. It contains "c, d, e" and which must become 3 lines in the result set

    Value of the key, col2 col3
    1 an abc def
    2B ghi jkl
    3 c mno, pqr
    3 d mno, pqr
    3 e mno, pqr
    4 f stu vwx

    The nut of the problem is that if the clause 'RETURN UPDATED ROWS' is used, then the lines 1, 2 and 4 must be updated in order to be returned in the result set. And if this clause is NOT used, then line 3 must be updated by 'c', d, e 'c' and two new product lines: one line for a ' and one for 'e '.

    How can you do this with the TYPE clause without the help of the clause "Return LINES UPDATE"? I don't have a solution to sample showing what I tried because I can't understand what it takes to even try.

    I have a solution that uses "RETURN UPDATED ROWS" but I want to compare this performance to the exercise, when this clause is NOT used.

    We ask BluShadow to add the solution of MODEL clause to this FAQ with other similar solutions.

    Like this?

    SQL > with t as
    () 2
    3 select the 1 key, 'a' val, 'abc' col2, col3 'def' of the double


    Select 4 Union all 2, 'b', 'ghi', 'jkl' from dual
    5 union all select 3, 'c, d, e, 'mno', 'pqr' from dual.
    Select 6 Union all the 4 'f', 'stu', 'vwx' from dual
    7)
    8. Select the key
    9, key_1
    10, val
    11, regexp_substr (val, ' [^,] +', 1, key_1) val_new
    12, col2
    13, col3
    14 t
    model 15
    16 partition by (key)
    dimension (1 key_1) 17
    18 measures (val, col2, col3, (length (val) - length (replace (val, ",")) + 1) as len)
    19 rules
    (20)
    21 val [for 1 to increment of len key_1 [1] 1] = val [1]
    22, col2 [for 1 to increment of len key_1 [1] 1] = col2 [1]
    23, col3 [for 1 to increment of len key_1 [1] 1] = col3 [1]
    24)
    25 order
    26 by key
    27, key_1;

    KEY KEY_1 VAL VAL_N NECK NECK
    ---------- ---------- ----- ----- --- ---
    1 1 has an abc def
    2 b 1 b ghi jkl
    3 1 c, d, e c mno pqr
    3 2 c, d, e d mno pqr
    3 3 c, d, e e mno pqr
    4 1 f f stu vwx

    6 selected lines.

  • Cell in the clause type assignments

    Hello

    I have a table TEST_DATA with the YEAR, FIELD VALUE, in the form of columns.
    Value1, value2, VALUE 3 columns are determined based on field values. If the data is as follows

    THE YEAR FIELD VALUE
    ------------------------------------------
    11111 1 2010
    22222 2 2010
    33333 3 2010
    . . .
    . . .
    . . .
    Can I get the following output using clause type.

    YEAR VALUE1 VALUE2 VALUE3
    ------ ----------- ----------- -----------
    2010 11111 222222 333333
    2009 23243 533635 343434
    2008--34343 364653 343434
    2007 34345 789879 343434

    I don't want to use pivot, because I have to do some calculations on columns of value above. So I tried the following query using the type clause
    and value1, value2, value3 to come in 3 different lines.
    How can I write cell assignments for the value 1, a value of 2, the value 3 in the same line?

    SELECT YEAR, VALUE OF 1, THE VALUE 2, VALUE 3
    OF TEST_FIN_DATA
    MODEL
    RETURN THE UPDATED LINES
    Single KEY
    PARTITION OF (YEAR)
    DIMENSION BY (FIELD)
    MEASURES (VALUE1, VALUE VALUE2, VALUE3 VALUE VALUE)
    RULES
    (value1 ['1'] = value1 ['1'],)
    value [('2 '] = value ['2'],
    value ['3'] = value ['3']);

    Here is an example that you might find interesting:
    http://technology.AMIS.nl/2004/12/16/pivoting-in-SQL-using-the-10G-model-clause/

  • Production PL/SQL query: can not find the error

    I have a PL/SQL which produces a query for me, which is used to fill out a report. I use this to supply a filter/search form. Since I want to only filter using xxx movies WHERE = yyy when yyy actually has something else NULL, I use a PL/SQL script in the query I need to produce. However, it does not work as expected and I can't understand why. Here is my code:
    DECLARE
      query VARCHAR(1000);
    BEGIN
      query := 'SELECT * FROM F_OBJEKTE WHERE 1=1 ';
      IF :P15_REGION != null THEN
        query := query || 'AND REGION = :P15_REGION ';
      END IF; 
      RETURN query;
    END;
    However, the report still contains all lines, as if P15_REGION has not been defined - and so I guess that the script never enters the IF part. Everyone around who can enlighten me, why it is?

    Just another little thing: is there a way I can print debugging messages I see when I press the "Debug" button on the lower bar of APEX? It would help a lot of debugging such PL/SQL functions.

    Try changing

    IF: P15_REGION! = null THEN

    TO

    IF: P15_REGION is not null THEN

  • SQL Query Help (not working not properly)

    Hello everyone,

    I use JDeveloper 12.1.2.0.0. I do a two-way communication using 3 tables, with links between them (and using schema HR).

    In my example, I have something like:

    Departments, employees, and the SalaryByJobs (I created this table where it shows a departmentd id, employee id, salary).

    Whenever I click on one OR more departments, the employees up-to-date table by putting on the table, employees who belong to the selected department and the salaryByjob to put the jobs of the employees selected on the employees table.

    So it's something like this:

    The departments selected-> employees selected-> salarybyjbobs of these employees.

    My query used (in the view) has the following code:

    SELECT Employees.COMMISSION_PCT,

    Employees.DEPARTMENT_ID,

    Employees.EMAIL,

    Employees.EMPLOYEE_ID,

    Employees.FIRST_NAME,

    Employees.HIRE_DATE,

    Employees.JOB_ID,

    Employees.LAST_NAME,

    Employees.MANAGER_ID,

    Employees.PHONE_NUMBER,

    Employees.SALARY

    Employees EMPLOYEES

    WHERE (department_id IN (select * from THE (select cast (in_list(:variavel3) as mytableType) double) a))

    Since I use the links, the employees table does not show anything at the beginning, so I added this to my query used to go: OR nvl(:variavel3,0) = 0

    But now, whenever I try to select multiple lines, it gives me the invalid numbers and I don't understand why...

    It's only one line of code and it is not in the bean.

    Can someone help me?

    PS - The bean will Department by Department, adds the departments with ',' and for each Department, gets employees who belongs to them.

    My best regards,

    Frederico Barracha.

    The expression NVL (: variavel3, 0) = 0 is not correct. The data type of the return value of the NVL function is considered to be equal to the data type of the argument of 1 (that is, the data type of the variable binding: variavel3). You said that this variable contained a list separated by commas to ID, so the data type of the variable is VARCHAR2. As long as you compare the NVL expression of a number, you get 'Invalid number' exception, because Oracle expects a numeric data type on the left side of the comparison operator.

    To avoid the exception "Invalid number", you can modify the expression by using one of the following options:

    : variavel3 IS NULL

    NVL (: variavel3, ' *') = ' *'

    NVL (: variavel3, ' 0') = '0'

    Option 1, so the simplest and clearest.

    Dimitar

  • PL/SQL Query return function, adding the column links

    Hi all

    I'm working on a SQL report with the area type = function from PL/SQL Query to return
    All columns are generated dynamically based on my mapping table and a column of ID.
    So whenever the page is loaded, according to the ID mapping table returns a set of columns in a particular order.
    So not only the columns are dynamic, but the order of the columns varies also.
    But the first 1 column is standard (it doesn't come from the mapping table) and is coded in my pl/sql block hard.

    I'm fighting with the addition of a link to this column. The link must be a Javascript function which takes the parameters in the form of 2 columns in the mapping table.
    My pl/sql block is something like that...
    v_select := 'SELECT <g href=javascript:f_report1(#map_id#,#comments#)><img src=""></a> as report1, ';  // g=a
    v_cols := 'contains all columns from the mapping table based on a ID(hidden item)';
    v_from := 'FROM table';
    v_where := 'where condition is put in here';
    v_query := v_select||v_from||v_where;
    return v_query;
    Now, I'm not able to transmit the values of this #map_id # and #comments # correctly. I tried so many different combinations of channels, but could not make it work.

    I'm not sure that the order of this map_id and commentscolumn and therefore cannot connect through the report attributes.
    How can I go to a column value to the function?



    Thank you
    Dippy

    This should work if all goes well:

    v_select := q'[ SELECT '' as chart, ]';
    

    Published by: Dimitri Gielis on May 20, 2010 20:10

  • Help needed on the BIC


    Hi team,

    Please help on the below questions on BICS.

    1. How do I delete records that has been loaded into the DATABASE LOADER?

    2. how to remove the database tables in DATA MODELER?

    3. how change the data type varchar to Date format in the DATA LOADER loading data?  I can see only the number and the type varchar.

    4. after the publication of the data model with success, I can create visual reports. If I change the data model for all the changes, the join conditions are go (missing persons) automatically. Don't know why its happening. I have to recreate the model again for this.

    Please provide documentation for the same if no.

    Thank you

    Malli

    At the beginning of the end, the tabs are domain names when loading data using the data loader fixed the issue with the knuckles that disappears into the data model.

    We can delete, create, and can change the formats of data using Oracle Apex for BICS.

    Thank you

    Malli

  • Dynamic SQL with in bulk in the record type

    Oracle 10.2 g

    I received this Tom

    [http://asktom.oracle.com/pls/apex/f?p=100:11:0:NO:]

    I'm able to do this without dynamic SQL, but my requirement is to do it in dynamic SQL
     create table t1 ( x int, y int );
    
     insert into t1 select rownum, rownum+1 from all_users where rownum <= 5;
    
     create table t2 ( x int, y int, z int );
    
     declare
                type array is table of t1%rowtype;
                l_data array;
        begin
                select * bulk collect into l_data from t1;
      
                forall i in 1 .. l_data.count
                
                       execute immediate 'insert into (select x, y from t2) values :x' using l_data(i);
        end;
     
    Error at line 1
    ORA-06550: line 9, column 90:
    PLS-00457: expressions have to be of SQL types
    ORA-06550: line 9, column 20:
    PL/SQL: Statement ignored
    There is a work around in 11g, but can we do something in 10g?



    Thank you
    HESH.

    HESH wrote:

    but following does not.

    declare
    type array is table of t1%rowtype;
    l_data array;
    begin
    select * bulk collect into l_data from t1;
    
    forall i in 1 .. l_data.count
    
    execute immediate 'insert into (select x, y from t2) values :x' using l_data(i);
    end;
    

    I want just a dynamic SQL code for the insert with FORALL statement would adopt as well as collections.

    Doesn't make much sense.

    Extract you the data from the SQL engine in the table of the record type. If the output data that cursor SQL must be read in the SQL engine and copied into the memory of PL/SQL engine.

    Then, you send that VERY SAME DATA back to the SQL engine to be used by a SQL insert cursor.

    Where is the logic behind the extraction of data from SQL in a PL/SQL table structure and then push this same structure table on the SQL engine database? What is the purpose to send data on a detour of underperforming and non-scalale through the PL/SQL engine?

    You have any justification (technical or functional wise) to back up this absurd approach?

    Why this can be achieved using a single SQL cursor that does both the choice (extraction) and (in bulk) insertion - using the plain old INSERT... SELECT structure?

    And if the insert is variable, then what? Create a dynamic INSERT... SELECT cursor and execute it (using bind values). This simple... Right?

  • Help needed in the management LR5.7 and color

    LR5.7 in the print module - the printer settings - I cannotget predefined a pop up screen under the settings tab to go to advanced or anywhere else, so I can't disable the printer management. I want to activate the icc profiles print from LR

    What is your OS and the brand/type of printer?

  • SQL query to dynamically filter the records-need help

    Hello

    I have a table with the structure as below

    Create table T1
    (Number (5) ID,)
    Action Varchar2 (20)
    )


    Here's the table of contents at different points in time and please help me with a query that must dynamically discover the results according to the entries in the table.

    First Table contents

    1 pending
    2 waiting


    Result of the query must be

    1 pending
    2 waiting


    After an insert the Table of contents

    1 pending
    2 waiting
    3 ignored

    Result

    Nothing should be displayed


    After an insert the Table of contents
    1 pending
    2 waiting
    3 ignored
    4 ignored

    Result
    Nothing should be displayed

    After an insert the Table of contents
    1 pending
    2 waiting
    3 ignored
    4 ignored
    5 in queue

    Result
    5 in queue

    Thanks in advance!

    Best regards
    Sridhar

    Hi, Sridhar,

    So you want to show t1 lines that come after the last row with action = "Ignored", which means no output at all during the last row has action = "ignored". (A line with a given id is considered as ' after' a line with a lower id. The line with the highest id is considered the "last" line.)
    Is this fair?

    Here's one way:

    SELECT     *
    FROM     t1
    WHERE     id  > (
              SELECT  MAX (id)
              FROM     t1
              WHERE     action     = 'Ignored'
               )
    ;
    

    It's not assume this id is whole consecutive.

    If the id is not unique, what results do you want? There is a chance that the above query is already doing if this is not the case, it can probably be changed.

Maybe you are looking for