[英]how to get no of employees dept wise who joined after 1st jan 1983
假設我有這張桌子:
+-----+------+------------+
| 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 |
+-----+------+------------+
現在,我想獲取1983年1月1日以后加入的每個部門的雇員人數記錄。
我試過這個查詢:
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 |
+-------+------+
但是我需要所有部門在一張桌子里。
select joining, dept, count(*)
from t1
group by dept
having(joining) > '1982-01-01'
order by joining;
沒有這樣的結果。
您需要一個簡單的group by子句,並在其中過濾所需的日期,例如:
select dept,count(*)
from t1
where joining > '1982-01-01'
group by dept
having
子句是應用於由group by
子句創建的group by
。 您需要使用一個簡單的where
子句,該子句將條件分別應用於每一行:
SELECT dept, COUNT(*)
FROM t1
WHERE joining > '1982- 01-01'
GROUP BY dept
您是否嘗試過僅將日期移動到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.