简体   繁体   English

查询每月返回项目小时数

[英]Query to return project hours per month

I'm trying to create SQL Query to show task under one project and how many hours spent on each task month by month ? 我正在尝试创建SQL Query以在一个项目下显示任务以及每月在每个任务上花费多少小时? 在此输入图像描述

Query 询问

SELECT 
  Projects.projectName, Tasks.taskName, 
  billingsTimes.taskID, billingsTimes.actualTotalTime
FROM Projects 
INNER JOIN Projects_tasks ON Projects.projectID = Projects_tasks.projectID 
INNER JOIN Tasks ON Projects_tasks.taskID = Tasks.taskID 
INNER JOIN billingsTimes ON Tasks.taskID = billingsTimes.taskID
WHERE (Projects.projectID = '') 

Sounds like you just need the following: 听起来你只需要以下内容:

select p.projectname,
    t.taskname,
    bt.taskid,
    bt.TotalTime
from projects p
left join projects_tasks pt
    on p.projectid = pt.projectid
left join tasks t
    on pt.taskid = t.taskid
left join
(
    select SUM(bt.actualTotalTime) TotalTime, bt.taskid
    from billingtimes bt
    group by bt.taskid
) bt
    on t.taskid = bt.taskid

If you provide more details about where the monthly dates are stored, then this could be altered to group by month to give you monthly totals per task. 如果您提供有关每月日期存储位置的更多详细信息,则可以将其更改为按月分组,以便为​​每个任务提供每月总计。

edit #1, if you are using the dateOfService to determine when the service was performed and you want to group by month/year, then you can use the following: 编辑#1,如果您使用dateOfService来确定服务的执行时间,并且您希望按月/年分组,那么您可以使用以下内容:

select p.projectname,
    t.taskname,
    bt.taskid,
    bt.TotalTime,
    bt.ServiceMonth,
    bt.ServiceYear
from projects p
left join projects_tasks pt
    on p.projectid = pt.projectid
left join tasks t
    on pt.taskid = t.taskid
left join
(
    select SUM(bt.actualTotalTime) TotalTime, bt.taskid, 
        datepart(month, bt.dateofService) ServiceMonth,
        datepart(year, bt.dateofService) ServiceYear
    from billingtimes bt
    group by bt.taskid, datepart(month, bt.dateofService), 
        datepart(year, bt.dateofService)
) bt
    on t.taskid = bt.taskid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM