[英]how to get no of employees dept wise who joined after 1st jan 1983
Suppose I have this table: 假设我有这张桌子:
+-----+------+------------+
| eid | dept | joining |
+-----+------+------------+
| 1 | IT | 1982-01-13 |
| 2 | IT | 1983-01-13 |
| 3 | CSE | 1984-04-13 |
| 4 | CSE | 1983-03-23 |
| 5 | IT | 1985-03-23 |
| 6 | ECE | 1986-03-23 |
| 7 | ECE | 1986-11-23 |
+-----+------+------------+
Now I want to get the records of number of employees in each department who joined after 1st January, 1983. 现在,我想获取1983年1月1日以后加入的每个部门的雇员人数记录。
I tried this query: 我试过这个查询:
select count(a.eid) as total, dept
from (
select *
from t1
where dept = 'IT'
having DATE(joining) > '1982-01-01'
) as a;
+-------+------+
| total | dept |
+-------+------+
| 3 | IT |
+-------+------+
But I need all departments in one table. 但是我需要所有部门在一张桌子里。
select joining, dept, count(*)
from t1
group by dept
having(joining) > '1982-01-01'
order by joining;
No such result. 没有这样的结果。
You need a simple group by clause with a where that filters the dates you want, like this: 您需要一个简单的group by子句,并在其中过滤所需的日期,例如:
select dept,count(*)
from t1
where joining > '1982-01-01'
group by dept
The having
clause is a condition applied on the groups created by the group by
clause. having
子句是应用于由group by
子句创建的group by
。 You need to use a simple where
clause which applies the condition to each row individually: 您需要使用一个简单的where
子句,该子句将条件分别应用于每一行:
SELECT dept, COUNT(*)
FROM t1
WHERE joining > '1982- 01-01'
GROUP BY dept
Have you tried simply just moving your date into a WHERE clause, instead of having it in the GROUP BY, like this: 您是否尝试过仅将日期移动到WHERE子句中,而不是将其放入GROUP BY中,例如:
SELECT joining, dept, count(*)
FROM t1
WHERE joining > '1982-01-01'
GROUP BY dept
ORDER BY joining ACS;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.