这将使用任务的所有百分比来计算里程碑的百分比。
UPDATE [milestones]
SET [percentage] = (SELECT AVG(pecentage) FROM [tasks] WHERE [tasks].[id_milestone] = [milestones].[id])
如果你想设置一个像 [completed] 这样的状态标志,你可以这样做:
UPDATE [milestones]
SET [completed] = (CASE WHEN (SELECT AVG(pecentage) FROM [tasks] WHERE [tasks].[id_milestone] = [milestones].[id]) = 100 THEN 1 ELSE 0 END)
这只是您想要的猜测,因为您没有完全向我们展示您的表格、结构等。希望它有所帮助。
编辑
您的表Milestone
需要一列percentage
,还是要将结果写入另一个表 Milestone_complete?我不明白你为什么有这些额外的表格。所以我们猜你percentage
的Milestone
表中有一个列。在这种情况下,SQL 是这样的:
UPDATE [Milestone]
SET [percentage] = (SELECT AVG(pecentage)
FROM [Tasks]
WHERE [Tasks].[ID_milestone] = [Milestone].[ID_milestone])
如果您想将结果写入Completed' column in the
Milestone_complete` 表中,请执行以下操作:
UPDATE [Milestone_complete]
SET [percentage] = (SELECT AVG(pecentage)
FROM [Tasks]
WHERE [Tasks].[ID_milestone] = [Milestone].[ID_milestone])
FROM [Milestone_complete]
JOIN [Milestone] ON [Milestone].[ID_milestone] = [Milestone_Complete].[ID_milestone]
或者您是否只想Milestone_Complete
在里程碑的所有任务都是 100% 时插入结果?我不知道您是否已经在强制表中有记录或需要添加以防万一。我假设您有一个并希望将完成设置为例如 1(0 = 未完成)。
UPDATE [Milestone_complete]
SET [completed] = 1
FROM [Milestone_complete]
JOIN [Milestone] ON [Milestone].[ID_milestone] = [Milestone_Complete].[ID_milestone]
WHERE (SELECT AVG(percentage)
FROM [Tasks]
WHERE [Tasks].[ID_milestone] = [Milestone].[ID_milestone])=100
我没有对它进行任何测试,所以不确定它是否有效,但由于我不是 100% 你的方法,你需要根据自己的需要进行修改。希望能帮助到你。下次你问一个问题时,专注于一个清晰、完整和易于理解的问题,让它变得更容易。