我一直坚持这个问题,我必须选择过去5年在公司工作的员工。
e-g
in 2010 1 employee Registered so Total Employees in 2010 1
in 2011 2 employees Registered so Total Emloyees in 2013 3
in 2012 4 employees Registered and 1 left so Total Employees in 2012 6
in 2013 5 employees Registered and 2 left So Total Employees in 2013 9
in 2014 No New employee Registered or Left So Same as Same
etc
所以最终员工报告就像
(2010), (2011), (2012), (2013), (2014)
1 3 6 9 9
现在我被困了,我怎么能在mysql查询中实现这一点。
我有表员工,其中注册了员工信息。
拥有员工表,如果员工已注册,则会分配加入日期,如果已分配,则分配结束日期。
此外还有性别表格,以显示一年中有多少男性和多少女性在公司工作。
最后这是我试过的查询..但这不是正确的查询。我在查询中遗漏了一些东西,但我不能理解。
SELECT MLGT.`gender_type_title`,MLGT.`gender_type_id` AS GenderID,COUNT(E.`gender`) AS TotalGenders,ET.`joining_date`
FROM employee E
INNER JOIN employment ET
ON E.`employee_id` = ET.`employee_id` AND ET.`current` = 1 AND ET.`trashed`=0
INNER JOIN ml_gender_type MLGT
ON E.`gender` = MLGT.`gender_type_id` AND MLGT.`trashed`=0
WHERE E.`trashed`=0 AND ET.`joining_date` >= DATE_SUB(NOW(),INTERVAL 5 YEAR) AND ET.`current`= 1
GROUP BY E.`gender`, YEAR(ET.`joining_date`);
答案 0 :(得分:0)
您需要另一个表(或常量子查询)来包含要分组的年份列表。在这个新表和就业表之间建立联接,并加入日期比较条件。然后计算结果:)
答案 1 :(得分:0)
尝试此查询
SELECT YEAR(ET.`joining_date`) as Year, MLGT.`gender_type_title` as Gender, count(E.employee_id) as Emp_count
FROM employee E
INNER JOIN employment ET
ON E.`employee_id` = ET.`employee_id` AND ET.`current` = 1 AND ET.`trashed`=0
INNER JOIN ml_gender_type MLGT
WHERE E.`trashed`=0 AND ET.`joining_date` >= DATE_SUB(NOW(),INTERVAL 5 YEAR) AND ET.`current`= 1
GROUP BY Year, Gender;