简体   繁体   中英

T-SQL : find the employee count as average by the number of months in the year

I have table called Employee with these columns:

Id (identity)
EmploymentStartDate (datetime),
EmploymentEndDate (nullable datetime),

My query:

DECLARE @FromYear int = 2010, @ToYear int = 2017;

WITH YEARS AS 
(
    SELECT @FromYear As TheYear
    UNION ALL
    SELECT TheYear + 1
    FROM YEARS
    WHERE TheYear < @ToYear
)
SELECT
    Y.TheYear,
    SUM(CASE 
           WHEN YEAR(EmploymentStartDate) <= Y.TheYear
                AND (EmploymentEndDate IS NULL OR YEAR(EmploymentEndDate) >= Y.TheYear)
              THEN 1
              ELSE 0
        END) WorkingEmployeeCount,
    SUM(CASE 
           WHEN YEAR(EmploymentStartDate) = Y.TheYear
              THEN 1
              ELSE 0
        END) StartedEmployeeCount,
    SUM(CASE 
           WHEN YEAR(EmploymentEndDate) = Y.TheYear
              THEN 1
              ELSE 0
        END) SeparatedEmployeeCount
FROM
    YEARS Y
CROSS JOIN
    Employees E
GROUP BY
    Y.TheYear

When I run this query, I get these results:

TheYear - WorkingEmployeeCount - StartedEmployeeCount - SeparatedEmployeeCount
---------------------------------------------------------------    
2010    -  1                   -  1                   -   0
2011    -  2                   -  1                   -   0
2012    -  2                   -  0                   -   0
2013    -  2                   -  0                   -   0
2014    -  2                   -  0                   -   0
2015    -  4                   -  2                   -   1
2016    -  3                   -  0                   -   0
2017    -  6                   -  3                   -   2

Question:

I need to use below formula. First I want to find every year's months count then AVG of EmployeeCount per year.

EmployeeCount / MonthsCountPerYear then get AVG

If I try below query it is not working for me (I can not create a solution)

(AVG(EmployeeCount / (CASE WHEN TheYear = DATE(GETUTCDATE) THAN 2 ELSE 12 END))) AS AvgEmployeeCount

What I want should be as below

TheYear - WorkingEmployeeCount - StartedEmployeeCount -SeperatedEmployeeCount - AvgEmployeeCount

2010    -  1                   -  1                   -   0                      - 1,30
2011    -  2                   -  1                   -   0                      - 1,20
2012    -  2                   -  0                   -   0                      - 1,00
2013    -  2                   -  0                   -   0                      - 3,50
2014    -  2                   -  0                   -   0                      - 5,33
2015    -  4                   -  2                   -   1                      - 7-33
2016    -  3                   -  0                   -   0                      - 9-34
2017    -  6                   -  3                   -   2                      - 1,15

How can I find employee count avg for every year according to months in a year? Any help will be appreciated. Thank you

Solution:

  DECLARE @MONTH INT = -100

                    DECLARE @ENDDATE DATE = CAST(GETUTCDATE() AS DATE)
                    DECLARE @STARTDATE DATETIME = DATEADD(M, @MONTH, @ENDDATE);

                    WITH CALENDAR AS
                    (
                        SELECT @STARTDATE F, YEAR(@STARTDATE) Y, MONTH(@STARTDATE) M
                        UNION ALL
                        SELECT DATEADD(MONTH, 1, F), YEAR(DATEADD(MONTH, 1, F)), MONTH(DATEADD(MONTH, 1, F))
                        FROM CALENDAR
                        WHERE DATEADD(MONTH, 1, F) <= @ENDDATE   
                    )

                    SELECT

                        X.Y AS 'Year',
                        COUN

T(X.Y) AS 'MonthCountInYear',
                        CAST(AVG(X.EmployeeCount) AS DECIMAL(18,2)) AS AvgEmployeeCount,
                        SUM(X.StartedEmployeeCount) AS StartedEmployeeCount,
                        SUM(X.EndedEmployeeCount) AS EndedEmployeeCount,
                        CASE WHEN AVG(X.EmployeeCount) != 0 THEN SUM(X.EndedEmployeeCount) / AVG(X.EmployeeCount) ELSE 0.0 END AS ConversionRate

                    FROM
                    (

                        SELECT
                            C.Y,
                            C.M,
                            SUM
                            (
                                CASE WHEN
                                    (YEAR(EmploymentStartDate) < C.Y OR (YEAR(EmploymentStartDate) = C.Y AND MONTH(EmploymentStartDate) <= C.M))
                                    AND (EmploymentEndDate IS NULL OR (YEAR(EmploymentEndDate) > C.Y OR (YEAR(EmploymentEndDate) = C.Y AND MONTH(EmploymentEndDate) >= C.M)))
                                THEN 1.0
                                ELSE 0.0
                                END
                            ) EmployeeCount,
                            SUM
                            (
                                CASE WHEN
                                    YEAR(EmploymentStartDate) = C.Y AND MONTH(EmploymentStartDate) = 

C.M
                            THEN 1
                            ELSE 0
                            END
                        ) StartedEmployeeCount,
                        SUM
                        (
                            CASE WHEN
                                YEAR(EmploymentEndDate) = C.Y AND MONTH(EmploymentEndDate) = C.M
                            THEN 1
                            ELSE 0
                            END
                        ) EndedEmployeeCount

                    FROM
                        CALENDAR C
                        CROSS JOIN
                        Employees E

                    WHERE
                        IsDeleted = 0 AND EmploymentStartDate IS NOT NULL

                    GROUP BY
                        C.Y, C.M
                ) X
                GROUP BY
                    X.Y

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