我有2个表,其中包含以下数据: -
公司
CompanyId CompanyName
1公司1
2 Company2
3 Company3
员工
EmployeeId EmployeeName CompanyId StartDate
1 Employee1 1 12/21/2011
2 Employee2 1 01/20/2012
3 Employee3 2 03/23/2012
4 Employee4 2 07/15/2012
5 Employee5 2 01/20/2013
6员工6 3 12/17/2013
现在我想查一下,在指定的月份和年份,团队中招募了多少人?我有如下存储表: -
RecruiterIndicator
CompanyId Year Month EmployeeRecruited
1 2011 12 1
1 2012 1 1
2 2012 3 1
2 2012 7 1
2 2013 1 1
3 2013 12 1
这应该是一个合并存储过程,如果数据存在于同一个月和公司,则应该更新数据,如果不存在则插入?循环将从特定日期开始,该日期可以是一个参数,它将循环到当前月份。
请帮我解决这个问题
由于
维沙尔
答案 0 :(得分:2)
SELECT YEAR(StartDate) AS [Year], MONTH(StartDate) AS [Month], COUNT(*) EmpTotal
FROM Employees
GROUP BY YEAR(StartDate), MONTH(StartDate)
如果您想按公司查看员工总数,您也可以这样做
SELECT YEAR(StartDate) AS [Year], MONTH(StartDate) AS [Month]
,C.CompanyName , COUNT(E.EmployeeId) EmpTotal
FROM Employees E INNER JOIN Company C
ON E.CompanyId = C.CompanyId
GROUP BY YEAR(StartDate), MONTH(StartDate) ,C.CompanyName