
(假设您使用的是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;
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)