Click here to Skip to main content
15,891,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have table as follows
INTCATEGORYID   NAME       PARENTID
1               ASD           1
2               EFD           1  
5               FGH           1 
6               JKL           2  
8               VBN           2 
9               FG            3
0               VB            3


I want to have result like follow
INTCATEGORYID   NAME       PARENTID
5               FGH           1 
2               EFD           1  
1               ASD           1
6               JKL           2  
8               VBN           2 
9               FG            3
0               VB            3


means i want to short the subset of whole record set in descending order
like rows which having parentID =1 has to be short in descending order only rest should be in ascending order
Posted
Updated 23-May-12 9:08am
v2

SQL
create table sort_test
(INTCATEGORYID int,
 name varchar(10),
 parentid int)

 insert into sort_test values (1,'ASD',1)
  insert into sort_test values (2,'EFD',1)
 insert into sort_test values (5,'FGH',1)
 insert into sort_test values (6,'JKL',2)
 insert into sort_test values (8,'VBN',3)
 insert into sort_test values (9,'FG',3)
 insert into sort_test values (0,'VB',3)


SQL
Select INTCATEGORYID ,NAME ,PARENTID
from sort_test
order by 
  parentid,
 case when parentid = 1
   then INTCATEGORYID * -1
 else 
   INTCATEGORYID
   end
 
Share this answer
 
v2
Like this:

SQL
SELECT 
    * 
FROM 
    MY_TABLE
ORDER BY
    PARENTID ASC, INTCATEGORYID DESC


Read about ORDER BY[^] here.
 
Share this answer
 
Try this

SQL
select * from
(select top 100 percent * from CATEGORY where PARENTID = 1 order by PARENTID,CATEGORYID desc) t
union all
select * from CATEGORY where PARENTID != 1 order by PARENTID,CATEGORYID
 
Share this 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