求助一个排序问题

luoboqingcai 2007-10-25 11:55:06
现在有这样一个表,要实现如下的排序,请高手指教。

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 27

13 14 15
22 23 24

要这样的效果,就是隔行去排序,然后呢,每一条下面的要跟着上面的动。
感觉就像是两条一组的样子。
...全文
115 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
fcuandy 2007-10-26
  • 打赏
  • 举报
回复
或者在sql 2005 中借助 row_number(),也可以不用临时表.
上面打错了.
fcuandy 2007-10-26
  • 打赏
  • 举报
回复
感谢楼主让我复习了下数学,呵呵.

借助临时表生成了identity列. 如果你原表中有连续的id列,则不需要临时表.
或者在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,114

select identity(int) id ,c1,c2,c3 into # from @t

select 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 #
fcuandy 2007-10-26
  • 打赏
  • 举报
回复
请教12楼在 sql 2k下写个 not so nasty as my query 的写法.
2005下用了row_number,又借助了一个临时表. 那么在sql 2k下用一个临时表完成row_number的使用,另一个临时表记录 gid (不是我语句中的gid,单位比我语句中的gid小),写出的代码 is not so nasty
Limpire 2007-10-26
  • 打赏
  • 举报
回复
12是偶数
luoboqingcai 2007-10-26
  • 打赏
  • 举报
回复
大侠实在对不起,刚才没有注意看。
luoboqingcai 2007-10-26
  • 打赏
  • 举报
回复
谢谢大家的关注。
luoboqingcai 2007-10-26
  • 打赏
  • 举报
回复
11 12 13 这样的奇数行去排,然后偶数行要跟着奇数行走。
谢谢大家关注。
Limpire 2007-10-26
  • 打赏
  • 举报
回复
这么早啊:)
dawugui 2007-10-26
  • 打赏
  • 举报
回复
楼主不在?
那洗洗睡了.
fcuandy 2007-10-26
  • 打赏
  • 举报
回复
insert @t select 11 , 12 , 13
union all select 13 , 14 , 15
union all select 22 , 23 , 24
union all select 21 , 22 , 23
--------------
调整一下插入顺序,结果不一样。结果依赖于插入顺序,这样的排序由何意义?

如果将奇数行和偶数行,理解为row_number(),任何一种序列都是奇数行跟偶数行,还排什么序呢

或许是我的数学太差了。



你可以问一下楼主是要按插入顺序排序, 还是按数据值. 同样的,我也觉得没有意义,但是很多人就是喜欢问这种没有意义的问题.我只管实现就是了.

如果按数据值排序的话就简单多了.
Generics 2007-10-26
  • 打赏
  • 举报
回复
although not very efficient, but also not so nasty as fcuandy's query.

set nocount on
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,114

GO

With 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
) a

select 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
GO

drop 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
fcuandy 2007-10-26
  • 打赏
  • 举报
回复
为了便于理将 将
select *, nid=
case when id%2=0
then id-1
else id end
from #1

插入临时表,然后语句中套用这个临时表 #1 ,就很容易理解了.

如果有的朋友还不能理解,那么, 将插入 #1的记录 再次做个处理插入#2, 到时就会看到 4行记录为一组,就容易理解了.
Generics 2007-10-26
  • 打赏
  • 举报
回复
答楼上的大侠: 我说的nasty并不是说您写的代码不好, 而是说您写的代码不大容易理解. 而我的代码效率不一定高, 但每一步都是一目了然, 比较容易理解.
Limpire 2007-10-26
  • 打赏
  • 举报
回复
insert @t select 11 , 12 , 13
union all select 13 , 14 , 15
union all select 22 , 23 , 24
union all select 21 , 22 , 23
--------------
调整一下插入顺序,结果不一样。结果依赖于插入顺序,这样的排序由何意义?

如果将奇数行和偶数行,理解为row_number(),任何一种序列都是奇数行跟偶数行,还排什么序呢

或许是我的数学太差了。
dawugui 2007-10-25
  • 打赏
  • 举报
回复
原来你也没看明白?
dawugui 2007-10-25
  • 打赏
  • 举报
回复
你这个没任何规律,如何排序?
Limpire 2007-10-25
  • 打赏
  • 举报
回复
“分组”算是看明白了,但不明白“分组”的依据是什么

34,589

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧