gpt4 book ai didi

mysql - 如何按年度计算员工总数和员工总数

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:02 27 4
gpt4 key购买 nike

这是我的员工表

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`)

最佳答案

您需要一个使用用户定义变量的运行总计。

您需要一个派生表,因为运行总计不适用于 group by 语句

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

关于mysql - 如何按年度计算员工总数和员工总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34063127/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com