简体   繁体   中英

T-SQL - Employee Annual Average

I'm trying to make an annual average of employees, in a year.

The table 'a' is thus constructed:

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.

Can you help me? (the correct calculation is to have 12 records indicating the monthly number and at the end divided by 12)

I can't do everything with a T-SQL command.

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?

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:

https://rextester.com/IQAB32422

Unfortunately, your dates were not all correct, considering you used the American syntax 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. But even that is only an "arbitrary" date and will not count employees who were there from 1st to the 14th of a month.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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