SUM 查询,日期之间,按员工分组

SUM query, between dates, grouped by employee

我有4个table,结构如下:

更详细地说,一个项目由一组任务组成。 员工可以向数据库添加一个或多个笔记,指定花费的时间 任务在任何给定的日期

我想为每个员工查找他在两个日期之间完成给定任务的总时长。我对此的第一次尝试如下:

SELECT  Project.Name AS ProjectName,
        Task.Name AS TaskName,
        Employee.Name AS EmployeeName,
        SUM(Note.WorkDuration)
        FROM 
        Note 
        INNER JOIN
        Employee 
        ON
        Note.ID_Employee = Employee.ID
        INNER JOIN
        Task
        ON
        Task.ID = Note.ID_Task
        INNER JOIN
        Project
        ON
        Task.ID_Project = Project.ID
        WHERE 
        Note.WorkDate BETWEEN '01/05/2015' AND '08/05/2015'
        GROUP BY
        Task.Name, Project.Name, Employee.Name
        ORDER BY
        Employee.Name
        ASC

然而,这给了填充数据库错误的值。谁能帮助我了解如何执行此操作?

这个想法是以某种方式将此查询写入 Excel table,Y 为 projects/tasks,X 为员工姓名,每个单元格包含工作的总时间任务。

更新:如果我将 BETWEEN 子句更改为:

Note.[Date] BETWEEN '20150501' AND '20150508'

它似乎工作正常。所以我遇到了日期格式问题。

"So I started doing this for you and saw when I was selecting records that the WHERE - BETWEEN clause isn't working correctly. I set the dates as above and I'm getting records from March in there too. Have I done something wrong with the date format such that it's being converted somehow? – Robinson 18 mins ago"

这取决于您来自哪里以及日期格式的默认设置;我猜你不是来自美国(因为你似乎把这个月的前一天放在'01/05/2015'),但我敢打赌你的服务器格式设置为美国标准(month/day/year) .参见 https://msdn.microsoft.com/en-us/library/ms189491.aspx

最安全的做法是像您在第二次尝试中那样使用通用标准:yyyymmdd(或者更好:yyyy-mm-dd)。