Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

SQL SERVER - Order By

hi im using sql server 2019 use northwind database I want customers to be sorted by country. With the difference that first it is the United States, Germany and France and then it is an ascending order according to the rest of the countries.

question from:https://stackoverflow.com/questions/65941924/sql-server-order-by

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@Gordon's answer will keep mentioned countries in the last.

You need to use case expression as follows:

order by (case when country in ('United States', 'Germany', 'France') then 0
          else 1 
          end),
         country

If you want specific order within the mentioned countries then use multiple when as follows:

order by (case when country = 'United States' then 0
               When country = 'Germany' then 1
               When country = 'France' then 2 
               else 3 
          end),
         country

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...