Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,


I have a doubt on SQL Query. I have a multiple columns in that one of the column is "Order" , values are 1,2,3,4,5....

Now, what i want is on every time i want to display 3 is either first or last. How to do this . By using order by ... syntax is possible to do this

Can any one help me to solve this issue..

Thanks in Advance
Posted
Comments
fjdiewornncalwe 25-Oct-12 9:42am    
Can you give an example of what you want your output to look like? Your question is not clear as it is. Cheers.
joshrduncan2012 25-Oct-12 9:42am    
You want to use order by but have the numbers out of order?
[no name] 25-Oct-12 9:49am    
THIS IS actual data
1
2
3
4
5....
But i want
1
2
4
5...
3 or

3
1
2
4
5...

Try this code block.

SQL
SELECT [ORDER] FROM (
SELECT CASE WHEN [ORDER] = 3 THEN 0 ELSE 1 END ROWNUM,[ORDER] FROM Orders
)A ORDER BY ROWNUM,[ORDER]


If you want to test this query then please run the following whole code block at once

SQL
WITH Orders AS
(
  SELECT 1 [ORDER]
  UNION ALL
  SELECT [ORDER] + 1 FROM Orders WHERE [ORDER] < 10 
)
 
SELECT [ORDER] FROM (
SELECT CASE WHEN [ORDER] = 3 THEN 0 ELSE 1 END ROWNUM,[ORDER] FROM Orders
)A ORDER BY ROWNUM,[ORDER]


Thank you
 
Share this answer
 
v3
use union all and order by
SQL
select order from
(
  select 1 as tempId, order from tbl where order=3
    union all
  select 2 as tempId, order from tbl where order<>3
)
as tbltemp
order by tempid,order

Happy Coding!
:)
 
Share this answer
 
Comments
damodara naidu betha 31-Oct-12 9:12am    
5+
Aarti Meswania 31-Oct-12 9:13am    
thank you! :)
damodara naidu betha 31-Oct-12 9:24am    
I have posted an answer for the same question .. check it :)
How about using Union operator.
http://www.w3schools.com/sql/sql_union.asp
 
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