-7

我有一个包含以下值的表

    OperationCode   OperationDesc   OperationOrder
    Repl        Remove          1
    R&I         Remove          1
    Ovrh        Remove          1
    Refn        Prep            1
    Repl        Replace         2
    R&I         Install         2
    Ovrh        Install         2
    Refn        Paint           2

我想编写一个查询来获得以下结果顺序。参见 OperationOrder 列

    OperationCode   OperationDesc   OperationOrder
    Repl            Remove      1
    Repl            Replace     2
    R&I             Remove      1
    R&I             Install     2
    Ovrh            Remove      1
    Ovrh            Install     2
    Refn            Prep        1
    Refn            Paint       2

我正在使用 sql server 2005。

4

4 回答 4

5

这是 SQLFiddel 演示

以下是您可以尝试的示例查询

SELECT * FROM temp
ORDER BY 
CASE OperationCode 
     WHEN 'Repl' THEN 1
     WHEN 'R&I' THEN 2
     WHEN 'Ovrh' THEN 3
     WHEN 'Refn' THEN 4
END, OperationOrder
于 2013-07-09T08:49:50.310 回答
2

看起来你想使用ORDER BY

SELECT OperationId, OperationCode, OperationDesc, OperationOrder
FROM YourTable
ORDER By OperationCode ASC
于 2013-07-09T08:48:07.593 回答
0

如果您需要“OperationCode”的固定顺序,又名:
Repl < R&I < Ovrh < Refn

SELECT *  
FROM YourTable  
ORDER By   
    OperationCode = 'Repl' DESC,  
    OperationCode = 'R&I'  DESC,  
    OperationCode = 'Ovrh' DESC,  
    OperationCode = 'Refn' DESC

"OperationCode = 'Repl'" 将返回 0/1,如果您使用 DESC 订购它,这将放置, 'Repl'值首先结束所有其他内容。

于 2013-07-09T08:58:05.660 回答
0

我想你想要

SELECT 
    ROW_NUMBER() OVER (ORDER BY OperationCode, OperationOrder) as OperationId, 
    OperationCode, 
    OperationDesc, 
    OperationOrder 
FROM SomeTable 
ORDER BY OperationCode, OperationOrder

因为看起来您的 OperationId 已更改每个结果集中的顺序,因此必须是自动生成的数字

于 2013-07-09T08:50:24.843 回答