SELECT COUNT每个日期和类型SQL Server 2012

时间:2017-04-03 06:50:36

标签: sql sql-server sql-server-2012

我在下面有一个SQL查询。

SELECT sched_date, sched_name, employee_name from tbl_schedule

返回此结果:

    sched_date    sched_name              employee_name

    2017-04-01    09:00 AM - 06:00 PM     M.G.Cave
    2017-04-01    09:00 AM - 06:00 PM     L.C.Manalo
    2017-04-01    11:00 AM - 8:00 PM      R.M.Jakosalem
    2017-04-01    11:00 AM - 8:00 PM      S.D.Dela Cruz
    2017-04-02    Rest Day                M.G.Cave
    2017-04-02    Rest Day                L.C.Manalo
    2017-04-02    Rest Day                R.M.Jakosalem
    2017-04-02    Rest Day                S.D.Dela Cruz

我希望员工按日期和时间表计算。我想要的结果是:

    2017-04-01    09:00 AM - 06:00 PM     2 employees
    2017-04-01    11:00 AM - 8:00 PM      2 employees
    2017-04-02    Rest Day                4 employees

提前谢谢。

2 个答案:

答案 0 :(得分:2)

GROUP BYsched_datesched_name上的COUNT(*)会为您提供所需的结果:

SELECT 
   sched_date, 
   sched_name, 
   COUNT(*) AS employees 
from 
   tbl_schedule
GROUP BY 
   sched_date, 
   sched_name

答案 1 :(得分:1)

尝试以下查询:

 SELECT *
 FROM 
 (
    SELECT sched_date, sched_name, COUNT(*) AS employees 
    FROM tbl_schedule
    GROUP BY sched_date,sched_name
 ) A