Everyone knows UPDATE statement: Updatetable name Set field = Value . It seems very simple.
But today I have a problem: I need to update a score field called Po_special_task_line table, which is calculated from another Score detail table (special_assessment_score_detail),
The association between them is that the fields are TaskID and Tasklineid.
Well, I'll write the following statement right away:
Update Po_special_task_line
Set Score= (
SELECT SUM (Score) from
(SELECT AVG (CONVERT (Decimal,score)) score,task_line_id from Dbo.special_assessment_score_detail WHERE task_id= '
GROUP by task_line_id,special_indicators_id) a
GROUP by task_line_id
)
where Task_id= '
The goal is simple, by calculating the average score of each metric for all rows of the current task by a scoring table and updating it to the corresponding row in the Task row table
The above is a task if there is only one row is not a problem, but if a task has more than one row of data, I need to batch update, obviously this is not possible.
So here's the advanced usage of update (self-described as advanced):
Update Table 1
Set Table 1.score= table 2.score
From table 1,
Table 2
Where table 1.task_line_id= table 2.task_line_id
Table 2 here can be a query view, the old iron is not feel very advanced, so no matter how many score in Table 2, can be updated to table 1
Here is also affixed to my project in the source code:
UPDATE dbo. Po_special_task_line
SET Po_special_task_line. Score=a.score
From Po_special_task_line,
(SELECT CAST (ROUND (SUM (Score), 0) as INT) score,task_line_id
From (
SELECT AVG (CONVERT (Decimal,score)) score,task_line_id from Dbo.special_assessment_score_detail WHERE task_id=#{ TaskId} and Process_status in (' 4 ', ' 5 ', ' 0 ') GROUP by special_indicators_id,task_line_id
) s GROUP by task_line_id) a
WHERE Po_special_task_line. task_line_id=a.task_line_id
and Po_special_task_line. Task_id=#{taskid}
This is the code in the XML in MyBatis, and the parameter is the task ID (taskId)
SQL Server Association Update issues