我有一个表,它看起来像多列排序(SQL Server 2005中)
Col1 col2 col3 col4 col5
1 5 1 4 6
1 4 0 3 7
0 1 5 6 3
1 8 2 1 5
4 3 2 1 4
剧本是
declare @t table(col1 int, col2 int, col3 int,col4 int,col5 int)
insert into @t
select 1,5,1,4,6 union all
select 1,4,0,3,7 union all
select 0,1,5,6,3 union all
select 1,8,2,1,5 union all
select 4,3,2,1,4
我想要的输出为每列按升序进行排序,即
Col1 col2 col3 col4 col5
0 1 0 1 3
1 3 1 1 4
1 4 2 3 5
1 5 2 4 6
4 8 5 6 7
我已经受如下因素程序解决了这个问题
Select
x1.col1
,x2.col2
,x3.col3
,x4.col4
,x5.col5
From (Select Row_Number() Over(Order By col1) rn1, col1 From @t)x1
Join(Select Row_Number() Over(Order By col2) rn2, col2 From @t)x2 On x1.rn1=x2.rn2
Join(Select Row_Number() Over(Order By col3) rn3, col3 From @t)x3 On x1.rn1=x3.rn3
Join(Select Row_Number() Over(Order By col4) rn4, col4 From @t)x4 On x1.rn1=x4.rn4
Join(Select Row_Number() Over(Order By col5) rn5, col5 From @t)x5 On x1.rn1=x5.rn5
但我对此解决方案并不满意。
有没有更好的方法来实现? (使用基于集合的方法)
如果是这样,任何人都可以请示例。
感谢
你能解释一下你为什么要这样做吗?这将表明你的数据库的设计也许不是最佳的... – 2010-05-24 04:24:43
我怀疑你会更好,询问你正在尝试做整个事情... – 2010-05-24 04:44:27
发售的声誉不会帮助解决一些东西,基本上是无法解决的,由于要求。你应该接受建议,即你正在尝试做的事情是根本错误的。 – 2010-05-28 03:51:50