Results 1 to 8 of 8

Thread: order by in decode

  1. #1
    Join Date
    Sep 2006
    Posts
    7

    Unhappy order by in decode

    Hi,

    I'm using decode in the select clause in order to count the number of members in each group and need to display the members exactly in the same way shown in decode. (the order in neither ascending or descending)

    Please let me know how to solve this issue.

    Thanks in advance!

    Sathya

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    Have any samples?

  3. #3
    Join Date
    Sep 2006
    Posts
    7
    Here is the example of what I need. The output should be exactly the same as shown below.

    select decode(upper(group_id), 'W','White','B','Black','O','Orange','G','Green',' P','Pink','B','Blue'
    from table_a
    group by group_id

    Output:
    -------
    White 450
    Black 590
    Orange 920
    Green 260
    Pink 370
    Blue 765

    Thanks,
    Sathya

  4. #4
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    select decode(upper(group_id), 'W','White','B','Black','O','Orange','G','Green',' P','Pink','B','Blue'), count(*)
    from table_a
    group by group_id
    order by decode(upper(group_id), 'W',1,'B',1,'O',3,'G',4,' P',5,'B',6) ASC

    This may still not work as for B you have two possible values Black or Blue, decode will always evaluate B as black.

  5. #5
    Join Date
    Sep 2006
    Posts
    7

    order by in decode

    Hi skhanal,

    That works great! Thanks a lot.
    I have one more question -- whenever there is zero count in any of the colors(White,Black,Green,etc...) it's not seen in the output. I woule like to see all the colors even if it is zero.

    Thanks!

    Sathya

  6. #6
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    You may have to create a table with all the colors you want to use a OUTER JOIN to join to table_a and use 0 for all NULL values in count(*) column.

  7. #7
    Join Date
    Sep 2006
    Posts
    7
    The count zero for each color differs everytime. I cannot point out any particualr color that will be zero everytime.

    Thanks,
    Sathya

  8. #8
    Join Date
    Sep 2006
    Posts
    7
    Can anyone help me on this query, please?

Posting Permissions

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