我提到了这个问题
我正在尝试获取每个 id 的前 5 个序列。 我正在使用 Sybase Ase 16.0,我相信不支持排名/分区
我的预期输出是这样的
id sequence
-- --------
1 1
1 2
1 3
1 4
1 5
2 1
2 2
3 1
3 2
3 3
我尝试了下面的方法,但不确定我是否在 sybase 中得到了上面那样的输出
select sysprocedures.id, sysprocedures.sequence,
(select count('x') from sysprocedures t2 where sysprocedures.id = t2.id and sysprocedures.sequence < t2.sequence) as myrank from sysprocedures;
sysprocedures.sequence
编号以 0
开头,因此我们可以在 where
子句中利用这一点:
select id,
sequence
from sysprocedures
where sequence <= 4
order by 1,2
go
在
master
数据库中运行此命令会生成:
id sequence
----------- -----------
889051172 0
889051172 1
889051172 2
889051172 3
889051172 4
921051286 0
921051286 1
921051286 2
921051286 3
921051286 4
... snip ...
注意:已在
ASE 16.0 SP04 PL04
上验证
假设 a)
sysprocedures
查询是对最小示例的尝试,因此 b) 建议的答案不适用于 OP 的现实世界数据...OP 可能想提出一个新问题并提供更多信息现实的数据集和解释。例如:
sequence
并不总是相同的数字(例如,id=1
有一个min(sequence)=3
,id=7
有一个min(sequence)=17
等)sequences
,一系列id
中可能存在间隙(例如,id=1
序列系列是3, 4, 5, 36, 42, 126
)