Rank within a group based on Total, start it over 1 in each group

Hello PL/SQL gurus and experts.


I use Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64-bit Production version
I have table-

Like this??

SELECT Pat_NM,

Hospital,

Test_Range,

SUM (Total),

DENSE_RANK)

COURSES (PARTITION BY test_range | pat_nm ORDER OF SUM (Total) / / DESC)

IN THE RANK,.

ROW_NUMBER)

COURSES (PARTITION BY test_range | pat_nm ORDER OF SUM (Total) / / DESC)

AS rk

THE t2

After HAVING Pat_NM = 'Andy '.

GROUP OF Pat_NM, hospital, Test_Range;

See you soon,.

Manik.

Tags: Database

Similar Questions

  • Restart rank comes not correct based on column group

    Hello PL/SQL gurus and experts.


    I use Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64-bit Production version
    I have table-

    drop table t2;
    create table t2(Hospital,Test_Range,Total) as select
    'Batra','> 10 Mph','20000' from dual union all select
    'Fortis','1-3 Mph','24500' from dual union all select
    'Max','5-10 Mph','10600' from dual union all select
    'Columbia','< 1 Mph','27700' from dual union all select
    'Nimhans','< 1 Mph','50000' from dual union all select
    'Meenam','< 1 Mph','11000' from dual union all select
    'Meeran','5-10 Mph','24625' from dual union all select
    'Mnagamani','> 10 Mph','12000' from dual union all select
    'Murari','> 10 Mph','20600' from dual union all select
    'Triveni','5-10 Mph','16500' from dual union all select
    'Batra','5-10 Mph','14700' from dual union all select
    'Max','< 1 Mph','170000' from dual union all select
    'Apollo Medical Centre','> 10 Mph','19000' from dual union all select
    'MLal','1-3 Mph','22600' from dual union all select    
    'Columbia','< 1 Mph','28900' from dual union all select
    'Asian','1-3 Mph','27900' from dual union all select
    'A.G.M.','< 1 Mph','22700' from dual union all select
    'Aashiana','5-10 Mph','23450' from dual union all select
    'Amar Hospital','1-3 Mph','21325' from dual union all select
    'Childs Trust','5-10 Mph','22775' from dual union all select
    'Crescent ','< 1 Mph','20025' from dual;
    

    I use the following DML-

    select Test_Range "Test_Range",
    SUM (Total) "Total",
    decode(grouping(Hospital), 0, Hospital, 'Total') "Hospital",
    decode(grouping(Hospital), 0, max(rank), null) Rank
    from
    (
    SELECT Hospital,
             Test_Range,
             SUM (Total) Total,
             DENSE_RANK ()
                OVER (PARTITION BY test_range || Hospital ORDER BY SUM (Total) DESC)
                AS RANK,
             ROW_NUMBER ()
                OVER (PARTITION BY test_range || Hospital ORDER BY SUM (Total) DESC)
                AS rk
        FROM t2
    GROUP BY Hospital, Test_Range
    )
    group by grouping sets((Hospital, Test_Range),())
    order by Test_Range, Rank;
    

    Get the following output-

    Test_Ran      Total Hospital                    RANK
    -------- ---------- --------------------- ----------
    1-3 Mph       24500 Fortis                         1
    1-3 Mph       21325 Amar Hospital                  1
    1-3 Mph       22600 MLal                           1
    1-3 Mph       27900 Asian                          1
    5-10 Mph      14700 Batra                          1
    5-10 Mph      22775 Childs Trust                   1
    5-10 Mph      10600 Max                            1
    5-10 Mph      23450 Aashiana                       1
    5-10 Mph      16500 Triveni                        1
    5-10 Mph      24625 Meeran                         1
    < 1 Mph       20025 Crescent                       1
    Test_Ran      Total Hospital                    RANK
    -------- ---------- --------------------- ----------
    < 1 Mph       50000 Nimhans                        1
    < 1 Mph       56600 Columbia                       1
    < 1 Mph       11000 Meenam                         1
    < 1 Mph      170000 Max                            1
    < 1 Mph       22700 A.G.M.                         1
    > 10 Mph      12000 Mnagamani                      1
    > 10 Mph      20000 Batra                          1
    > 10 Mph      20600 Murari                         1
    > 10 Mph      19000 Apollo Medical Centre          1
                 610900 Total
    21 rows selected.
    

    While I am looking for following output-

    Test_Ran      Total Hospital                    RANK
    -------- ---------- --------------------- ----------
    1-3 Mph       27900 Asian                          1
    1-3 Mph       24500 Fortis                         2
    1-3 Mph       22600 MLal                           3
    1-3 Mph       21325 Amar Hospital                  4
    5-10 Mph      24625 Meeran                         1
    5-10 Mph      23450 Aashiana                       2
    5-10 Mph      22775 Childs Trust                   3
    5-10 Mph      16500 Triveni                        4
    5-10 Mph      14700 Batra                          5
    5-10 Mph      10600 Max                            6
    < 1 Mph      170000 Max                            1
    < 1 Mph       56600 Columbia                       2
    < 1 Mph       50000 Nimhans                        3
    Test_Ran      Total Hospital                    RANK
    -------- ---------- --------------------- ----------
    < 1 Mph       22700 A.G.M.                         4
    < 1 Mph       20025 Crescent                       5
    < 1 Mph       11000 Meenam                         6
    > 10 Mph      20600 Murari                         1
    > 10 Mph      20000 Batra                          2
    > 10 Mph      19000 Apollo Medical Centre          3
    > 10 Mph      12000 Mnagamani                      4
                 610900 Total
    21 rows selected.
    

    Kindly help me and thanks in advance for your time, effort and assistance

    Hello

    If you want a separate set of numbers (1, 2, 3,...) for each value of test_range, without seeking to hold account of the hospital, then the PARTITON OF only test_range, not to mention the hospital.

  • How to get the subtotal and top 3 discs only within a group

    Hello PL/SQL gurus and experts.


    I use Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64-bit Production version
    I have table-

    drop table t2;
    create table t2(Pat_NM,Hospital,Test_Range,Total) as select
    'Andy','Batra','> 10 Mph','20000' from dual union all select
    'Andy','Fortis','1-3 Mph','24500' from dual union all select
    'Andy','Max','5-10 Mph','10600' from dual union all select
    'Andy','Max','5-10 Mph','22500' from dual union all select
    'Andy','Aashiana','5-10 Mph','110600' from dual union all select
    'Andy','Amar','5-10 Mph','34800' from dual union all select
    'Andy','Max','5-10 Mph','600' from dual union all select
    'Andy','Columbia','< 1 Mph','27700' from dual union all select
    'Andy','Nimhans','< 1 Mph','50000' from dual union all select
    'Andy','Meenam','< 1 Mph','11000' from dual union all select
    'Andy','Meeran','5-10 Mph','24625' from dual union all select
    'Andy','Mnagamani','> 10 Mph','12000' from dual union all select
    'Andy','Murari','> 10 Mph','20600' from dual union all select
    'Andy','Triveni','5-10 Mph','16500' from dual union all select
    'Cindy','Batra','5-10 Mph','14700' from dual union all select
    'Cindy','Max','< 1 Mph','170000' from dual union all select
    'Cindy','Apollo Medical Centre','> 10 Mph','19000' from dual union all select
    'Cindy','MLal','1-3 Mph','22600' from dual union all select    
    'Cindy','Columbia','< 1 Mph','28900' from dual union all select
    'Cindy','Asian','1-3 Mph','27900' from dual union all select
    'Cindy','Mahagun','< 1 Mph','28700' from dual union all select
    'Cindy','Manipal','< 1 Mph','29040' from dual union all select
    'Cindy','Prestige','< 1 Mph','12700' from dual union all select
    'Cindy','A.G.M.','< 1 Mph','97800' from dual union all select
    'Cindy','Shobha','< 1 Mph','700' from dual union all select
    'Cindy','Aashiana','5-10 Mph','23450' from dual union all select
    'Cindy','Amar','1-3 Mph','21325' from dual union all select
    'Cindy','Childs Trust','5-10 Mph','22775' from dual union all select
    'Cindy','Crescent ','< 1 Mph','20025' from dual;
    

    I need to extract the data based on the condition that-
    In each Test_Range get the subtotal of all the records and display only the top 3 hospitals
    and ultimately make the total general.

    For this I use the following query-

    select Pat_NM,
    Hospital,
    SUM (Total) "Total",
    decode(grouping(Test_Range), 0, Test_Range, 'Total') "Test Range",
    decode(grouping(Test_Range), 0, max(rank), null) Rank
    from
    (
    SELECT Pat_NM,
             Hospital,
             Test_Range,
             SUM (Total) Total,
             DENSE_RANK ()
                OVER (PARTITION BY test_range || pat_nm ORDER BY SUM (Total) DESC)
                AS RANK,
             ROW_NUMBER ()
                OVER (PARTITION BY test_range || pat_nm ORDER BY SUM (Total) DESC)
                AS rk
        FROM t2
    GROUP BY Pat_NM, Hospital, Test_Range
    )
    where rank <=3
    group by grouping sets((Pat_NM, Hospital, Test_Range),())
    order by Pat_NM,Test_Range, Rank;
    


    The result is I'm getting is -.

    PAT_N Test Ran HOSPITAL                   Total       RANK
    ----- -------- --------------------- ---------- ----------
    Andy  1-3 Mph  Fortis                     24500          1
    Andy  5-10 Mph Aashiana                  110600          1
    Andy  5-10 Mph Amar                       34800          2
    Andy  5-10 Mph Max                        33700          3
    Andy  < 1 Mph  Nimhans                    50000          1
    Andy  < 1 Mph  Columbia                   27700          2
    Andy  < 1 Mph  Meenam                     11000          3
    Andy  > 10 Mph Murari                     20600          1
    Andy  > 10 Mph Batra                      20000          2
    Andy  > 10 Mph Mnagamani                  12000          3
    Cindy 1-3 Mph  Asian                      27900          1
    PAT_N Test Ran HOSPITAL                   Total       RANK
    ----- -------- --------------------- ---------- ----------
    Cindy 1-3 Mph  MLal                       22600          2
    Cindy 1-3 Mph  Amar                       21325          3
    Cindy 5-10 Mph Aashiana                   23450          1
    Cindy 5-10 Mph Childs Trust               22775          2
    Cindy 5-10 Mph Batra                      14700          3
    Cindy < 1 Mph  Max                       170000          1
    Cindy < 1 Mph  A.G.M.                     97800          2
    Cindy < 1 Mph  Manipal                    29040          3
    Cindy > 10 Mph Apollo Medical Centre      19000          1
          Total                              793490
    21 rows selected.
    

    While I'm looking forward to a result like -

    PAT_N Test Ran HOSPITAL                   Total       RANK
    ----- -------- --------------------- ---------- ----------
    Andy  1-3 Mph  Fortis                     24500          1
          Subtotal       24500
    Andy  5-10 Mph Aashiana                  110600          1
    Andy  5-10 Mph Amar                       34800          2
    Andy  5-10 Mph Max                        33700          3
          Subtotal       220225
    Andy  < 1 Mph  Nimhans                    50000          1
    Andy  < 1 Mph  Columbia                   27700          2
    Andy  < 1 Mph  Meenam                     11000          3
          Subtotal       88700
    Andy  > 10 Mph Murari                     20600          1
    Andy  > 10 Mph Batra                      20000          2
    PAT_N Test Ran HOSPITAL                   Total       RANK
    ----- -------- --------------------- ---------- ----------
    Andy  > 10 Mph Mnagamani                  12000          3
          Subtotal       52600
    Cindy 1-3 Mph  Asian                      27900          1
    Cindy 1-3 Mph  MLal                       22600          2
    Cindy 1-3 Mph  Amar                       21325          3
          Subtotal       71825
    Cindy 5-10 Mph Aashiana                   23450          1
    Cindy 5-10 Mph Childs Trust               22775          2
    Cindy 5-10 Mph Batra                      14700          3
          Subtotal       60925
    Cindy < 1 Mph  Max                       170000          1
    Cindy < 1 Mph  A.G.M.                     97800          2
    Cindy < 1 Mph  Manipal                    29040          3
          Subtotal      387865
    PAT_N Test Ran HOSPITAL                   Total       RANK
    ----- -------- --------------------- ---------- ----------
    Cindy > 10 Mph Apollo Medical Centre      19000          1
          Subtotal       19000
          Total                              925640
    29 rows selected.
    


    I thank you in advance for your valuable time and effort.

    Just move the position of the filter level upward.

    SQL > select *.
    2 starting at)
    3. Select pat_nm
    4, hospital
    5, sum (total) 'total '.
    6, decode (grouping (test_range) + grouping (hospital), test_range, 0, 1, 'total of void, 2,' 'total') 'test range. "
    7, decode (grouping (test_range) + grouping (hospital), 0, max (rank), null) row
    8 of)
    9. Select pat_nm
    10 in the hospital
    11, test_range
    12, sum (total)
    13, dense_rank () over (partition test_range | pat_nm order of sum (total) / / desc) as rank
    14, row_number () over (partition of test_range | pat_nm order of sum (total) / / desc) as rk
    15 of t2
    Group 16
    17 by pat_nm
    18, hospital
    19, test_range
    20                   )
    Group 21
    22 by grouping sets ((pat_nm, test_range, hôpital), (pat_nm, test_range), ())
    Order 23
    24 by pat_nm
    25, test_range
    26, (grouping (test_range) + grouping (hospital))
    27, rank
    28         )
    29 where to classify<= 3="" or="" rank="" is="">

    Total number of tests PAT_N HOSPITAL rank RANK
    ----- --------------------- ---------- --------- ----------
    Andy Fortis 24500 1 - 3 mph 1
    Andy 24500 subtotal
    Andy Aashiana 110600 5-10 mph 1
    Andy Amar 34800 5-10 mph 2
    Andy, Max 33700 5-10 mph 3
    Andy 220225 subtotal
    Andy Nimhans 50000< 1="" mph           ="">
    Andy Columbia 27700< 1="" mph           ="">
    Andy Meenam 11000< 1="" mph           ="">
    Andy 88700 subtotal
    Andy Murari 20600 > 10 mph 1
    Andy Batra 20000 > 10 mph 2
    Andy Mnagamani 12000 > 10 mph 3
    Andy 52600 subtotal
    Cindy 27900 1 Asia - 3 mph 1
    Cindy MLal 22600 1 - 3 mph 2
    Cindy Amar 21325 1 - 3 mph 3
    Total part-time cindy 71825
    Cindy Aashiana 23450 5-10 mph 1
    Cindy Childs Trust 22775 5-10 mph 2
    Cindy Batra 14700 5-10 mph 3
    Cindy 60925 subtotal
    Cindy Max 170000< 1="" mph           ="">
    Cindy A.G.M.                     97800< 1="" mph           ="">
    Cindy Manipal 29040< 1="" mph           ="">
    Cindy 387865 subtotal
    Medical center of cindy Apollo 19000 > 10 mph 1
    Cindy 19000 subtotal
    925640 total

    29 selected lines.

    SQL >

  • The autodeploiement based on the Start Date

    Hello world

    I have a requirement that I take a stream of HR (GTC FF) and this user from IOM and AD based on the start date of the provision. Without taking into account the start date, I can do it by assigning a role that contains an access strategy for AD. Under certain conditions, I attribute this role based on an attribute in the HR feed with a ruler, I defined.

    then how I also factor in the start date in the rule I created which determines if the role must be assigned. It does not resemble the rule designer can handle anything, except for string literals. If I can account for the date, I could probably use a scheduled task to later assign roles to users based on their start date. so in this process I assign the role if the two attribute condition and departure date are met.

    I could take a different approach and say I'm not going to assign the role to all through the accession of any rule and just manipulate through scheduled tasks. I'm not sure I like this approach, but if that's the solution preferred, please let me know.

    Thank you

    I suggest the route of the scheduled task. Recon in your current values, including the start date. Create an another UDF which is a checkbox, or a few true/false, yes/no field type. In your scheduled task, find users who have a start date< current="" time="" and="" then="" mark="" the="" udf="" as="" the="" true/yes/checked="" value.="" in="" your="" group="" membership="" rule="" for="" ad="" provisioning,="" add="" a="" rule="" that="" checks="" for="" this="" field.="" this="" will="" then="" trigger="" the="" access="" policy="" to="" apply.="" i="" have="" used="" this="" technique="" before="" and="" it="" works="" very="" well="" to="" turn="" a="" field="" that="" you="" can't="" have="" a="" group="" membership="" rule="" on="" turned="" into="" a="" field="" that="" can="" have="" a="" rule="">

    -Kevin

  • Mobile groups within other groups

    Seems really basic, but keep problems to move a group within another group (tested CC 2014 and 2015). So the following code results in an error of the argument not comply .move line (I also tried other variants like. PLACEATBEGINNING and. INSIDE):

    var doc = app.documents.add (256, 256, 72, "foo")

    var group1 = doc.layerSets.add)

    var group2 = doc.layerSets.add)

    Group1.move (Group2, ElementPlacement.PLACEATEND)

    doc. Close (SaveOptions.DONOTSAVECHANGES)

    But this way works OK

    var doc = app.documents.add (256, 256, 72, "foo")

    var group1 = doc.layerSets.add)

    var group2 = doc.layerSets.add)

    Layer1 var = group1.artLayers.add)

    Layer2 var = group2.artLayers.add)

    Group1.move (layer2 , ElementPlacement.PLACEAFTER)

    doc. Close (SaveOptions.DONOTSAVECHANGES)

    No cracking what limits are at stake here? You can do the latter, but then you might need to create and remove the layers of the tmp to use as target.

    Yes, it has been a problem, but you can use AM.

    var doc = app.documents.add( 256, 256, 72, "foo");
    doc.layerSets.add();
    var group1 = getLayerID();
    doc.layerSets.add();
    var group2 = getLayerID();
    
    moveLayerToLayerSet( group2, group1);
    
    function moveLayerToLayerSet( fromID, toID ){
    var desc5 = new ActionDescriptor();
    var ref4 = new ActionReference();
    ref4.putIdentifier( charIDToTypeID('Lyr '), Number(fromID) );
    desc5.putReference( charIDToTypeID('null'), ref4 );
    var ref5 = new ActionReference();
    ref5.putIndex( charIDToTypeID('Lyr '), getLayerIndexByID(toID) );
    desc5.putReference( charIDToTypeID('T   '), ref5 );
    desc5.putBoolean( charIDToTypeID('Adjs'), false );
    desc5.putInteger( charIDToTypeID('Vrsn'), 5 );
    try{
    executeAction( charIDToTypeID('move'), desc5, DialogModes.NO );
    }catch(e){alert(e);}
    };
    function getLayerID(){
    var ref = new ActionReference();
    ref.putEnumerated( charIDToTypeID('Lyr '),charIDToTypeID('Ordn'),charIDToTypeID('Trgt') );
    var desc = executeActionGet(ref);
    return desc.getInteger(stringIDToTypeID( 'layerID' ));
    };
    function getLayerIndexByID(ID){
    var ref = new ActionReference();
    ref.putIdentifier( charIDToTypeID('Lyr '), ID );
    try{
    activeDocument.backgroundLayer;
    return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1;
    }catch(e){
    return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ));
    }
    };
    
  • Apply an object style to the group without changing the object style of page within the group elements?

    Does anyone know a way to apply an object style to a group without changing the style of the subject of the articles within the Group?

    Well, as far as I know, there no way to do it. Apply an object style to a group applies this style to all elements within the group as well. So, I resorted to writing a script for this.

  • can I format the values aggregated within a group

    Hello everyone,

    Question: How can I format the values aggregated within a group.

    I created a group of - >
    <? for-each - group:current-group(); / PZN? >
    ...
    <? end for each group -? >

    Inside the group, I have grouped the VALUE->
    <? amount (current - group (/VALUE))? >

    Now I want to format the VALUE. I tried to apply a mask to format #, # 0.00 and tried to apply
    <? format - number:(sum(current-group()/PREIS));' 999G999D99'? > but either workes.

    full code:
    <? for-each - group:current-group(); / PZN? >
    <? format - number:(sum(current-group()/PREIS));' 999G999D99'? >
    <? end for each group -? >

    Andybody knows what I'm doing wrong?

    Thanks in advance!

    Sorry, I got them mixed up. This should work for you:

    or

    I'm not sure about your version and xslt function, but it seems that it does not recognize the xdoxslt:format_number so I guess that is not supported in the version 5.6.3.

    BR
    Magnus

  • In Yahoo Mail, when you compose an email I get a Panel "asking to leave this page. It does not matter that I choose to stay or leave, the Panel will appear again... and again... the only way is to force enough Firefox and start all over... help please...

    In Yahoo Mail, when you compose an email I get a Panel "asking to leave this page. It does not matter that I choose to stay or leave, the Panel will appear again... and again... the only way is to force enough Firefox and start all over... help please...

    Some extensions or toolbars can add such a report.

    It can also be part of the code on a page to prevent dataloss if you began to type text (composing a new mail) and you try to close this tab.

    You should be able to close this report and close the active tab.

    What I posted above is applicable to all platforms, including Mac.

  • When you play FarmVille, my computer crashes or I get a notice that "bits have been lost" and refresh the page... usually don't work and I have to start all over again.

    When you play FarmVille, my computer crashes or I get a notice that "bits have been lost" and refresh the page... usually don't work and I have to start all over again.

    I think that there is a prob with this game, it does not matter if you use firefox or IE. Or another browser. It blocks something. Or causes probs.

    FB needs to fix it. There are a lot of people (in a Google search), who are having probs. One way or the other

  • Can I install a new edition of retail XP on an existing OEM edition or what I need to reformat and start all over again?

    Original title: Windows XP

    Can I install a new edition of retail XP on an existing OEM edition or what I need to reformat and start all over again?

    Hi billavery,

    1. which type of installation do you want?
    2. is it repair install or clean install?

    Yes, you can install the retail version of XP on an existing OEM edition.
    See perform a repair Installation
    See How to install or upgrade to Windows XP

    Visit our Microsoft answers feedback Forum and let us know what you think.

  • I have spent at a new laptop and have to start all over everything install

    I learned to find the place to start all over again with CC on an already existing account. I have spent at a new laptop and have to start the installation everything @.

    Cloud license allows 2 activations http://www.adobe.com/legal/licenses-terms.html

    -Install on a 2nd computer http://forums.adobe.com/thread/1452292?tstart=0

    -Windows or Mac has no importance... 2 on the same operating system or 1 on each

  • Can I delete the Organizer (8 items) and start all over again? Help, please.

    How can I start all over in the Organizer? I have so many duplicates in so many different places (as hidden and Unhidden) and even with the names of album and keywords. I jumped in before I took the tutorials, and now I'm paying for it. I'll blow the roof soon if I don't come up with a plan!

    Hello

    When you delete an Organizer file, it deletes the file from the catalogue (so from Albums, Tags). It also invites you to delete the file from your hard drive.

    Choose Yes it removes the hard drive as well. No. Chossing, will remove from the catalog, but the actual file will remain on the hard disk.

    Thank you/Cooman

  • SQL: Rearrange random a column within a grouping value

    10g release 2.

    I have a grouping column and a column of data.
    CREATE TABLE t1 (
        id      NUMBER
    ,   x1      VARCHAR2(10)
    );
    INSERT INTO t1 (id, x1) VALUES (1,'A');
    INSERT INTO t1 (id, x1) VALUES (1,'B');
    INSERT INTO t1 (id, x1) VALUES (1,'C');
    INSERT INTO t1 (id, x1) VALUES (1,'D');
    INSERT INTO t1 (id, x1) VALUES (1,'E');
    INSERT INTO t1 (id, x1) VALUES (2,'F');
    INSERT INTO t1 (id, x1) VALUES (2,'G');
    INSERT INTO t1 (id, x1) VALUES (2,'H');
    INSERT INTO t1 (id, x1) VALUES (2,'I');
    INSERT INTO t1 (id, x1) VALUES (3,'J');
    INSERT INTO t1 (id, x1) VALUES (4,'K');
    INSERT INTO t1 (id, x1) VALUES (4,'L');
    I am looking for a SELECT statement that will pull all the x 1 for a random ID given, such that every x 1 original is represented once in the new random column, but this data is not "Cross" his original group ID border.

    So, this might be valid outputs:
            ID X1         RANDOMIZED
    ---------- ---------- ----------
             1 A          E
             1 B          A
             1 C          C
             1 D          B
             1 E          D
             2 F          F
             2 G          H
             2 H          G
             2 I          I
             3 J          J
             4 K          K
             4 L          L
    .. .as would have this:
            ID X1         RANDOMIZED
    ---------- ---------- ----------
             1 A          A
             1 B          C
             1 C          E
             1 D          D
             1 E          B
             2 F          I
             2 G          H
             2 H          G
             2 I          F
             3 J          J
             4 K          L
             4 L          K
    Note how the random value never cross a line ID.

    On the other hand, the following would not be outputs valid...
            ID X1         RANDOMIZED
    ---------- ---------- ----------
             1 A          B
             1 B          E
             1 C          C
             1 D          A
             1 E          I
             2 F          D
             2 G          G
             2 H          F
             2 I          H
             3 J          J
             4 K          K
             4 L          L
    because randomized 'I' is now the fifth data element to group the ID 1, whereas in the table, 'I' belongs to the group ID 2. Similarly, the randomized study "D" now appears as a line associated with the ID 2, whereas it is defined initially as belonging to ID 1.

    A solution purely SQL is possible?

    Thank you.

    Hello

    It is a different and much more interesting problem!

    At the heart of the solution is always good: use two different schemes of numbering (at least one of them, based on a random number) to tell which element will be substituted for each.

    Here's one way:

    WITH      unpivoted_data   AS
    (
         SELECT     id
         ,     address_ln_1                         AS address_txt
         ,     1                              AS address_ln_num
         ,     1 + LENGTH (address_ln_1)
                - LENGTH (REPLACE (address_ln_1, ' '))     AS token_cnt
         FROM     t_addr
         UNION ALL
         SELECT     id
         ,     address_ln_2                         AS address_txt
         ,     2                              AS address_ln_num
         ,     1 + LENGTH (address_ln_2)
                - LENGTH (REPLACE (address_ln_2, ' '))     AS token_cnt
         FROM     t_addr
    )
    ,      cntr          AS
    (
         SELECT     LEVEL     AS n
         FROM     dual
         CONNECT BY     LEVEL <= 1 + ( SELECT  MAX (token_cnt)
                               FROM    unpivoted_data
                             )
    )
    ,     got_tokens     AS
    (
         SELECT     u.*
         ,     c.n               AS token_num
         ,     REGEXP_SUBSTR ( u.address_txt
                         , '[^ ]+'
                         , 1
                         , c.n
                         )          AS token_txt
         ,     ROW_NUMBER () OVER ( PARTITION BY  u.address_ln_num
                                          ,                    c.n
                             ORDER BY        dbms_random.string ('P', 10)
                                  )      AS o_num
         ,     ROW_NUMBER () OVER ( PARTITION BY  u.address_ln_num
                                          ,                    c.n
                             ORDER BY        dbms_random.value
                                  )      AS r_num
         FROM    unpivoted_data     u
         JOIN     cntr          c     ON     c.n <= u.token_cnt
    )
    ,     got_scrambled_txt     AS
    (
         SELECT     CONNECT_BY_ROOT id     AS id
         ,     address_ln_num
         ,     TRIM ( SYS_CONNECT_BY_PATH ( token_txt
                                       , ' '
                                )
                   )          AS scrambled_txt
         ,       r_num
         FROM       got_tokens
         WHERE       CONNECT_BY_ISLEAF     = 1
         START WITH     token_num     = 1
         CONNECT BY     token_num     = PRIOR token_num + 1
              AND     address_ln_num  = PRIOR address_ln_num
              AND     r_num          = PRIOR r_num
    )
    SELECT       o.id
    ,       MIN (CASE WHEN r.address_ln_num = 1 THEN r.scrambled_txt END)     AS scrambled_ln_1
    ,       MIN (CASE WHEN r.address_ln_num = 2 THEN r.scrambled_txt END)     AS scrambled_ln_2
    FROM       got_scrambled_txt     r
    JOIN       got_tokens          o     ON     r.r_num     = o.o_num
    WHERE       o.address_ln_num     = 1
    AND       o.token_num          = 1
    GROUP BY  o.id
    ORDER BY  o.id
    ;
    

    This assumes that address_ln_1 is not NULL.

    Exit (1st time):

    . ID SCRAMBLED_LN_1                 SCRAMBLED_LN_2
    ---- ------------------------------ ------------------------------
     101 901 ORACLE PKWY
     102 1 INFINITE ROAD                APT 3B
     103 500 ANTONIO LOOP               SUITE 100
    

    Output (2nd time: no changes to the query or table):

    . ID SCRAMBLED_LN_1                 SCRAMBLED_LN_2
    ---- ------------------------------ ------------------------------
     101 1 ANTONIO PKWY                 APT 3B
     102 500 INFINITE ROAD
     103 901 ORACLE LOOP                SUITE 100
    
  • 4 ranks of a group and 6 rows of next and then 4 of third... so now...

    Hi people,

    I have an obligation to which the Table and the data I have given below.

    -There are several departments. (In the data provided, there is only one Department).
    -For each Department, there are 10 departments void numbered 10 to 100.
    -In each Department, the total_emp column gives the total number of employees in each sub_dept.

    Requirement.
    -Data must be ordered as the "dept, total_emp desc".
    -Then I have to choose 4 rows of sub_dept 10. Leave the remaining lines of sub_dept 10. -> Group 1.
    -Then, 6 rows of sub_dept 20. Leave the remaining lines. -> Group 2.
    -Choose 4 ranks of sub_dept 30. Let the rest of the lines. -> Group 3.
    .. and so on until we reach 100 sub_dept. -> Group 4.
    -Choose 6 sub_dept 100 ranks and leave the remaining lines of sub_dept 100 only. -> Group 10.

    Now,.
    -Return to sub_dept 10, choose 4 rows of sub_dept 10, leave remaining only lines. -> Group 11.
    -Choose 4 ranks of sub_dept 20, leave remaining alone, lines (if any left). -> Group 12.
    -Choose 6 ranks of sub_dept 30, leave remaining only lines. -> Group 13
    .. and so on

    Now, we need to iterate over the query results until all these lines were processed and put into groups of 4 and 6.
    It is possible that in the last or last but 1 iteration, the groups would not be to have 4 or 6 ranks, but less. That's ok.

    NOTE:-the sub_dept do not have the same number of lines, they may have a different number of lines.
    Just to be able to explain things to you people, I for the data lines 10 sample by sub_dept.

    In addition, you can refer to this post by me that responded
    [Groups alternated | http://forums.oracle.com/forums/thread.jspa?messageID=2845078 & #2845078]
    create table emp_gr
    (dept      number,
     sub_dept  number,
     total_emp number
    )
    /
    
    
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,10,3898);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,10,2189);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,10,1576);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,10,941);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,10,533);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,10,428);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,10,412);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,10,358);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,10,251);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,10,180);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,20,2361);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,20,2315);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,20,1715);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,20,1347);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,20,1282);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,20,1234);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,20,1203);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,20,1173);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,20,250);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,20,205);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,30,2544);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,30,2492);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,30,2431);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,30,2371);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,30,2333);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,30,1707);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,30,633);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,30,527);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,30,277);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,30,205);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,40,2985);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,40,2894);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,40,2513);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,40,2066);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,40,2038);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,40,1557);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,40,863);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,40,561);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,40,249);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,40,159);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,50,4260);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,50,3474);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,50,2486);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,50,2260);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,50,1767);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,50,1603);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,50,681);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,50,439);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,50,410);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,50,231);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,60,4478);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,60,2474);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,60,1573);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,60,1428);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,60,940);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,60,844);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,60,722);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,60,683);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,60,359);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,60,238);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,70,2775);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,70,2731);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,70,2505);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,70,2173);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,70,2093);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,70,1546);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,70,1039);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,70,739);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,70,106);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,70,24);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,80,3522);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,80,2348);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,80,2226);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,80,1555);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,80,1236);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,80,1143);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,80,867);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,80,778);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,80,280);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,80,165);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,90,3430);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,90,2344);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,90,1743);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,90,1629);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,90,1535);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,90,1109);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,90,1073);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,90,915);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,90,564);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,90,121);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,100,4770);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,100,3836);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,100,2590);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,100,2025);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,100,841);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,100,804);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,100,661);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,100,272);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,100,267);
    Insert into emp_gr (DEPT,SUB_DEPT,TOTAL_EMP) values (1,100,265);
    The result I'm looking for is as follows:
    sub_dept  total_emp  group
    10        3898       1
    10        2189       1
    10        1576       1
    10         941       1
    20       2361       2
    20       2315       2
    20       1715       2
    20       1347       2
    20       1282       2
    20       1234       2
    30       2544       3
    30       2492       3
    30       2431       3
    30       2371       3
    Hope that's clear

    Thanks in advance. Help would be really appreciated.

    Published by: henryswift on November 3, 2008 13:07

    Here we try more:

    SQL> select dept,
      2         sub_dept,
      3         total_emp,
      4         sum(group#) over (partition by dept order by rn,sub_dept) group#
      5  from   (select dept,
      6                 sub_dept,
      7                 total_emp,
      8                 trunc((rn-1)/decode(mod(rk,2),1,4,6)) rn,
      9                 decode(lag(sub_dept) over (partition by dept order by trunc((rn-1)/decode(mod(rk,2),1,4,6)),sub_dept),sub_dept,0,1) group#
     10          from   (select dept, sub_dept, total_emp,
     11                          row_number() over (partition by dept,sub_dept order by total_emp desc) rn,
     12                          dense_rank() over (partition by dept order by sub_dept) rk
     13                   from   emp_gr))
     14  order by dept,group#,sub_dept,total_emp desc;
    
          DEPT   SUB_DEPT  TOTAL_EMP     GROUP#
    ---------- ---------- ---------- ----------
             1         10       3898          1
             1         10       2189          1
             1         10       1576          1
             1         10        941          1
             1         20       2361          2
             1         20       2315          2
             1         20       1715          2
             1         20       1347          2
             1         20       1282          2
             1         20       1234          2
             1         30       2544          3
             1         30       2492          3
             1         30       2431          3
             1         30       2371          3
             1         40       2985          4
             1         40       2894          4
             1         40       2513          4
             1         40       2066          4
             1         40       2038          4
             1         40       1557          4
             1         50       4260          5
             1         50       3474          5
             1         50       2486          5
             1         50       2260          5
             1         60       4478          6
             1         60       2474          6
             1         60       1573          6
             1         60       1428          6
             1         60        940          6
             1         60        844          6
             1         70       2775          7
             1         70       2731          7
             1         70       2505          7
             1         70       2173          7
             1         80       3522          8
             1         80       2348          8
             1         80       2226          8
             1         80       1555          8
             1         80       1236          8
             1         80       1143          8
             1         90       3430          9
             1         90       2344          9
             1         90       1743          9
             1         90       1629          9
             1        100       4770         10
             1        100       3836         10
             1        100       2590         10
             1        100       2025         10
             1        100        841         10
             1        100        804         10
             1         10        533         11
             1         10        428         11
             1         10        412         11
             1         10        358         11
             1         20       1203         12
             1         20       1173         12
             1         20        250         12
             1         20        205         12
             1         30       2333         13
             1         30       1707         13
             1         30        633         13
             1         30        527         13
             1         40        863         14
             1         40        561         14
             1         40        249         14
             1         40        159         14
             1         50       1767         15
             1         50       1603         15
             1         50        681         15
             1         50        439         15
             1         60        722         16
             1         60        683         16
             1         60        359         16
             1         60        238         16
             1         70       2093         17
             1         70       1546         17
             1         70       1039         17
             1         70        739         17
             1         80        867         18
             1         80        778         18
             1         80        280         18
             1         80        165         18
             1         90       1535         19
             1         90       1109         19
             1         90       1073         19
             1         90        915         19
             1        100        661         20
             1        100        272         20
             1        100        267         20
             1        100        265         20
             1         10        251         21
             1         10        180         21
             1         30        277         22
             1         30        205         22
             1         50        410         23
             1         50        231         23
             1         70        106         24
             1         70         24         24
             1         90        564         25
             1         90        121         25
    
    100 rows selected.
    
    SQL>
    

    Nicolas.

    1. a subquery lesss
    2 change the order by clause
    Edited by: N. Gasparotto on November 3, 2008 11:56

  • How to insert as an entry within the formula variable, the total time on the scope window?

    How to insert, as an entry within the formula variable, the window of total time on the scope (i.e. of 20ms/div x 10 div = 200ms)?

    HERE'S A SAMPLE QUESTION:

    FORMULA FOR INTEGRAL ACTION:

    STATISTICS:

    Input variable: DPO4034 (CH1);

    Box: number of samples.

    FORMULA

    Input variable 0: DPO4034 (CH1); alias: x 0

    Input variable 1: 'time window Total out of scope?. " alias: x 1

    Input variable 2: number of samples (CH1); alias: x 2

    Under the operation Configuration: formula

    Y = (x 0 ^ 2) *(x1/x2)

    Output: Data 1 (CH1)

    THEN, WITH THE HELP OF STATISTICS:

    Input signal: data processing 1 (CH1)

    Checkbox: SUM

    Output: CH1 integral Action [has ^ 2s].


Maybe you are looking for