0

MySQL-すべてのデータをアルファベット順に並べ替えますが、特定のアイテムを最後に配置します

この質問はすでにMySqlに対して行われていますが、残念ながらこのソリューションはSQLServerでは機能しません。これは私の現在のクエリと結果です:

select ShipMethodID As UseMe, Name As ShowMe 
from Purchasing.ShipMethod
union
Select 0 As UseMe, 'n/a' As ShowMe
order by ShowMe

結果:

5   CARGO TRANSPORT 5
0   n/a
4   OVERNIGHT J-FAST
3   OVERSEAS - DELUXE
1   XRQ - TRUCK GROUND
2   ZY - EXPRESS

次のように並べ替える必要があります。

0   n/a
5   CARGO TRANSPORT 5
4   OVERNIGHT J-FAST
3   OVERSEAS - DELUXE
1   XRQ - TRUCK GROUND
2   ZY - EXPRESS
4

3 に答える 3

4
select * from (
    select ShipMethodID As UseMe, Name As ShowMe 
    from Purchasing.ShipMethod
    union
    Select 0 As UseMe, 'n/a' As ShowMe
) t
order by (case when UseMe = 0 then null else ShowMe end)
于 2013-02-27T13:18:51.630 に答える
3

最初にフィールドで並べ替えて、最初に N/A を取得し、次に実際に並べ替えたいフィールドで並べ替えます。実際に最後に n/a が必要な場合は、order by DisplayOrder DESC, ShowMe.

select      ShipMethodID As UseMe, Name As ShowMe, 1 AS DisplayOrder
from        Purchasing.ShipMethod
union
Select      0 As UseMe, 'n/a' As ShowMe, 0 AS DisplayOrder
order by    DisplayOrder, ShowMe
于 2013-02-27T13:21:39.207 に答える
2

このクエリで試してみてください..

select ShipMethodID As UseMe, Name As ShowMe, 1 As dummycolumn
from Purchasing.ShipMethod
union
Select 0 As UseMe, 'n/a' As ShowMe, 0 As dummycolumn
order by dummycolumn, ShowMe
于 2013-02-27T13:22:27.377 に答える