you place your order of rows based on the values

Hello

I have two tables for example: emp1 emp2 and
 emp1 
  
AD_NAME     VD     COURSE_NOS
------------------------------
AD1           100     3
AD2              2     2
AD3             50     5

emp 2

AD_NAME     COURSE
-------------------------

AD1              C1
AD1              C2
AD1              C3
AD2              C1
AD2              C2
AD3           C1
AD3              C2
AD3              C3
AD3              C4
AD3              C5

i want to order the ad_name of emp2 based on  highest vd order of emp1 but the order should be for alternative vd

like

AD1       100
AD3         50
AD2           2
AD1        100
AD3          50
AD2            2
.
.

.
AD3          50        
Published by: vishnu prakash on Sep 8, 2010 04:40

You mean, more like this...

SQL> ed
Wrote file afiedt.buf

  1  with emp1 as (select 'AD1' as ad_name, 100 as vd, 3 as course_nos from dual union all
  2                select 'AD2', 2, 2 from dual union all
  3                select 'AD3', 50, 5 from dual union all
  4                select 'AD4', null, 2 from dual)
  5      ,emp2 as (select 'AD1' as ad_name, 'C1' as course from dual union all
  6                select 'AD1', 'C2' from dual union all
  7                select 'AD1', 'C3' from dual union all
  8                select 'AD2', 'C1' from dual union all
  9                select 'AD2', 'C2' from dual union all
 10                select 'AD3', 'C1' from dual union all
 11                select 'AD3', 'C2' from dual union all
 12                select 'AD3', 'C3' from dual union all
 13                select 'AD3', 'C4' from dual union all
 14                select 'AD3', 'C5' from dual union all
 15                select 'AD4', 'C1' from dual union all
 16                select 'AD4', 'C2' from dual)
 17  --
 18  -- END OF TEST DATA
 19  --
 20  select ad_name, vd, course_nos, course
 21  from (
 22        select emp1.ad_name, vd, course_nos, course
 23              ,row_number() over (partition by emp1.ad_name order by decode(vd,null,1,0), course) as crn
 24        from emp1 join emp2 on (emp1.ad_name = emp2.ad_name)
 25       )
 26* order by decode(vd,null,1,0), crn, vd desc nulls last
SQL> /

AD_         VD COURSE_NOS CO
--- ---------- ---------- --
AD1        100          3 C1
AD3         50          5 C1
AD2          2          2 C1
AD1        100          3 C2
AD3         50          5 C2
AD2          2          2 C2
AD1        100          3 C3
AD3         50          5 C3
AD3         50          5 C4
AD3         50          5 C5
AD4                     2 C1
AD4                     2 C2

12 rows selected.

SQL>

?

Tags: Database

Similar Questions

  • You place your order by number.

    Hello
    I have a report that I would like to order by order of calendar. By that, I mean I already know the order in which I'm supposed to get these results. This is what the data would look like.
    Student Category      Total Students     Total Fee Amount
    =========================================================
    61                              9999       999,999,999.00
    64                              9999       999,999,999.00
    6A                              9999       999,999,999.00
    65                              9999       999,999,999.00
    66                              9999       999,999,999.00
    67                              9999       999,999,999.00
    68                              9999       999,999,999.00
    68A                             9999       999,999,999.00
    69                              9999       999,999,999.00
    60                              9999       999,999,999.00
    48                              9999       999,999,999.00
    As you can see that if I stopped my request based on the category student, then 48 would come up to high and 6 would come after 69 so spoil the order. My question is:

    Is there a simple solution or this would require the construction of a PL/SQL table? Don't want to spend too much time, if it's really not as simple as that.

    For any help or suggestion is welcome.

    Thank you!

    Try something like this:

    order by decode(student_category
                   ,'61',1
                   ,'64',2
                   ,......
                   ,'48',11
                   ,0
                   )
    
  • Preloader you place your order

    Hi, I'm replenish items are several tutorials on different sites to create my own slideshow based on xml with correlated images. It works great except that I am not able to control the order in which the images are loaded. They get loads in the order that they are downloaded, not in order of my XML file. It is that is causing the links and other attributes that I put in my xml doc, to fall out of sync.

    I guess that there is a way to push my items in a specific order, or make sure that the additional attributes are loaded at the point where the image is pushed into the table. I guess some context would help (see attached code)

    I think that I have provided all the relevant information, but if you need to see another feature let me know.

    Yet once, in charge of everything very well when I test locally, it's just that the order is out of sync when I actually download the items. The heavier images are transferred backward, even though these attributes are in the right order.

    I really wish to keep pictures in the order of the XML doc, but if this is not possible I need to at least be able to make sure that the attribute values are assigned to the correct image container. I don't know where and how.

    Thanks in advance!

    Try:

  • You place your order for recovery disc for mini 110

    Hello world

    first of all, I say this isn't really a laptop, I have a mini 110-3610sa.

    I've upgraded to windows 7 Professional to windows 7 basic, whatever he was going well until I stopped

    I lost the HP quick web and also the restoration of the factory, it goes to the windows recovery.

    can help you, I would really like to have fast web back it was very good and I was getting just configured with e-mails and fav sites.

    any help

    Ron tinydamed

    You can order recovery disks by following the instructions in the link below

    http://h20000.www2.HP.com/bizsupport/TechSupport/document.jsp?objectID=c00810334

  • You place your order for dashboards

    Hello world

    I want to sort the dashboard at the top of the screen. For now, they are sorted in alphabetical order, but I want to sort by a custom order. I know that I can put a number in front of it, but I don't like the idea of having numbers on the dashboard menu. In the past, I thought I was able to put spaces "" in front of a dashboard I wanted to ordered first, but for some reason, I can't it works again.

    Is it possible to order dashboards without putting the numbers in the names?

    Thank you!

    -Joe

    Vidal method would be the way to go if you want numbers. Don't know why the 'method of space' does not work.

    http://oraclebizint.WordPress.com/2008/04/02/Oracle-BI-EE-101332-changing-sort-order-of-dashboards/

  • you place your order in xquery-distinct values

    I was surprised to see the return of the following different results, is this a bug in the XML Query rewrite, or is the order of not only guaranteed the distinct values?

    Oracle Database 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production

    With partitioning, OLAP, Data Mining and Real Application Testing options

    SQL > SELECT XMLQUERY)

    2 ' (string code points

    3 distinct-values ((99,98,97)))'

    4 ELECTION CONTENT) result

    5 FROM dual;

    RESULT

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

    ABC

    SQL > SELECT / * + NO_XML_QUERY_REWRITE * /.

    () 2 XMLQUERY

    3 ' (string code points

    distinct-values ((99,98,97))) 4'

    5 ELECTION CONTENT) result

    6 FROM dual;

    RESULT

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

    CBA

    SQL >

    Hello

    The specs say the order is "dependent application": fn: distinct values

    Summary so is (I guess), strict language point of view, should not rest on any apparent order in the resulting sequence.

    The results collected by the two queries probably be different for the same reason: different implementation.

    First one with XQuery Rewrite active is solved using the SQL operators and functions, something along these lines:

    SELECT 'CODEPOINT2STR_FUNC' ('SEQUENCE')

    DE)

    SELECT 'AGGREGATE_FUNC' (VALUE (T) SEPARATE) "SEQUENCES".

    TABLE ("SEQUENCE_FUNC"(99, 98, 97)) T

    ) ;

    While the second is evaluated by the XVM and apparently maintains the original order.

  • Combine the rows based on the value of the column

    I try to combine lines based on a common value.

    Data in the table:
    Result A
    Player_ID   Player_Name  Team_Name  Points Sequence
    99999       John Smith    Bulls              50         1
    99999       John Smith    Pistons          14         2
    
    
    What i want in the output
    Result B
    
    Player_ID Player_Name Team_Name  Points
    99999     John Smith      Bulls           64 (50 for Bulls)
    Here are my main request...
    select player_id, player_name, team_name, points
    from
            (
              SELECT player_id, player_name, team_name, sum(points) sum_points
              FROM stats_data
              WHERE season_id = 2008
              AND points > 0
              GROUP BY player_id, player_name, team_name
              ) t
    ORDER BY sum_points DESC, last_name;
    Current query gives me the result of the result of A

    (1) not entirely sure how, to combine the player_id basic data
    2) once I found 1, I'm sure that by adding the ("sum_points" for "team_name") to the output can be done with a case statement, I think?

    Hello

    Thank you.

    Let me make sure I understand: If a player on two or more teams, the one with the largest value of the sequence appears in the column team_name. Others will be displayed, in the order of sequence, in the points column.

    This done:

    WITH     gb1     AS
    (
         SELECT     player_id, player_name, team_name, sequence
         ,     SUM (points)          AS sum_points
         ,     ROW_NUMBER () OVER ( PARTITION BY player_id, player_name
                                   ORDER BY         sequence   DESC
                           )   AS r_sequence
         ,     STRAGG (' ' || SUM (points) || ' for ' || team_name)
                  OVER ( PARTITION BY  player_id, player_name
                          ORDER BY        sequence
                    )  AS all_points
         FROM     game
         GROUP BY  player_id, player_name, team_name, sequence
    )
    SELECT    player_id
    ,         player_name
    ,         MIN (team_name)  KEEP (DENSE_RANK FIRST ORDER BY r_sequence)     AS team_name
    ,       TO_CHAR (SUM (sum_points))
           || MAX ( CASE
                          WHEN  r_sequence > 1
                     THEN  ' ('
                               || REGEXP_REPLACE ( SUBSTR (all_points, 2)
                                         , ' ,[^,]$'
                                    )
                        || ')'
                 END
                    )  AS points
    FROM          gb1
    GROUP BY  player_id,            player_name
    ORDER BY  player_id,            player_name;
    

    The information listed in points (e.g. '(16 pour les TAUREAUX, 4 pour les PISTONS) ') requires the aggregation of the chain.
    [AskTom.oracle.com | http://asktom.oracle.com/pls/asktom/f?p=100: 11:0:P11_QUESTION_ID:2196162600402] shows several ways to do so.
    I used the first option on this page, the function defined by the user STRAGG, that you can copy from this page.

    You can have a similar function, the WM_CONCAT (property WMSYS), already installed.
    WM_CONCAT is not documented, so you can't use it in your Production applications.

  • SQL question - aggregate rows based on the value of the columns

    Hi all

    DBA_EXTENTS reports on the number of starting point of measurement (column BLOCK_ID) and the size of the scale in the Oracle (BLOCKS column) blocks.

    I'm trying to combine the lines of the segments of same as if their associated extensions are adjacent.

    For example, assume that the data in the view are as follows:

    Nom_segment Block_id BLOCKS
    EMP108
    EMP188
    DEPARTMENTS2616
    DEPARTMENTS428

    EMP

    5016EMP668

    And I want to do a set of results that looks like this:

    Nom_segment Block_id BLOCKS
    EMP1016
    DEPARTMENTS2624
    EMP5016

    I thought to use an analytical query, but I couldn't find something that meets this requirement.

    Can you please help?

    Hello

    Here's a way to do that using the analytical SUM function:

    WITH got_grp AS

    (

    SELECT nom_segment, block_id, blocks

    , block_id - SUM (blocks) over (PARTITION BY nom_segment

    ORDER BY block_id

    ) AS the grp

    FROM the data

    )

    SELECT nom_segment

    MIN (block_id) AS grp_block_id

    SUM (blocks) AS grp_blocks

    OF got_grp

    GROUP BY nom_segment, grp

    ORDER BY grp_block_id

    ;

    The data you posted in response to #3:

    Pini Dibask wrote:

    Thanks Frank,.

    If you would care to post CREATE TABLE and INSERT to your sample data and the version of your database, then I could show you exactly.

    The Oracle version is 11.2.0.4

    This is the CREATE TABLE and INSERT statements for the sample data:

    CREATE THE TABLE DATA (NOM_SEGMENT VARCHAR2 (20), BLOCK_ID NUMBER, NUMBER OF BLOCKS);

    INSERT IN DATA VALUES ("EMP", 10, 8);

    INSERT IN DATA VALUES ("EMP", 18: 8);

    INSERT IN DATA VALUES ("DEPARTMENTS", 28, 16);

    INSERT IN DATA VALUES ("DEPARTMENTS", 42, 8);

    INSERT IN DATA VALUES ("EMP", 50, 8);

    INSERT IN DATA VALUES ("EMP", 66, 8);

    is not the same as the data in your first post and the results are not the same, either.

    The fixed difference technique involves lines don't overlap.  In the sample data in response #3, 'DEPARTMENTS' lines overlap, i.e. blocks 42 and 43 belong to two segments.

  • order by clause based on the value of the parameter

    Hello

    I use reports 6i with db 10 g

    I need to create a report sorted in the order of four fields. There is a field selection parameter which should come first in the order by clause.

    IE the user has available to display the report in a sorted order selected

    Assume that the four fields are emp_id, name, dept_id desig.

    If the user selects dept_id, then the order by clause should be in order of dept_id emp_id, name, desig

    Please help solve this scenario.

    Thank you

    Hi Clement,.

    Use the query as follows:

    select column_name1, column_name1, ....
    from  table_name
    &order_by_clause
    

    Now in the trigger AFTER-PARAMETER-SHAPE has the order by clause below according to requirements:

    if :sort_column = dept_id then
    
      :order_by_clause := 'order by dept_id,emp_id,name,desig';
    
    else
    
      :order_by_clause := 'order by emp_id, dept_id, name,desig';
    
    end if;
     
    

    Here's sort_column setting user who will be selected by the user, provide the list of values for it.

    Hope this helps

    Best regards

    Arif Khadas

  • Filtering rows based on the value of the field

    Hello

    I have something like this:

    create table stuff
    (
    Avalue varchar2,
    anothervalue varchar2,
    thirdValue varchar2,
    aNumber number
    )

    INSERT INTO stuff Values ('abc', 'cde', efg', 2);
    INSERT INTO stuff Values ('abc', 'cde', efgggg', 4);
    INSERT INTO stuff Values ('abc', 'xxx', efg', 1);
    INSERT INTO stuff Values ('abc', 'xxx', efgxxx', 2);
    INSERT INTO stuff Values ('abc', 'yyy', efg', 2);

    I would like to receive the lines with the most "anumber" - value, grouped by "value" and "anothervalue" (but not "thirdvalue"). So, in this example, I want to rank 2, 4 and 5.

    Someone has an idea? I think that a 'order by desc"isn't the best way here?
    select  avalue,
            anothervalue,
            thirdvalue,
            anumber
      from  (
             select  s.*,
                     rank() over(partition by avalue,anothervalue order by anumber desc) rnk
               from  stuff s
            )
      where rnk = 1
    /
    
    AVALUE     ANOTHERVAL THIRDVALUE    ANUMBER
    ---------- ---------- ---------- ----------
    abc        cde        efgggg              4
    abc        xxx        efgxxx              2
    abc        yyy        efg                 2
    
    SQL> 
    

    SY.

  • You place your order on the Apple Site

    I ordered a 7 IPhone September 21, as it was very
    hard to get one at the store here in Rome, Italy
    at that time. Today, 7 October, I see that the IPhone is available to any
    store, but I have still not received my... and shipping follow-up shows that
    the phone is always located in Amsterdam, Holland. This means that it will be
    probably need another 7 days to be delivered to me! Now, I want to only disclose my
    concern that I think that what makes an online prescription should be advantaged
    otherwise it's not really a reason to buy
    an article on the Web Site!

    And this is the last time I'll do so!

    Thank you!

    Greetings from Rome

    !

    Don't know what your question on the iTunes Store, but you already know that you talk only to other users on these forums and that Apple are not here - if you want to contact Apple using the 'contact us' link at the bottom right of each page here.

  • You place your order of cross-directories

    Hello

    I need your help.

    I want to browse directories and order all the files by date.

    I used this command:

    cmd /c dir "c:\nuovacartella" / b/s A:-D / o:-d

    but the files are classified at the folder level.

    See the example:

    /C dir "c:\nuovacartella" C:\>cmd / b/s /A:-D / TC o:d

    c:\nuovacartella\cartella file\uno.txt

    c:\nuovacartella\cartella file\AAA\ciao.txt

    c:\nuovacartella\cartella file\AAA\due.doc

    c:\nuovacartella\cartella file\AAA\tre.xsl

    c:\nuovacartella\cartella file\AAA\appenaCreato.rar

    c:\nuovacartella\cartella file\BBB\B2\son a file.txt

    file Link\son a c:\nuovacartella\cartella

    c:\nuovacartella\cartella Link\uno

    c:\nuovacartella\cartella Link\appenaCreato

    c:\nuovacartella\cartella Link\ciao

    /C dir "c:\nuovacartella" C:\>cmd / b/s /A:-D / TC o:-d

    c:\nuovacartella\cartella file\uno.txt

    c:\nuovacartella\cartella file\AAA\appenaCreato.rar

    c:\nuovacartella\cartella file\AAA\tre.xsl

    c:\nuovacartella\cartella file\AAA\due.doc

    c:\nuovacartella\cartella file\AAA\ciao.txt

    c:\nuovacartella\cartella file\BBB\B2\son a file.txt

    c:\nuovacartella\cartella Link\ciao

    c:\nuovacartella\cartella Link\appenaCreato

    c:\nuovacartella\cartella Link\uno

    file Link\son a c:\nuovacartella\cartella

    but I want the files classified into cross-folder.

    Is it possible to do so (for example, using a batch script)?

    Thanks in advance. Leonardo

    Hi Leon,

    Thanks for posting your question in the Microsoft Community!

    I suggest you to ask your question on the MSDN Forums.

    http://social.msdn.Microsoft.com/forums/en-us/categories

    Hope this hslps.

  • You place your order by number Char field

    I have a char with data field.

    Required order is lower.

    Column1

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

    12

    12-A

    12-B

    122

    122-A

    122-C

    123

    123-A

    123-B

    I have to order both at the level of form and report.

    Pasha

    Lahore, Pakistan

    The 'trick' is pad strings or a part of them to the same length. In your case, it may be sufficient for the whole string if lpad suffix (A, B, C) are always of length 1.

    Otherwise, you will need to split the string at the position of the hyphen and lpad for both parties.

    Ah, now I see that you do not always have a suffix. Just so add an artificially.

    As the length, you must choose a value greater or equal to the maximum length of the string or their parts RESP.

    order by lpad (decode (instr (column1,'-'), 0, column1 |)) ((' - 0', column1), 10, '0')

    or

    Select Column1 from)

    Select

    Column1

    , max (length (decode (instr (column1,'-'), 0, column1 |))) (((' - 0', column1))) on len)

    tab

    )

    order by (lpad, len, '0')

  • You place your order by comparing - SQL Experts pls help

    Hi all

    I want to measure the closest points according to the percentage between these points and order.
    in other words, I want to show the relationship between the points according to the percentage value.
     
    drop table temp_value; 
    create table temp_value(id number(10),location varchar2(20), percentage number(9)); 
    
    
    insert into temp_value values  (1,'LOC 1,2',30); 
    insert into  temp_value values (2,'LOC 1,3',0); 
    insert into  temp_value values (3,'LOC 1,4',0); 
    insert into  temp_value values (4,'LOC 1,5',0); 
    insert into  temp_value values (5,'LOC 1,6',50); 
    insert into  temp_value values (6,'LOC 2,3',0); 
    insert into  temp_value  values(7,'LOC 2,4',0); 
    insert into  temp_value values (9,'LOC 2,5',0); 
    insert into  temp_value values (10,'LOC 2,6',10); 
    insert into  temp_value values (11,'LOC 3,4',90); 
    insert into  temp_value values (12,'LOC 3,5',80); 
    insert into  temp_value values (13,'LOC 4,5',0); 
    insert into  temp_value values (14,'LOC 4,6',0); 
    insert into  temp_value values (15,'LOC 5,6',0); 
    I want the output like this, the ranking of the point like this
     
    4 
    3 
    5 
    6 
    1 
    2 
    or like this
      6 
    1 
    2 
    4 
    3 
    5 
    If you ask me why I say this ranking only
     
    
    4    [90 percent between point 3,4 that mean can be 3,4 or 4,3] 
    3 
    5    [80 percent between 3,5 that mean can be 5 after 3,4] 
    6    [50 percent between 1,6 that mean can be 1,6 or 6,1 ] 
    1 
    2    [ 30 percent between 1,2 that mean can be 1,2 or 2,1] 
    concerning

    Published by: Isabelle October 6, 2012 10:18

    Published by: Isabelle October 6, 2012 10:43

    Published by: Isabelle October 6, 2012 20:04
    SQL> select loc
      2  from(
      3  select loc,rownum rn
      4  from(
      5  select percentage,location,regexp_substr(location,'\d+',1,rn) loc
      6  from temp_value a,
      7       (select 1 rn from dual union all
      8        select 2 from dual ) b
      9  order by percentage desc,to_number(regexp_substr(location,'\d+',1,rn)) asc
     10  ))
     11  group by loc
     12  order by min(rn);
    
    LOC
    --------------------
    3
    4
    5
    1
    6
    2
    
    6 rows selected.
    
  • You place your order with a reports folder

    Hi all

    I have a problem in sequencing the name of reports, the present system automatically organizes the reports based on alphabetically as below

    A
    B
    C

    I want the order to be as below, is that possible? I want reports to be arranged according to our use.

    A
    C
    F
    B

    Please confirm if this is possible.

    Thank you
    Natraj

    Hi Ludovic,.

    Maybe you can just put a number before the name of reports to change the sequence:

    1A
    2 C
    3 F
    4 B

    I don't know of any other possibility.

    Best regards, Tim

Maybe you are looking for

  • Not showing mediafire download link

    Since 4 days I can not get no download link that is hosted on Mediafire.com. Even my files are not open it says: [IMG] http://i42.Tinypic.com/2rzw8xd.PNG Line Whenever I try to open "my files" on Firefox it's just stuck on this screen.Please help ASA

  • Offcejet Pro 8500 a - A910a connection error

    It is history.Perfect work new printer as a printer, but the problem is with ePrint services. Just installed. (2nd printer, same model).-J' installed and configured; I tried to get the email ePrint or ID. The error of the connection said it was an up

  • "I get,' files for the recovery diskette can not be created ' and I can't save my PC

    When I try to back up my PC, I get "files for the recovery diskette could not be created" how to fix this?

  • attempt to remove old contacts from my windows mail,

    whenever I try to delete a contact with contacts of windows messaging, my explore stops and starts again, I tried to suppress-: start, all programs, windows but contacts always stop everything else to do with windows mail works fine, everything.pleas

  • Unit does not start

    I was able to recover a device this morning at my local Office Max and have really enjoyed the last 10 hours to use my new Playbook.  Tonight trying to get my installed chips of debugging, I decided that my PC could not connect to my playbook, and ma