如果指定一个分母的上限,再把最简分数按从小到大的顺序排列,比如说,当分母不大于7时,我们可以得到以下17个分数:1/7,1/6,1/5,1/4,2/7,1/3,2/5,3/7,1/2,4/7,3/5,2/3,5/7,3/4,4/5,5/6,6/7。这就是所谓的“法莱数列”(Farey序列)。

var n number
exec :n:=7
with t as(select level l from dual connect by level<=:n),
t2 as(select a.l/b.l v,a.l||'/'||b.l s,
row_number()over(partition by a.l/b.l order by a.l) rn
from t a,t b
where a.l<b.l)
select listagg(s,',')within group(order by v)s
from t2 where rn=1;
-------------------------------------------------------------------
1/7,1/6,1/5,1/4,2/7,1/3,2/5,3/7,1/2,4/7,3/5,2/3,5/7,3/4,4/5,5/6,6/7

SQL因为内置排序功能,很容易把相等的分数值分组在一起