寻找2017年最少使用的员工

时间:2018-04-21 18:26:57

标签: mysql sql

我有一个这样的表:Service_Done

Employee_ID  Grooming_ID     Date_Serviced:                                                               
1               3                  1/23/2016                                                  
2               4                  04/4/2017
3               3                 07/24/2017

我的代码如下所示:

 SELECT Employee_ID
FROM Service_Done
WHERE Employee_ID = (SELECT Grooming_ID, Count(Employee_ID) FROM Service_Done 
AND year(Date_Serviced) = 2017;

我运行此代码但不起作用。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

DROP TABLE IF EXISTS T;
CREATE TABLE T (Employee_ID  INT, Grooming_ID INT,    Date_Serviced DATE);
INSERT INTO T VALUES                                                               
(1        ,       3     ,   '2016-01-23'),                                                      
(2        ,       4     ,   '2017-04-04'),     
(3        ,       3     ,   '2017-07-24'),
(3        ,       3     ,   '2017-07-24'),
(3        ,       3     ,   '2017-07-24'),
(4        ,       3     ,   '2017-07-24')
;

SELECT Employee_ID #,COUNT(*) SERVICES
FROM T
WHERE  year(Date_Serviced) = 2017
GROUP BY EMPLOYEE_ID 
ORDER BY COUNT(*)  LIMIT 1;

+-------------+
| Employee_ID |
+-------------+
|           2 |
+-------------+
1 row in set (0.00 sec)

如果抽签,返回的employee_id是不确定的。