对分组子集排序和过滤

各科成绩都在前10名的学生

SQL解法

select 姓名
from (select 姓名
      from (select 姓名,rank()
                   over(partition by 科目 order by 成绩 desc) 名次
            from 成绩表)
      where 名次<=10)
group by 姓名
having count(*)=(select count(distinct 科目) from 成绩表)

使用带分组的窗口函数可以方便地计算出各科目的排名,但计算所有前10名的交集遇到了麻烦,需要转换思路改成计算每个学生在前10名中出现的次数,与科目数相同则意味着出现在所有科目的前10名中。

SPL解法

A B
1 =demo.query(“select * from 成绩表”).group(科目)
2 =A1.(~.rank(成绩).pselect@a(~<=10)) 每组前10名所在位置
3 =A1.(~(A2(#)).(姓名)).isect()

使用SPL只要按思路过程写出计算代码即可。