oracle 每组前5行后由学生选择共同选择的愿望拼贴画

kx5bkwkv  于 2022-12-29  发布在  Oracle
关注(0)|答案(1)|浏览(137)

对于子查询,我需要在每组id_student的前五行之后进行选择,并且必须在id_student之间使用id_desireCollage的公共值。
更多解释:在选择了五个愿望之后,为每个学生的愿望选择共同的拼贴画
| 识别号|id_期望拼贴|id_学生|
| - ------| - ------| - ------|
| 1个|1个|1个|
| 第二章|第二章|1个|
| 三个|三个|1个|
| 四个|四个|1个|
| 五个|五个|1个|
| 六个|八个|1个|
| 七|九|1个|
| 八个|七|1个|
| 九|第二章|第二章|
| 十个|十二|第二章|
| 十一|1个|第二章|
| 十二|三个|第二章|
| 十三|六个|第二章|
| 十四|五个|第二章|
| 十五|八个|第二章|
| 十六|九|第二章|
| 十七|七|第二章|
| 十八|四个|三个|
| 十九|三个|三个|
| 二十个|第二章|三个|
| 二十一|1个|三个|
| 二十二|八个|三个|
| 二十三|九|三个|
| 二十四|七|三个|
| 二十五|五个|三个|
就像

select id_desireCollage 
from
    (select * 
     from desires ds 
     where ds.id_desireCollage = desires.id_desireCollage) 
group by (id_student) 
having count(*) > 5

预期结果为:
| id_期望拼贴|
| - ------|
| 七|
| 九|

67up9zun

67up9zun1#

请尝试以下操作:

select id_desireCollage
from
(
  select d.*,
    row_number() over (partition by id_student order by ID) as rn
  from desires d
) T
where rn > 5
group by id_desireCollage
order by count(*) desc
fetch first 1 row with ties

如果你不想使用行号函数(正如你所评论的),你可以尝试下面的**-假设ID列中没有间隙**:

select id_desireCollage
from desires d
where id >= 
  (
    select min(id)+5
    from desires t 
    where t.id_student = d.id_student
  )
group by id_desireCollage
order by count(*) desc
fetch first 1 row with ties

See demo
正如@MatBailie所建议的,如果您的意思是 * common *,即所有学生都选择了id_desireCollage值,则您可以使用以下命令:

select id_desireCollage
from desires d
where id >= 
  (
    select min(id)+5
    from desires t 
    where t.id_student = d.id_student
  )
group by id_desireCollage
having count(*)=
(
  select count(distinct id_student)
  from desires 
)

相关问题