我正在使用SQL Server 2008 R2并具有以下查询。我基本上试图返回两列,即员工的姓名以及他们为当前月份拨打的总人数。我已经得到了运行的查询,但是当它运行时它需要相当长的时间并且它返回大量不正确的数字来计算总呼叫数。任何人都可以帮我一把吗?
Select A.Name, count(1) As TotalCalls
From Calls B INNER JOIN Employees A on B.EmployeeID = A.TechID
Where A.Name IN ('Steven', 'John', 'Mark', 'Jason', 'Tom')
AND Month(B.CreationDate) = Month(GetDate()) and Year(B.CreationDate) = Year(GetDate())
Group By A.Name
编辑: 我已经尝试了以下查询,我认为我现在非常接近。它返回的结果集准确无误,我只是不确定如何使此查询仅查看当前月份。
Select A.Name, B.CallCount
From Employees A Inner Join (Select EmployeeID, Count(*) As CallCount From Calls Group By EmployeeID) B
On A.TechID = B.EmployeeID
Where A.Name IN ('Steven', 'John', 'Mark', 'Jason', 'Tom')
Group By A.Name, B.CallCount
答案 0 :(得分:0)
这个怎么样?
Select A.Name, count(A.name) As TotalCalls
From Calls B INNER JOIN Employees A on B.EmployeeID = A.TechID
Where A.Name IN ('Steven', 'John', 'Mark', 'Jason', 'Tom')
AND Month(B.CreationDate) = Month(GetDate()) and Year(B.CreationDate) = Year(GetDate())
Group By A.Name
Order by TotalCalls
答案 1 :(得分:0)
想出来!非常感谢哈尔和其他评论过的人。
Select A.Name, B.CallCount
From Employees A Inner Join (Select EmployeeID, Count(*) As CallCount From Calls Where Month(EntryDate) = Month(GetDate()) and Year(EntryDate) = Year(GetDate()) Group By EmployeeID) B
On A.TechID = B.EmployeeID
Where A.Name IN ('Steven', 'John', 'Mark', 'Jason', 'Tom')
Group By A.Name, B.CallCount