MS SQL QUERY (Hard Query)

Hi I have 1 problem.
How I can write MS SQL query for given task:

ID   |    Name    |   Price    |
1     |   Gogo      |       5.00
2     |   Kiko        |     3.00
3     |  Ceco        |      5.00
4     |   Ceco       |      4.00
5     |  Bobo       |      3.00

How I can to ordered table like this:  Order By Name, Price  but name Ceco to be TOP and after that another Names to be ordered by ASK and his Price to ORdered ASK?????

ID   |    Name    |   Price    |
4     |   Ceco       |      4.00
3     |  Ceco        |      5.00
5     |  Bobo       |      3.00
1     |   Gogo      |       5.00
2     |   Kiko        |     3.00


PLEASE HELP ME
Tsvetomir TsanevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
SELECT ID, Name, Price
FROM YourTable
ORDER BY CASE WHEN NAME='Ceco' THEN 1 ELSE 2 END, Name, Price

To test, copy-paste the below into your SSMS and execute
CREATE TABLE #tmp (id int, Name varchar(10), Price int)

INSERT INTO #tmp (id, Name, Price) 
VALUES  (1, 'Gogo', 5), (2, 'Kiko', 3), (3, 'Ceco', 5), (4, 'Ceco', 4), (5, 'Bobo', 3)

SELECT ID, Name, Price
FROM #tmp
ORDER BY CASE WHEN NAME='Ceco' THEN 1 ELSE 2 END, Name, Price

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vikas GargBusiness Intelligence DeveloperCommented:
Hello,

You can try this

SELECT ID, Name, Price,CASE WHEN NAME='Ceco' THEN 1 ELSE 0 END RN
FROM YourTable
ORDER BY RN desc,Name, Price 

Open in new window

1
Rgonzo1971Commented:
Hi,

pls try

SELECT Table.ID, Table.Name, Table.Price
FROM Table
ORDER BY CASE WHEN NAME='Ceco' THEN Null ELSE NAME END, Table.Price;

Open in new window

Regards
1
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tsvetomir TsanevAuthor Commented:
Thank you very much!
0
Vikas GargBusiness Intelligence DeveloperCommented:
@Jim,

Well when I was writing my answer you had give a different answer.

And after my answer you have modified the logic as per my answer which has been awarded.

Anyways Nice job!
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
@Vitor - The only modification I made to my comment was to fix a typo which didn't alter any logic, make the CASE block bold, and then to add the SSMS testing code.
0
Vikas GargBusiness Intelligence DeveloperCommented:
Its Ok Jim

You previous query was this

SELECT ID, Name, Price
FROM YourTable
ORDER BY CASE WHEN NAME='Ceco' THEN 1 ELSE 2 END, Ceco, Price 

Open in new window


Which is not correct.

But its very ok now.

Have a nice day
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Correct, I changed Ceco to Name within two minutes of the original post, as I was developing the SSMS test code.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.