Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table named 'TITLE' .Table structure is like this

TITLE_ID || TITLE_NAME
========================
1 || Herr
2 || Frau
3 || Kind
4 || Fam

What I have tried:

I wrote a query for selecting the data ,"SELECT TITLE_ID,TITLE_NAME FROM TITLE ORDER BY 'TITLE_ID' asc" , got the result like this,

TITLE_ID || TITLE_NAME
========================
1 || Herr
2 || Frau
3 || Kind
4 || Fam

But I want " 2 || Frau " in first position and " 1 || Herr " in second position . Is it possible by writing the select query ?
Posted
Updated 21-Feb-16 21:03pm
Comments
dan!sh 22-Feb-16 1:23am    
On what basis do you want to order the data?
vinu paul 22-Feb-16 1:47am    
Ascending order , but need " 2 || Frau " in first position and " 1 || Herr " in second position
dan!sh 22-Feb-16 2:10am    
Ascending order of what? Name? Id? Something else?
vinu paul 22-Feb-16 2:18am    
Ascending order of id
dan!sh 22-Feb-16 2:26am    
You understand 1 < 2. Right? So record with id 1 WILL come before 2.

Have you tried to use ORDER BY + CASE WHEN ...?

SQL
SELECT ...
FROM ...
ORDER BY CASE TITLE_ID WHEN 2 THEN 0 ELSE TITLE_ID END


See:
ORDER BY CASE | SQL with Manoj[^]
SQL SERVER - CASE Statement in ORDER BY Clause - ORDER BY using Variable - Journey to SQL Authority with Pinal Dave[^]
 
Share this answer
 
Comments
vinu paul 22-Feb-16 3:29am    
Now i tried this.. got the result..

I tried another way,will also get the result
SELECT TITLE_ID,TITLE_NAME FROM TITLE WHERE TITLE_ID =2
union all

SELECT TITLE_ID,TITLE_NAME FROM TITLE WHERE TITLE_ID !=2


Thanks :)
Maciej Los 22-Feb-16 3:45am    
Hmmm... Your approach is wrong in performance aspect. As you can see, you have gto 2 select statements and it causes that database is queried twice. Got it?
vinu paul 22-Feb-16 3:52am    
Ya got it. Thank you very much for your valuable informations :)
Maciej Los 22-Feb-16 3:56am    
You're very welcome.
Cheers,
Maciej
You are ordering records from the "TITLE_ID" field, You should order query by "TITLE_NAME" like that:

SELECT TITLE_ID,TITLE_NAME FROM TITLE ORDER BY TITLE_NAME

This query will return result as

TITLE_ID || TITLE_NAME
========================
1 || Fam
2 || Frau
3 || Herr
4 || Kind


Ashish Nigam
 
Share this answer
 
v2
Comments
vinu paul 22-Feb-16 1:49am    
I tried this,but in this query "1 || Fam" come first position and again "2 || Frau" in second position

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900