How to SORT in order as entered in SQL Server?

前端 未结 5 798
猫巷女王i
猫巷女王i 2020-12-04 00:44

I\'m using SQL Server and I\'m trying to find results but I would like to get the results in the same order as I had input the conditions.

My code:

S         


        
5条回答
  •  星月不相逢
    2020-12-04 01:23

    You can replace IN with a JOIN, and set a field for ordering, like this:

    SELECT AccountNumber , EndDate
    FROM Accounts a
    JOIN (
        SELECT 212345 AS Number, 1 AS SeqOrder
    UNION ALL
        SELECT 312345 AS Number, 2 AS SeqOrder
    UNION ALL
        SELECT 145687 AS Number, 3 AS SeqOrder
    UNION ALL
        ... -- and so on
    ) AS inlist ON inlist.Number = a.AccountNumber
    ORDER BY inlist.SeqOrder
    

提交回复
热议问题