tags:

views:

76

answers:

6

I am trying to get the following query to display the results in alphabetical order by City except for the fact that I want "Berlin" to appear at the top of the list

So the results would look something like

  • Berlin
  • Algeria
  • Australia
  • Fiji
  • Greece
  • ...

Hope that makes sense,

I currently have the following...

SELECT CompanyName, City
FROM customers
ORDER BY case when City = 'Berlin' END
A: 
SELECT CompanyName, City 
FROM customers 
ORDER BY case when City = 'Berlin' then 1 else 999 END asc
be here now
Please forgive my ignorance but what does this bit mean "then 1 else 999"??
Tom
The data resulting from the Case. When its Berlin it returns 0 else it returns 999
rdkleine
do this query need customers as well in order by clause? Without that its not giving results for numeric columns
Anil
+3  A: 

Almost:

SELECT CompanyName, City
FROM customers
ORDER BY CASE WHEN City = 'Berlin' THEN 0 ELSE 1 END, City
tdammers
Sounds like this should work, but why doesn't it on this SQL tryit page??
Tom
http://www.w3schools.com/sql/sql_tryit.asp
Tom
+1  A: 

Try something like

SELECT CompanyName, City 
FROM customers 
ORDER BY case when City = 'Berlin' THEN 0 ELSE 1 END, City
astander
+2  A: 
SELECT CompanyName, City, CASE WHEN City = 'Berlin' THEN 0 ELSE 1 END AS Ordering
FROM customers
ORDER BY Ordering, City
rdkleine
+1 for SQL-92 compliance i.e. only column names from the `SELECT` clause should be used in the `ORDER BY` clause. I've chosen to turn a blind eye to the missing `AS` keyword i.e. `AS Ordering` ;)
onedaywhen
Good one. Edited the answer
rdkleine
+1  A: 

As blank string appears first in any string ordered list, all other results sorted normally. So this works perfectly:

SELECT CompanyName, City
FROM customers
ORDER BY CASE WHEN City = 'Berlin' THEN '' ELSE City END

Tested with:

CREATE TABLE customers (CompanyName VARCHAR(50), City VARCHAR(50))

INSERT INTO customers VALUES ('Customer1', 'Berlin')
INSERT INTO customers VALUES ('Customer2', 'Algeria')
INSERT INTO customers VALUES ('Customer3', 'Australia')
INSERT INTO customers VALUES ('Customer4', 'Fiji')
INSERT INTO customers VALUES ('Customer5', 'Greece')

SELECT CompanyName, City
FROM customers
ORDER BY CASE WHEN City = 'Berlin' THEN '' ELSE City END

-- OUPUT
-- Customer1    Berlin
-- Customer2    Algeria
-- Customer3    Australia
-- Customer4    Fiji
-- Customer5    Greece
badbod99
A: 

How about using Union? Something like this, for example:

SELECT 1 as Weight, CompanyName, City FROM customers
WHERE city='Berlin'
UNION ALL
SELECT 2 as Weight, CompanyName, City FROM
customers 
WHERE city<>'Berlin'
ORDER BY Weight, City
SPIRiT_1984
That doesn't work; ORDER BY applies to the entire union, and you'll still be left with Algeria up top.
LittleBobbyTables
Thanks for the remark. But now with parenthesis it works correctly. I used the SQL tryit page for checking
SPIRiT_1984
Sorry, no, it's wrong, my mistake. The Union all ignores the ordering actually...
SPIRiT_1984
Now it really works correctly, based on the idea of weighted sorting
SPIRiT_1984