显示该月以及该月雇用了多少人

时间:2018-10-07 02:21:40

标签: sql sql-server

我试图列出每个月雇用了多少人。我不确定哪里出了问题,但我得到了23行并且得到了重复的月份。

SELECT DISTINCT COUNT(E.BusinessEntityID) AS NumberOfEmployees,
CASE
    WHEN DATEPART(MONTH,E.HireDate) = 1 THEN 'Janurary'
    WHEN DATEPART(MONTH,E.HireDate) = 2 THEN 'Feburary'
    WHEN DATEPART(MONTH,E.HireDate) = 3 THEN 'March'
    WHEN DATEPART(MONTH,E.HireDate) = 4 THEN 'April'
    WHEN DATEPART(MONTH,E.HireDate) = 5 THEN 'May'
    WHEN DATEPART(MONTH,E.HireDate) = 6 THEN 'June'
    WHEN DATEPART(MONTH,E.HireDate) = 7 THEN 'July'
    WHEN DATEPART(MONTH,E.HireDate) = 8 THEN 'August'
    WHEN DATEPART(MONTH,E.HireDate) = 9 THEN 'September'
    WHEN DATEPART(MONTH,E.HireDate) = 10 THEN 'October'
    WHEN DATEPART(MONTH,E.HireDate) = 11 THEN 'November'
    WHEN DATEPART(MONTH,E.HireDate) = 12 THEN 'December'
    ELSE 'Unknown'
END AS Month
FROM HumanResources.Employee AS E
GROUP BY
E.HireDate;

3 个答案:

答案 0 :(得分:0)

DATENAME函数用于月份名称,将group by用于月份计数

    SELECT COUNT(E.BusinessEntityID) AS NumberOfEmployees,
    DATENAME(mm,E.HireDate) from t
    group by DATENAME(mm,E.HireDate)

但是我不确定您是否需要year,因为如果您需要年份,那么它也将用于选择和分组

答案 1 :(得分:0)

您是否尝试过将_char(e.HireDate,月份)作为hire_month

答案 2 :(得分:0)

从雇员e组中按to_char(e.HireDate,'Month')选择count(e.businessentityid)empcount,to_char(e.HireDate,'Month')月名   按to_date(monthname,'Month')排序