T-SQL:根据一年中的月数查找员工人数

时间:2017-02-14 09:00:32

标签: sql-server

我有一个名为Employee的表,其中包含以下列:

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

我的查询:

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

当我运行此查询时,我得到以下结果:

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

问题:

我需要使用以下公式。首先,我希望每年都能找到每年的AVG of EmployeeCount。

EmployeeCount / MonthsCountPerYear then get AVG

如果我尝试以下查询,它对我不起作用(我无法创建解决方案)

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

我想要的应该如下

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

如何根据一年中的月份找到每年的员工人数?任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

<强>解决方案:

  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