简体   繁体   English

T-SQL - 员工年平均值

[英]T-SQL - Employee Annual Average

I'm trying to make an annual average of employees, in a year. 我试图在一年内制作员工的年平均值。

The table 'a' is thus constructed: 因此构造表'a':

CREATE TABLE [dbo]. [A] 
(
    [id] [INT] NOT NULL,
    [start] [DATETIME] NOT NULL,
    [end] [DATETIME] NOT NULL,
    [employee_code] [INT] NOT NULL
)

1 - 01/01/2016 - 03/31/2019 - 56
2 - 01/01/1995 - 06/06/2017 - 13
-
-

If I simply count the [employee_code] between 01/01/2017 and 12/31/2017 the calculation is incorrect as it does not make a monthly average. 如果我只是在01/01/2017到12/31/2017之间计算[employee_code],那么计算是不正确的,因为它没有按月平均值计算。

Can you help me? 你能帮助我吗? (the correct calculation is to have 12 records indicating the monthly number and at the end divided by 12) (正确的计算是有12条记录表示月数,最后除以12)

I can't do everything with a T-SQL command. 我不能用T-SQL命令做任何事情。

Can you help me? 你能帮助我吗?


hello thanks, yes, that's what I was. 你好,谢谢,是的,那就是我。 Only one thing, in the query result there are the following records: 只有一件事,在查询结果中有以下记录:

1995 1 0 NULL
1995 2 0 NULL
1995 3 0 NULL
1995 4 0 NULL
1995 5 6 NULL
1995 6 6 NULL
1995 7 10 NULL
1995 8 10 NULL
1995 9 10 NULL
1995 10 12 NULL
1995 11 12 NULL
1995 12 12 NULL
NULL 78 1995 6.500000

The initial data from which I started are: 我开始的初始数据是:

03/04/1995 10/20/2005 4
03/04/1995 19/06/2016 2
03/04/1995 12/15/2016 2847
03/04/1995            1
03/04/1995            5
03/04/1995            3
02/06/1995 03/07/2009 9
02/06/1995            8
02/06/1995            7
02/06/1995            6
09/15/1995 16/05/2017 34
09/15/1995            33

why are the 6 "ecodes" in May and not April? 为什么5月份的6“ecodes”而不是4月?

Edited : 编辑

WITH mnths as (
  select CAST('01/15/1995' as date) as mnth UNION ALL
  select DATEADD(month,1,mnth) FROM mnths WHERE mnth<'12/15/2019'
), edata AS (
  SELECT mnth,
  (SELECT count(ecode) FROM @A A 
   WHERE mnth between estart AND COALESCE(eend,getdate())) ecnt
  FROM mnths
)
SELECT year(mnth) yr, month(mnth) mn, AVG(ecnt+0.) emplcount
FROM edata 
group by year(mnth), month(mnth) 
WITH ROLLUP
OPTION (MAXRECURSION 1000)

This version takes into account your latest edits and uses ROLLUP to shorten the query code, see demo here: 此版本考虑了您的最新编辑并使用ROLLUP缩短查询代码,请参阅此处的演示:

https://rextester.com/IQAB32422 https://rextester.com/IQAB32422

Unfortunately, your dates were not all correct, considering you used the American syntax month/day/year . 不幸的是,考虑到您使用美国语法month/day/year ,您的日期并不正确。 For this reason I got slightly different numbers ... 出于这个原因,我的数字略有不同......

The comparison, whether an employee is employed in a particular month was originally done for the first day of each month. 比较,无论员工是否在特定月份工作,最初都是在每个月的第一天进行。 I changed it now to the 15th of each month. 我现在改为每个月的15号。 But even that is only an "arbitrary" date and will not count employees who were there from 1st to the 14th of a month. 但即使这只是一个“任意”的日期,也不会计算从1月1日到14日在那里工作的员工。

According to your comment and previous answers, you are looking for something like this? 根据您的评论和以前的答案,您正在寻找这样的东西?

DECLARE @A table (
eid int NOT NULL,
estart datetime,
eend datetime,
ecode int)
insert into @A VALUES (1,'02/01/2016','03/31/2019',56),
(2,'05/01/1995','01/06/2017',13);

WITH mnths as (
  select CAST('01/01/1995' as date) as mnth UNION ALL
  select DATEADD(month,1,mnth) FROM mnths WHERE mnth<'12/01/2019'
), edata AS (
  SELECT mnth,
  (SELECT count(ecode) FROM @A A WHERE mnth between estart AND eend) ecnt
  FROM mnths
)
select * FROM(
    SELECT year(mnth) yr, month(mnth) mn, ecnt, null eavg 
    FROM edata
    union all
    SELECT year(mnth) yr, null mn, SUM(ecnt) as ecnt, SUM(ecnt) / 12.00 eavg 
    FROM edata
    GROUP BY year(mnth)) A
order by 
    A.yr, 
    CASE WHEN a.mn IS NULL THEN 13 else a.mn END
OPTION (MAXRECURSION 1000)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM