Sql-Server
自定義訂購方式
還有其他方法可以用於
ORDER
我的結果嗎?20150507094958A DISPOSED 2015-05-07 09:50:00.853 Samuel MONEY 20150507094958A DISPOSED 2015-05-22 09:25:00.000 Jeff AUTOMOBILES 20150507094958A DISPOSED 2015-05-26 09:39:00.000 Joel FIREARMS 20150507094958A DISPOSED-READY 2015-05-29 09:57:00.000 Samuel MONEY 20150706132447A OUT 2011-10-19 11:25:00.000 Samuel AUTOMOBILES 20150406110457A OUT 2015-04-02 11:07:00.000 Shane FIREARMS 20150406105740A OUT 2015-04-06 11:00:00.000 Cordero FIREARMS 20150406105740A IN 2012-03-27 21:15:00.000 Alex BICYCLES
我想把所有的都
FIREARMS
放在頂部,然後仍然按任何其他列排序。
有幾種方法可以做到這一點。但是,這可能對您有用:
select myTable.* from myTable order by (case myCol when 'FIREARMS' then '' else myCol end);
我會冒險猜測你在尋找什麼。我假設該表被稱為
test
,包含 、 等條目的列FIREARMS
被MONEY
稱為category
,當然您還有其他列。因此,使用簡化表和一些數據定義為
CREATE TABLE test ( id integer, person varchar(30), category varchar(30)); INSERT INTO test VALUES (1,'Samuel', 'MONEY'); INSERT INTO test VALUES (2,'Jeff', 'AUTOMOBILES'); INSERT INTO test VALUES (3,'Joel', 'FIREARMS'); INSERT INTO test VALUES (4,'Samuel', 'MONEY'); INSERT INTO test VALUES (5,'Samuel', 'AUTOMOBILES'); INSERT INTO test VALUES (6,'Shane', 'FIREARMS'); INSERT INTO test VALUES (7,'Cordero', 'FIREARMS'); INSERT INTO test VALUES (8,'Alex', 'BICYCLES');
然後,我會將查詢寫為
SELECT * FROM test ORDER BY CASE category WHEN 'FIREARMS' THEN '1' ELSE category END, person;
這使您可以首先將
FIREARMS
條目放在頂部,然後按輔助列排序,在這種情況下,我選擇person
了輔助ORDER BY
列。您可以使用此**SQL FIDDLE**來確認它是否滿足您的需求。