SQL中的多数功能

SQL中的多数功能,第1张

SQL中的多数功能

(假设您使用的是Oracle,因为在MySQL中您始终无法编写自己的聚合)

我认为您可以使用窗口功能来做到这一点

这等效于第一个示例

select distinct agefrom (  select age,          dept,         count(*) over () as total_count,          count(*) over (partition by age) as age_count  from emp) where age_count >= total_count / 2;

通过简单地将部门添加到分区(组)定义中,这应该可以为您提供所需的信息:

select distinct agefrom (  select age,          dept,         count(*) over (partition by dept) as dept_count,          count(*) over (partition by dept, age) as age_count  from emp) where age_count >= dept_count / 2;


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5640397.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存