Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help with query

Author  Topic 

Darkmatter5
Starting Member

17 Posts

Posted - 2012-09-06 : 14:38:40
Here's my current query:


SELECT ClientCompanyID, CompanyName
FROM clientCompanies
ORDER BY CompanyName ASC


How can I have it put ClientCompanyID = 1 at the very top and then order everything else, but not include ClientCompanyID = 1, so that it stays at the top of the results?

Thanks!

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-06 : 15:06:02
quote:
Originally posted by Darkmatter5

Here's my current query:


SELECT ClientCompanyID, CompanyName
FROM clientCompanies
ORDER BY CompanyName ASC


How can I have it put ClientCompanyID = 1 at the very top and then order everything else, but not include ClientCompanyID = 1, so that it stays at the top of the results?

Thanks!

Add another piece to the ORDER BY clause like this:
SELECT ClientCompanyID, CompanyName
FROM clientCompanies
ORDER BY
CASE WHEN ClientCompanyID = 1 THEN 1 ELSE 2 END,
CompanyName ASC
Go to Top of Page
   

- Advertisement -