Search code examples
sqlsql-serversql-order-bywhere-clause

Order in where in


I have this:

select CODART as 'MODELO / REF', DESCLIN as DESCRIPCION, UNIDADES as CANTIDAD 
from LINEPEDI 
where IDPEDV in ('69782','69780','68931','69781')

In the result I would like it to return me in the order I have put it but by default it returns it sorted by id.

Would anyone know how to tell him not to order it by id?


Solution

  • To sort rows in the result set you need to use ORDER BY. Otherwise the engine is free to return them with any ordering.

    You can do:

    select
      CODART as "MODELO / REF", 
      DESCLIN as DESCRIPCION, 
      UNIDADES as CANTIDAD 
    from LINEPEDI 
    where IDPEDV in ('69782','69780','68931','69781')
    order by case IDPEDV when '69782' then 1
                         when '69780' then 2
                         when '68931' then 3
                         when '69781' then 4
             end