简体   繁体   English

如何获得1983年1月1日以后加入的部门明智员工的人数

[英]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.

相关问题 第一次点击后如何更改动作 - How to change action after the 1st click Java和Javascript之间的区别在1月1日0001 UTC - Difference between Java and Javascript on 1st Jan 0001 UTC Date.getTime()给出2000年1月1日的时间 - Date.getTime() giving a time of 2000 1st Jan 格式化短信收件箱日期显示为“1970 年 1 月 1 日” - Formatting SMS inbox date showing as "Jan 1st 1970" java jodatime在月底后无法回到第一 - java jodatime cannot get back to 1st after the end of month 如何从第二活动到第一活动获取数据 - How to get data from 2nd Activity to 1st Activity 如何在JSON中只获取大数组的第一个元素? - How to get only the 1st element of an large array in JSON? 仅将 sim 卡插入第一个插槽时如何获取 otp? - how to get otp when sim is inserted in 1st slot only? 如何将周数(1 月 1 日 = 1,12 月 25 日 = 52)数转换为开始日期和结束日期以过滤 jsp 报告 java 7? - How to convert the multiple number of weeks( 1st jan = 1, 25th dec = 52) number into start date & end date to filter jsp report java 7? 问题如何解决——优化后出现有无工作员工的车辆? - How to solve the problem - after the optimization, there is vehicles with employees who have no jobs appear?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM