我有这两个表,我需要找到员工人数最多的部门名称。
其他解决方案不适用于Oracle,因此我发布了这个问题。另外,如果可以对查询进行彻底的解释,这将非常有帮助,因为我发现它很难可视化。
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^ABC--[0-9A-Z-.]{24}--[a-z0-9]{40}$"
test_str = "ABC--2000-01-10X13-11-44.237Z--572b3b7681572b3b7681572b3b7681572b3b7681"
matches = re.search(regex, test_str, re.IGNORECASE)
if matches:
print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group()))
for groupNum in range(0, len(matches.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.group(groupNum)))
到目前为止,我可以设法吸引最多的员工。因此,我在考虑是否可以编写另一个子查询,以再次计算部门中的员工人数,并将其与我在第一个查询中计算出的max_num_emp进行比较。
这是检索最大员工数的查询。它不会返回dept_no。
EMPLOYEE
EMPNO EMPNAME MANAGER SALARY DEPT_NO
1 Puja 6 30000 2
2 Purabi 1 15000 3
3 Barun 6 23000 2
4 Sudha 1 20000 1
5 Amal 2 20000 1
6 Rakesh 3 30000 4
DEPARTMENT
Dept_No Dept_Name Location
1 Production LaneA
2 Marketing LaneB
3 Sales LaneC
4 HR LaneD
预期产量
select count(dept_no)
from employee
group by dept_no
order by count(dept_no) desc
fetch first row only;
我还可以在查询中添加dept_no列,然后我将不得不以某种方式找出如何获得最大值,并且由于查询违反了某些规则,这在某种程度上给了我错误。我实际上已经尝试过执行max( above query )。
因此,我想到的只是获得最大的员工人数,然后确定拥有这么多员工的所有部门并显示其姓名。
答案 0 :(得分:3)
您有一个有效的查询,需要将其连接到表department
:
select d.Dept_Name
from department d inner join (
select dept_no
from employee
group by dept_no
order by count(*) desc
fetch first row only
) t
on t.dept_no = d.dept_no
编辑
试试这个(我不能尝试):
select d.dept_name
from department d inner join (
select x.dept_no from (
select dept_no, rank() over (order by count(*) desc) rn
from employee
group by dept_no
order by count(dept_no) desc
) x
where x.rn = 1
) t
on t.dept_no = d.dept_no
答案 1 :(得分:2)
您可能已经使用FETCH..FIRST
而不是WITH TIES
来使用ONLY
语法。
SELECT d.dept_name
FROM department d
JOIN employee e ON d.dept_no = e.dept_no
GROUP BY d.dept_name
ORDER BY COUNT(*)
DESC FETCH FIRST 1 ROW WITH TIES ;
答案 2 :(得分:0)
如果您不查找重复项,则:
Private Sub SearchTxt_TextChanged(sender As Object, e As EventArgs) Handles SearchTxt.TextChanged
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "server=localhost;userid=root;password=;database=agents"
Dim dbDataSet As New DataTable
Dim DV As New DataView(dbDataSet)
DV.RowFilter = String.Format("FULLNAME Like '%" & SearchTxt.Text & "&'")
DataGridView1.DataSource = DV
End Sub