Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
I have a table as following

Employeeid     Employeename                      Relation ship
16         Malliah                             Self
25         vishnu                             Son
25         Narayana Murthy Konathala      Husband
25         Tulasi                              Self
16         NageswaraRao                        Father
16         Vivek                                Son

<b>
And i want the output as follows</b>

Employeeid     Employeename                      Relation ship
25         Tulasi                              Self
25         vishnu                             Son
25         Narayana Murthy Konathala      Husband
16         Malliah                             Self
16         NageswaraRao                        Father
16         Vivek                                Son


First the sekf relation must be present and in the next rows their relations must have to come as like first self and then next father, husband like that
Posted

Try this...
SQL
select  
Employeeid, Employeename, [Relation ship]
From TableName
Order By EmployeeId desc,
case when [Relation ship] = 'self' then 1 else 2 end

Happy Coding!
:)
 
Share this answer
 
v3
Comments
Arun Vasu 24-May-13 2:56am    
but output is not correct or what he want?
Aarti Meswania 24-May-13 3:01am    
check now :)
Member 9848727 24-May-13 2:58am    
Only 'self' must have to come first and then next any relation any can come...

If i use the above code it is displaying as 1,2,3,4,5 under the column relationship


Aarti Meswania
Aarti Meswania 24-May-13 3:01am    
check updated solution :)
Arun Vasu 24-May-13 3:07am    
NO still its not correct. check my solution
Consider to add hierarchyid column if it possible, update values in this column with respective weights and construct your queries.
This link could be helpful: Tutorial: Using the hierarchyid Data Type[^]
 
Share this answer
 
SQL
create table emp(Employeeid  int, Employeename nvarchar(30), [Relation ship] nvarchar(50))


insert into emp values(16, 'Malliah', 'Self')
insert into emp values(25, 'vishnu', 'Son')
insert into emp values(25, 'Narayana Murthy Konathala', 'Husband')
insert into emp values(25, 'Tulasi', 'Self')
insert into emp values(16, 'NageswaraRao', 'Father')
insert into emp values(16, 'Vivek', 'Son')


select Employeeid, Employeename, [Relation Ship] from
(
select Employeeid, Employeename, [Relation Ship],
CASE 
WHEN [Relation ship]= 'Self' THEN 1 
WHEN [Relation ship]= 'Father' THEN 2 
WHEN [Relation ship]= 'Husband' THEN 2 
WHEN [Relation ship]= 'Son' THEN 3 
END Col4
from emp 
)x order by Employeeid, Col4 ASC




RESULT

Employeeid Employeename Relation Ship
----------- ------------------- ----------------------------------------
16 Malliah Self 1
16 NageswaraRao Father 2
16 Vivek Son 3
25 Tulasi Self 1
25 Narayana Murthy Konathala Husband 2
25 vishnu Son 3

(6 row(s) affected)
 
Share this answer
 
v2
Comments
Member 9848727 24-May-13 3:08am    
i dont want the last column from the output which you hace given to me
@@Arun Vasu
Arun Vasu 24-May-13 3:12am    
now check it.. my updated answer
Member 9848727 24-May-13 3:16am    
ya its working thanku..

@@Arun Vasu
Arun Vasu 24-May-13 3:17am    
close this discussion by mark as answer.

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