SQL Sorting Question

  • I'm having trouble sorting data..

    -----

    select sect_id,cat_id,sub_id,cat_nam

    from category

    order by sect_id,cat_id,cat_nam

    sect_id cat_id     sub_id   cat_nam                                                                                                                                                                                                                                                       

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

    0           0           0           Main

    0           1           0           Boo

    0           2           1           Cat

    0           2           0           Comp

    0           2           5           Flame

    0           3           1           Rock

    I want it to return like this but with each

    sub_id of 0 listed before the alphabetical cat_nam sort

    IE goal:

    sect_id     cat_id      sub_id      cat_nam                                                                                                                                                                                                                                                       

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

    0           0           0           Main

    0           1           0           Boo

    0           2           0           Comp

    0           2           1           Cat

    0           2           5           Flame

    0           3           1           Rock

    Help!

  • This should do it:

    select sect_id,cat_id,sub_id,cat_nam

    from category

    order by

      sect_id,

      cat_id,

      Case When sub_id = 0 then 0 Else 1 End,

      cat_nam

  • Works great

    Thanks!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply