Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I'm not sure there is even a way to do this, but thought I would ask anyway.

I have the following example output from a query:

Name     Country   Number
--------------------------
Harry    Germany   2
Harry    UK        5
Harry    Portugal  6   
Zoe      Spain     10
Zoe      UK        5   

For the task in hand, we use the data for UK most often, and would therefore like this at the top of the results set. The data is grouped by name, and then split in to country and number, and it needs to be ordered by Name, and then Country (but with UK at the top of each grouping). The desired output would be:

Name     Country   Number
--------------------------
Harry    UK        5   
Harry    Germany   2    
Harry    Portugal  6   
Zoe      UK        5
Zoe      Spain     10

As you can see, UK is at the top of each grouping.

I would appreciate any advice, even if this cannot be done.

Thanks in advance

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

Use this syntax:

ORDER BY name,CASE WHEN country='UK' THEN 0 ELSE 1 END,country
share|improve this answer
    
So simple!! Thank you, this has been puzzling me for a while! –  zatgolf Nov 5 '13 at 14:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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