列值之间的关系

时间:2016-05-18 05:57:12

标签: sqlite

所以我有两列,employeeIDReportsTo,这是员工报告的EmployeeID。如果我发现拥有最多员工的employeeID向他们报告如何做到这一点?

5 个答案:

答案 0 :(得分:1)

您必须将ReportsTo列上的记录分组并计算员工数量。您可以使用以下查询。此查询将提供员工分配给Reports To的记录。

select COUNT(distinct EmployeeID) as EmployeeCount,ReportsTo  from TableName group by ReportsTo order by EmployeeCount desc

答案 1 :(得分:1)

我无法清楚地得到你的问题。据我了解,此查询可能会有所帮助。

SELECT count(ReportsTo) as largest_number from tablename GROUP BY ReportsTo ORDER BY largest_number DESC LIMIT 0,1

答案 2 :(得分:0)

SELECT TOP 1 ReportsTo,COUNT(*) AS NoOfReporters 
FROM TABLE_NAME 
GROUP BY ReportsTo 
ORDER BY NoOfReporters DESC

我假设EmployeeID是您表的主键,因此不会重复记录。 ReportsTo是EmployeeID,其他员工报告和NoOfReporters是向他报告的员工。

答案 3 :(得分:-1)

试试这个

select top 1 ReportsTo, count(distinct employeeID)
from InsertYourTableName
group by ReportsTo
order by 2 desc

答案 4 :(得分:-1)

尝试此操作(考虑ReportsToEmployeeID组合具有唯一条目):

SELECT TOP 1 COUNT(ReportsTo) as NoOfEmp, EmployeeID 
FROM TABLE_NAME
GROUP BY EmployeeID
ORDER by NoOfEmp DESC