0

I am trying to assign 'A' to [Student Details].group based on this SELECT statement.

SELECT     TOP (10) PERCENT [Person Id], [Given Names], Surname, Gpa, [Location Cd]
FROM         [Student Details]
WHERE     ([Location Cd] = 'PAR')
ORDER BY Gpa DESC

I can't figure out how to use a SELECT statement in an UPDATE statement. Can someone please explain how to accomplish this?

I am using ASP .NET and MsSQL Server if it makes a difference.

Thanks

3
  • Possible duplicate stackoverflow.com/questions/2334712/… Commented Oct 17, 2013 at 7:46
  • Do you need to update the table or just a select with 'A' as group? Commented Oct 17, 2013 at 7:46
  • Currently Group is null.. I need to update group to contain 'A' for all of the rows that are returned from the above query Commented Oct 17, 2013 at 7:49

3 Answers 3

1

I'm assuming you want to update these records and then return them :

SELECT TOP (10) PERCENT [Person Id], [Given Names], Surname, Gpa, [Location Cd]
INTO #temp
FROM [Student Details]
WHERE     ([Location Cd] = 'PAR')
ORDER BY Gpa DESC

update [Student Details] set group='A' where [person id] in(select [person id] from #temp)

select * from #temp

I'm also assuming person id is the PK of student details

Sign up to request clarification or add additional context in comments.

Comments

0

Try this using CTE (Common Table Expression):

;WITH CTE AS
(
    SELECT TOP 10 PERCENT [Group]
    FROM      [Student Details]
    WHERE     ([Location Cd] = 'PAR')
    ORDER BY Gpa DESC
)
UPDATE CTE SET [Group] = 'A'

4 Comments

thanks, but I'm getting incorrect syntax near GROUP and FROM :(
There is no syntax error as far as I can see. Check this similar example with no such syntax errors. May be you are missing angle brackets '[]' around group which is a key word.
ah you're a champion, the problem was that there was 2 of the word BY :) Apart from that, it worked :D Thank you.
Early morning hear and the engine is still warming up ! :)
0

Is this you want?

Update top (10) Percent [Student Details] set [group] = 'A' 
where [Location Cd] = 'PAR' AND [group] is null

2 Comments

No sorry, it must sort the rows based on GPA and only assign group='A' to the top 10% Thanks for the reply though.
Sorry still wouldn't work.. It needs to sort the table in descending order from the column 'gpa'

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.