Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Reorder Rows

Author  Topic 

kishoreg
Starting Member

11 Posts

Posted - 2010-02-24 : 04:54:25
Hi,
I have two columns Col1, Col2 and both contains same data. Now i want to shuffle(Reorder) the rows in Col2 and the rows in Col1 should be unchanged.

Please let me know the query for this.

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-02-24 : 04:57:46
Do you need this?

select Col1, Col2 from table_name order by newid()

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-24 : 05:00:21
Post some sample data with expected result

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

kishoreg
Starting Member

11 Posts

Posted - 2010-02-24 : 05:04:38
Original Table
Col1 col2
1 1
2 2
3 3
4 4
5 5

Result
Col1 Col2
1 4
2 1
3 5
4 2
5 1

I want col2 to be shuffled. The col's doesn't have any pk's.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-02-24 : 05:14:29
quote:
Originally posted by kishoreg

Original Table
Col1 col2
1 1
2 2
3 3
4 4
5 5

Result
Col1 Col2
1 4
2 1
3 5
4 2
5 1

I want col2 to be shuffled. The col's doesn't have any pk's.



Do you want the columns sorted this way?
Result
Col1 Col2
1 5
2 4
3 3
4 2
5 1


PBUH
Go to Top of Page

kishoreg
Starting Member

11 Posts

Posted - 2010-02-24 : 05:17:46
I want Col2 to be shuffled in any way, except ascending & descending order.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-02-24 : 05:22:12
[code]
select * from

(
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5
)t(x) order by NEWID()
[/code]

PBUH
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-02-24 : 05:29:18
This thread can be helpful:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=96175


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-24 : 05:33:15
Try this


select t1.col1,t2.col2 from
(
select row_number() over(order by col1) as sno,col1 from table1
) as t1
inner join
(
select row_number() over(order by newid()) as sno,col2 from table1
) as t2
on t1.sno=t2.sno


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -