Sorting Rows in a UserDefined(Custom) way. [Resolved]

Posted by Chvrsri under Sql Server on 10/25/2010 | Points: 10 | Views : 2814 | Status : [Member] [Moderator] [MVP] | Replies : 5
Hi all,

Can any one tell me how can we Sort the Entries(Rows) in custom or UserDefind way?

i have
Column1 Column2
abc 1
def 2
ghi 3

now want somethng like 'def' on Top

Column1 Column2
def 2
abc 1
ghi 3

I want this within a query !!!!

Thanks,
Radha Srikanth



Responses

Posted by: Chvrsri on: 10/25/2010 [Member] [Moderator] [MVP] Silver | Points: 50

Up
0
Down

Resolved
My actual requirement is getting my desired row on top and having my specific order. order by gives either Ascending or Descending. But i want a Custom way to have my Defined Order..
For that after googling it i got it....

//Creating a table called #temp

create table #temp( id int primary key,Pref_Score varchar(30)unique)

//and inserting it in the order i require
insert into #temp values(1,'Exceptional')
insert into #temp values(2,'Developing')
insert into #temp values(3,'Strong')
insert into #temp values(4,'Solid')
insert into #temp values(5,'UnAcceptable')

//and then selecting it ..
Select * from #temp order by id

Previously my problem is Even though i insert in the order i require SQL server automatically Sorting it...
So to stop and place my own order i declared a ID column and accessed the values from it....

Thanq Saravanan and RamKumar for your help.

Thanks,
Radha Srikanth

Chvrsri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: ramkumarsubramaniyamust-global-6869 on: 10/25/2010 [Member] Starter | Points: 25

Up
0
Down
select Column1, Column2 from tbl_name
order by Column1

P.S. here tbl_name -> your table name and Column1 and Column2 are your columns.


Chvrsri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chvrsri on: 10/25/2010 [Member] [Moderator] [MVP] Silver | Points: 25

Up
0
Down
Hi Ramkumar,
I dont want to sort my data using Columns , i want to use my ROWS to sort as i mentioned clearly......Even wht u said will sort in ascending order, thats not what i wanted.

Thanks,
Radha Srikanth

Chvrsri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: ramkumarsubramaniyamust-global-6869 on: 10/25/2010 [Member] Starter | Points: 25

Up
0
Down
hi,
if you sort by column, the row also automatically gets sorted.

pls try.

Chvrsri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 10/25/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi Srikanth,

What's your Exact output from your query?

If you need this type of output means,
Column1  Column2

def 2
abc 1
ghi 3
Query --> Select Column1,Column2 from Table1 order by Column2


else edit your question and what you need for exact output.

Ramkumar answer is correct for your question.

Thanks,
T.Saravanan

Chvrsri, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response