DBAsupport.com Forums - Powered by vBulletin
Results 1 to 7 of 7

Thread: Order by in oracle

  1. #1
    Join Date
    Jun 2001
    Posts
    316

    Order by in oracle

    Hi,

    I have this table A as

    Cust_id cust_2_id flag
    1 1 new
    2 2 new
    3 3 new
    4 4 new
    1 1 skip
    1 1 skip
    2 2 skip
    4 4 skip

    I want to order by such that...for each cust, the record with new flag appears first..then skip..then next record...as below

    Cust_id cust_2_id flag
    1 1 new
    1 1 skip
    1 1 skip
    2 2 new
    2 2 skip
    3 3 new
    4 4 new
    4 4 skip

    Can anyone help me with this?

    Thanks

  2. #2
    Join Date
    Jun 2001
    Posts
    316
    1 more thing..

    No alalytical fucntion...as i would be using this in pl/sql...version < 9

  3. #3
    Join Date
    Sep 2002
    Location
    England
    Posts
    7,334
    you can use analytical functions with an execute immedite if you want

  4. #4
    Join Date
    Dec 2000
    Location
    Ljubljana, Slovenia
    Posts
    4,439
    So what is so special about this ordering? Based on your description, the following will do just fine:

    .... ORDER BY Cust_id, cust_2_id, flag;
    Jurij Modic
    ASCII a stupid question, get a stupid ANSI
    24 hours in a day .... 24 beer in a case .... coincidence?

  5. #5
    Join Date
    May 2000
    Location
    ATLANTA, GA, USA
    Posts
    3,135
    Modic,

    I think "helpme" might have thought if some other value is stored in the flag column - other than new and skip, then what could be the result.

    May be he needs to add decode in the order by clause.

    Code:
    SQL> desc v1
     Name              Null?    Type
     ----------------- -------- --------------------
     ID1                        NUMBER(38)
     ID2                        NUMBER(38)
     TXT                        VARCHAR2(10)
    
    SQL> select * from v1 order by id1, id2, txt ;
    
           ID1        ID2 TXT
    ---------- ---------- ----------
             1          1 new
             1          1 skip
             1          1 skip
             1          1 skip
             2          2 new
             2          2 skip
             2          2 skip
    
    7 rows selected.
    
    The above code works as long as the TXT 
    column has only 2 values.
    
    SQL> insert into v1 values (1,1,'abc');
    
    1 row created.
    
    SQL> commit;
    
    Commit complete.
    
    SQL> select * from v1
         order by id1, id2, txt ;
    
           ID1        ID2 TXT
    ---------- ---------- ----------
             1          1 abc     -----------> watch
             1          1 new
             1          1 skip
             1          1 skip
             1          1 skip
             2          2 new
             2          2 skip
             2          2 skip
    
    8 rows selected.
    
    SQL>  select * from v1
          order by id1, id2, decode(txt,'new',1,2) ;
    
           ID1        ID2 TXT
    ---------- ---------- ----------
             1          1 new
             1          1 skip
             1          1 skip
             1          1 skip
             1          1 abc    <--------- watch
             2          2 new
             2          2 skip
             2          2 skip
    
    8 rows selected.
    Tamil

  6. #6
    Join Date
    Jun 2001
    Posts
    316
    jmodic...actually yes.....i THOUGHT it wont work...so tried all other stuff.....:-&..

  7. #7
    Join Date
    May 2003
    Location
    Pretoria, Rep of South Africa
    Posts
    191
    The closest I could get to your desired result :

    SELECT V.ID1,V.ID2,V.TXT
    FROM V1 V
    GROUP BY ROLLUP (V.ID1,V.ID2,V.TXT)

    or just

    SELECT V.ID1,V.ID2,V.TXT
    FROM V1 V
    GROUP BY (V.ID1,V.ID2,V.TXT)

    rollup result:
    1 1 NEW
    1 1 SKIP
    1 1
    1
    2 2 NEW
    2 2 SKIP
    2 2
    2
    3 3 NEW
    3 3
    3
    4 4 NEW
    4 4 SKIP
    4 4
    4
    Able was I ere I saw Elba

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width