题解 | #汇总各个部门当前员工的title类型的分配数目#

汇总各个部门当前员工的title类型的分配数目

http://www.nowcoder.com/practice/4bcb6a7d3e39423291d2f7bdbbff87f8

-- 窗口函数count,注意distinct 窗口函数不聚合故要distinct
 SELECT
 distinct de.dept_no, dp.dept_name, t.title, count(t.title) over(partition by de.dept_no,t.title order by t.title )
 FROM
 departments dp,dept_emp de,titles t
 where dp.dept_no = de.dept_no
 and de.emp_no = t.emp_no
 order by de.dept_no

-- 法2 联立三表,然后按(dept_no, title)分组
select d.dept_no,
       max(d.dept_name),
       t.title,
       count(t.title)as count
from departments d,dept_emp de,titles t
where de.emp_no=t.emp_no
and de.dept_no=d.dept_no
and de.to_date='9999-01-01'
and t.to_date='9999-01-01'
group by d.dept_no,t.title
order by d.dept_no, t.title
全部评论
法2 不加order by t.title可以超过9.67% 加了超过2.67% 不加竟然也通过了 上哪说理去
点赞 回复 分享
发布于 2021-10-28 11:30

相关推荐

仁者伍敌:难怪小公司那么挑剔,让你们这些大佬把位置拿了
点赞 评论 收藏
分享
评论
3
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务