Results 1 to 2 of 2

Thread: [sql2005] order by defined value

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    [sql2005] order by defined value

    hi guys, how would you sort columns according to defined values. suppose

    action
    -----------
    MSC
    GP
    DL
    IDA


    output
    GP
    DL
    MSC
    IDA

    i was thinking something like this:
    Code:
    select action from table1
    order by 'gp','dl','msc','ida'
    but im getting error
    Learn something new every .001 second.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [sql2005] order by defined value

    Create another table containing two fields, the value you want to sort on ('gp', 'dl', etc), and a number representing the order it should be in (1,2, etc). The numbers do not need to be continuous (so you could have 50, 1000, etc), which might simplify things if you need to add new values later.

    You can then Left-Join to the other table (so that other values get displayed), and sort by the numeric field.

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