这是我的员工表
empid name Date_of_joining
1 dilip 2010-01-30
2 suresh 2001-03-01
3 ramesh 2003-01-01
我希望按员工加入日期获得员工总数的员工人数
预期输出
year new joining total employees
2001 10 10
2002 12 22
2003 15 27
查询
select YEAR(`DATE_OF_JOINING`) as 'year', COUNT(*) as 'count1',sum(count(*)) from employee
GROUP BY YEAR(`DATE_OF_JOINING`)
答案 0 :(得分:3)
您需要使用用户定义的变量运行总计。
您需要一个派生表,因为运行总计不能使用分组语句
SET @SUM = 0;
SELECT
YEAR,
NoOfEmployee AS newJoining,
(@SUM := @SUM + NoOfEmployee) AS totalJoining
FROM (
SELECT
YEAR(Date_of_joining) AS YEAR,
COUNT(*) AS NoOfEmployee
FROM
employees
GROUP BY
YEAR(Date_of_joining)
) O
此处为sample