现在有这样一个表,要实现如下的排序,请高手指教。column1 column2 column3
11 12 13
21 22 23
13 14 15
22 23 24
12 13 14
25 26 27排序后的效果:
column1 column2 column3
11 12 13
21 22 23 12 13 14
25 26 2713 14 15
22 23 24要这样的效果,就是隔行去排序,然后呢,每一条下面的要跟着上面的动。
感觉就像是两条一组的样子。
11 12 13
21 22 23
13 14 15
22 23 24
12 13 14
25 26 27排序后的效果:
column1 column2 column3
11 12 13
21 22 23 12 13 14
25 26 2713 14 15
22 23 24要这样的效果,就是隔行去排序,然后呢,每一条下面的要跟着上面的动。
感觉就像是两条一组的样子。
或者在sql 2005 中借助 row_number(),也可以不用函数.declare @t table(c1 int,c2 int ,c3 int)
insert @t select 11 , 12 , 13
union all select 21 , 22 , 23
union all select 13 , 14 , 15
union all select 22 , 23 , 24
union all select 12 , 13 ,14
union all select 25, 26 ,27
union all select 10,11,12
union all select 23,20,11
union all select 99,88,77
union all select 100,111,112
union all select 112,113,114select identity(int) id ,c1,c2,c3 into # from @tselect c1,c2,c3,gid=isnull(
(select max(
case when id%2=0
then id-1
else id end
) from #
where (
case when id%2=0
then id-1
else id end
)=(
case when a.id%2=0
then a.id-1
else a.id end
)-4
),
case when id%2=0
then id-1
else id end
) from # a
order by gid,id
/*
这里的gid我为了显示方便列出来的,你要不显示,就把 gid=isnull...去掉,把后面的 order by gid中的 gid替换为 isnull这一串.
c1 c2 c3 c4 gid
1 11 12 13 1
2 21 22 23 1
5 12 13 14 1
6 25 26 27 1
3 13 14 15 3
4 22 23 24 3
7 10 11 12 3
8 23 20 11 3
9 99 88 77 5
10 100 111 112 5
11 112 113 114 7*/
drop table #
上面打错了.
union all select 13 , 14 , 15
union all select 22 , 23 , 24
union all select 21 , 22 , 23
--------------
调整一下插入顺序,结果不一样。结果依赖于插入顺序,这样的排序由何意义?如果将奇数行和偶数行,理解为row_number(),任何一种序列都是奇数行跟偶数行,还排什么序呢或许是我的数学太差了。
use tempdb
create table T1
(column1 int, column2 int, column3 int)insert T1
select 11, 12, 13
union all select 21, 22, 23
union all select 13, 14, 15
union all select 22, 23, 24
union all select 12, 13, 14
union all select 25, 26, 27
union all select 10,11,12
union all select 23,20,11
union all select 99,88,77
union all select 100,111,112
union all select 112,113,114GOWith t_cte AS
(select *, row_number() over(order by current_timestamp) as rowNum from T1)select column1, column2, column3, rowNum, seqNum
INTO #T1
from
( select *, 2*(row_number() over(order by column1, column2, column3))-1 as seqNum from t_cte where rowNum%2=1
UNION all
select *, NULL as seqNum from t_cte where rowNum%2=0
) aselect column1, column2, column3 from #T1 t1
order by CASE WHEN seqNum is NOT NULL THEN seqNum ELSE (select seqNum+1 from #T1 t2 Where t2.rowNum=t1.rowNum-1) END
GOdrop table #T1
drop table T1
column1 column2 column3
----------- ----------- -----------
10 11 12
23 20 11
11 12 13
21 22 23
12 13 14
25 26 27
13 14 15
22 23 24
99 88 77
100 111 112
112 113 114
union all select 13 , 14 , 15
union all select 22 , 23 , 24
union all select 21 , 22 , 23
--------------
调整一下插入顺序,结果不一样。结果依赖于插入顺序,这样的排序由何意义? 如果将奇数行和偶数行,理解为row_number(),任何一种序列都是奇数行跟偶数行,还排什么序呢 或许是我的数学太差了。
你可以问一下楼主是要按插入顺序排序, 还是按数据值. 同样的,我也觉得没有意义,但是很多人就是喜欢问这种没有意义的问题.我只管实现就是了.如果按数据值排序的话就简单多了.
2005下用了row_number,又借助了一个临时表. 那么在sql 2k下用一个临时表完成row_number的使用,另一个临时表记录 gid (不是我语句中的gid,单位比我语句中的gid小),写出的代码 is not so nasty
select *, nid=
case when id%2=0
then id-1
else id end
from #1插入临时表,然后语句中套用这个临时表 #1 ,就很容易理解了.如果有的朋友还不能理解,那么, 将插入 #1的记录 再次做个处理插入#2, 到时就会看到 4行记录为一组,就容易理解了.