0

I am using MS SQL. I want to update a specific row within a table with data from another table. I have created a query that will get the specific row I want to update. Please note that I have used a select query to select the specific row that needs to be updated. Also note that there is a sub query used to get the right row. For me, this makes it difficult to incorporate into a set statement.

select tbl1.assessmentcode,  tbl1.Overview from subjectassessmentareas tbl1
inner join  
(   
select assessmentcode,MIN(areaseq) as minassessarea from subjectassessmentareas
where resultgroup = 'PR_Yr8_2' and ResultType = 'KUS_5' 
group by AssessmentCode
) tbl2
on tbl1.AssessmentCode = tbl2.AssessmentCode and tbl1.AreaSeq = tbl2.minassessarea
where fileyear = 2016 and filesemester = 3

This gives me

enter image description here

Now I want to update the overview column with data from another table. This select query gives me the info I want to use to update the other table.

SELECT AssessmentCode, Overview
FROM SubjectAssessments
WHERE (ClassCampus = 'S') 
    and (FileYear = 2015)   
    and (FileSemester = 3) 
    and filetype = 'A' 
    and AssessmentCode like '08%'

This gives me

enter image description here

Can someone please help me with the syntax to update the overview column from the row obtained in the first query above with the overview column contained in the second query where the Query1.AssessmentCode = Query2.AssessmentCode from both queries.

How can I use a set statement but then use the first query above to say which row to set? Other similar questions just use a simple set and then a field without any where statements.

3

2 Answers 2

1

Just join these two in an updateable CTE:

;with x as (
    select tbl1.assessmentcode,  tbl1.Overview 
    from subjectassessmentareas tbl1
    inner join  
    (   
    select assessmentcode,MIN(areaseq) as minassessarea from subjectassessmentareas
    where resultgroup = 'PR_Yr8_2' and ResultType = 'KUS_5' 
    group by AssessmentCode
    ) tbl2
    on tbl1.AssessmentCode = tbl2.AssessmentCode and tbl1.AreaSeq = tbl2.minassessarea
    where fileyear = 2016 and filesemester = 3
),
y as (
    SELECT AssessmentCode, Overview
    FROM SubjectAssessments
    WHERE (ClassCampus = 'S') 
        and (FileYear = 2015)   
        and (FileSemester = 3) 
        and filetype = 'A' 
        and AssessmentCode like '08%'
),
z as (
    select x.Overview as dest, y.Overview as src
    from x join y on x.AssessmentCode = y.AssessmentCode
)
update z set dest = src
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much. This works well. Have not used CTEs much and this is a nice simple solution.
1

Try following:

;WITH cteBaseInfo AS
(
    SELECT AssessmentCode, Overview
    FROM SubjectAssessments
    WHERE (ClassCampus = 'S') 
        and (FileYear = 2015)   
        and (FileSemester = 3) 
        and filetype = 'A' 
        and AssessmentCode like '08%'
)

, cteToBeUpdated AS
(
    select tbl1.assessmentcode,  tbl1.Overview from subjectassessmentareas tbl1
    inner join  
    (   
        select assessmentcode,MIN(areaseq) as minassessarea from subjectassessmentareas
        where resultgroup = 'PR_Yr8_2' and ResultType = 'KUS_5' 
        group by AssessmentCode
    ) tbl2
    on tbl1.AssessmentCode = tbl2.AssessmentCode and tbl1.AreaSeq = tbl2.minassessarea
    where fileyear = 2016 and filesemester = 3.
)

UPDATE  subjectassessmentareas
SET     Overview = (SELECT Overview FROM cteBaseInfo WHERE AssessmentCode = subjectassessmentareas.AssessmentCode)
WHERE   AssessmentCode IN (SELECT AssessmentCode FROM cteToBeUpdated)

Please note that, here AssessmentCode should be Primary Key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.