SQL Server Association Update issues

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.