Keep order from 'IN' clause

后端 未结 5 1421
花落未央
花落未央 2020-12-11 00:23

Is it possible to keep order from a \'IN\' conditional clause?

I found this question on SO but in his example the OP have already a sorted \'IN\' clause.

My

5条回答
  •  悲哀的现实
    2020-12-11 01:07

    Decode function comes handy in this case instead of case expressions:

    SELECT SomeField,OtherField
    FROM TestResult 
    WHERE TestResult.SomeField IN (45,2,445,12,789)
    ORDER BY DECODE(SomeField, 45,1, 2,2, 445,3, 12,4, 789,5)
    

    Note that value,position pairs (e.g. 445,3) are kept together for readability reasons.

提交回复
热议问题