2

Are there other ways to use ORDER on my results?

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

I want to get all the FIREARMS on the top and then still order by any other column.

3

There are several ways to do it. However, this may work for you:

select 
    myTable.* 
from myTable
order by (case myCol when 'FIREARMS' then '' else myCol end);
1

I'll venture a guess at what you're looking for. I've made the assumption that the table is called test, the column which contains the entries for FIREARMS, MONEY, etc. is called category, and of course you have your other columns.

So, with a simplified table and some data defined as

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');

Then, I would write the query as

SELECT * FROM test
ORDER BY CASE category WHEN 'FIREARMS' THEN '1'
ELSE category END, 
person;

which lets you first order to put FIREARMS entries at the top, and then sort by a secondary column, where in this case I've chosen person as the secondary ORDER BY column.

You can use this SQL FIDDLE to confirm it works for your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.