need same order as the IN list of ids ...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sereypong
    New Member
    • Apr 2008
    • 1

    need same order as the IN list of ids ...

    Hi,

    I have this Query : SELECT field_name FRM meta WHERE id IN ('13','11','7', '8','9','10','1 2')

    I want the rows to be display in the same order as the IN list
    of ids.Any ideas?

    All the best wished,
    Regards and Thanks,
    Pong
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Originally posted by sereypong
    Hi,

    I have this Query : SELECT field_name FRM meta WHERE id IN ('13','11','7', '8','9','10','1 2')

    I want the rows to be display in the same order as the IN list
    of ids.Any ideas?

    All the best wished,
    Regards and Thanks,
    Pong
    Will this work?
    Code:
    SELECT 
    field_name FRM meta WHERE id IN ('13','11','7','8','9','10','12')
    order by case 
    when id = 13 then 1
    when id = 11 then 2
    else id
    end
    if not put in a subquery

    Code:
    (SELECT top 100 PERCENT
    sortid = case 
    when id = 13 then 1
    when id = 11 then 2
    else id
    end,
    field_name FRM meta WHERE id IN ('13','11','7','8','9','10','12'))
    order by sortid
    try the first one first. it's faster

    --CK

    Comment

    • Brad Orders
      New Member
      • Feb 2008
      • 21

      #3
      You could also consider putting the values inside the "IN" statement in their own table. Then you could solve the problem by doing a simple join, and order by the identity ID in the new table.

      If performance is important, this is a solution worth considering.

      Comment

      Working...