Click here to Skip to main content
15,910,661 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hai

i have table and in that only one column
the column data is
col1
1
0
1
0

so here i want to present the data in reverse order of col1 is like below
col1
o
1
0
1

how many ways are there to return result above order and how can we return?
please give me answer to this question....
Posted

1 solution

And what is the order logic in the first case? What is the ordering key? Use it in with DESC! If it is the natural order of inserting the data, you should use an autoincrement field or a timestamp and us that field when selecting. A table is not an excel sheet.
Still, although the answer to your original question should not be used, it exists:
SQL
select * from
    (select ROW_NUMBER() OVER (ORDER BY (SELECT 1)) RowNumber ,* from sys.databases) TMP
order by RowNumber desc

Replace the underlined part with your query. This is the general approach, but represent an unnecessary overhead in most cases. You should review your model.
 
Share this answer
 
Comments
Madhu from Bangalore 15-Jun-13 9:21am    
thank you
but here in the order by clause having (select 1)
what is that meaning
Zoltán Zörgő 15-Jun-13 13:58pm    
Yes, because you did not specify any other order. There is no natural ordering that gives you 1,0,1,0. So you probably have some other logic you kept for yourself. This order by clause is only used because ROW_NUMBER is needing one, and I needed ROW_NUMBER to reverse it. But if you have your ORDER BY clause in your query (you could have posted here), you can simply use DESC-
Madhu from Bangalore 16-Jun-13 2:30am    
Thank you for your response.
I am new to SQL Server . now i am in learning stage
the above table is simple table i created and there is no keys and orders like DESC and ASC.
so give me how many ways are there to return reverse order like 0101.
and you mentioned in order by clause is "(select 1)" what is that purpose?
please give brief informatin
Zoltán Zörgő 16-Jun-13 14:35pm    
I already described it: ROW_NUMBER needs an order by clause, and because there is no other ordering, I have used SELECT 1. If you have no other ordering you can use this, and reverse it as I did. Only a concrete order can be reversed - be aware, that the table is a set of rows. A set is unsorted. If you don't specify any ordering in your query nobody will guarantee that the order will be the same next time also.

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