我有三张表出席,员工,部门 员工表
EmiId -Name -SectorId
123 ABC 1
231 BCD 2
125 WER 1
考勤
AttId -EmpId -Dt
1 123 12/12/2014 9:00
2 231 12/12/2014 10:00
扇区
SectorId -SectorName
1 North Sector
2 East Sector
我的查询是
SELECT COUNT(Attendance.Emp_Id) as AttCount,(select COUNT(*) from Employee) as EmpCount
FROM Employee INNER JOIN
Sector ON Employee.SectorId = Sector.SectorId INNER JOIN
Attendance ON Employee.EmpId = Attendance.EmpId
group by Sector.SectorId
并且我为两者保持相同数量的员工,因此(select COUNT(*) from Employee)
- EmpCount似乎是不正确的。我继续为这两个部门获得相同的数字。虽然Attcount似乎工作得很好。
请帮忙。提前谢谢。
答案 0 :(得分:0)
也许你需要参加LEFT JOIN
SELECT COUNT(Attendance.Emp_Id),(select COUNT(*) from Employee)
FROM Employee JOIN Sector ON Employee.SectorId = Sector.SectorId
LEFT JOIN Attendance ON Employee.EmpId = Attendance.EmpId
group by Sector.SectorId
答案 1 :(得分:0)
您只需从同一张表中选择计数 - 不要指望其他结果。
也许有人可以做得更好
SELECT COUNT(Attendance.Emp_Id),COUNT(case when Employee.empid=attendance.attid then 1 else 0 end)
FROM Employee JOIN Sector ON Employee.SectorId = Sector.SectorId
LEFT JOIN Attendance ON Employee.EmpId = Attendance.Emp_Id
group by Sector.SectorId