项目从任务表或billingTimes更新时间时如何获得?

时间:2013-01-11 19:43:47

标签: sql sql-server-2008-r2

我遇到了这个问题我正在尝试获取与项目相关的上一个任务更新任务以及如何通过获取最后一个计费时间来更新 我也有Copley的设计问题

  • 获取项目和上次更新时间表格任务的最佳方式是什么?我应该使用 createdOn 列表格 billingTime
  • 我应该在项目表上添加新列来记录上次更新的任务吗?

enter image description here

我的查询:

SELECT     p.projectID, p.projectName, t.lastUpdate, billingsTimes.userID
FROM         Tasks AS t INNER JOIN
                      Projects_tasks ON t.taskID = Projects_tasks.taskID INNER JOIN
                      Projects AS p ON Projects_tasks.projectID = p.projectID INNER JOIN
                      billingsTimes ON t.taskID = billingsTimes.taskID
GROUP BY p.projectName, p.projectID, t.lastUpdate, billingsTimes.userID

1 个答案:

答案 0 :(得分:0)

我认为您不需要在项目表中添加新列。看起来您已经在2个表之间建立了关系。如果您正在查看给定项目的上次更新时间,请尝试以下操作:

SELECT     p.projectID, p.projectName, MAX(t.lastUpdate) as 'Last Updated Time'
FROM         Tasks AS t INNER JOIN
                      Projects_tasks ON t.taskID = Projects_tasks.taskID INNER JOIN
                      Projects AS p ON Projects_tasks.projectID = p.projectID
GROUP BY p.projectName, p.projectID

- 编辑

要显示UserId,请尝试以下操作:

SELECT     p.projectID, p.projectName, BillingsTimes.UserId, MAX(t.lastUpdate) as 'Last Updated Time'
FROM         Tasks AS t INNER JOIN
                      Projects_tasks ON t.taskID = Projects_tasks.taskID INNER JOIN
                      Projects AS p ON Projects_tasks.projectID = p.projectID INNER JOIN
                      BillingsTimes ON t.taskID = BillingsTimes.taskID
GROUP BY p.projectName, p.projectID, BillingsTimes.UserId