Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update Query

Status
Not open for further replies.

sila

Technical User
Aug 8, 2003
76
GB
Hi
I need to add some code to the query below which also sets any records other than the winner who has the same DivElecID to 0. Any help greatly appreciated!

Code:
update dbo.Candidate
set Winner = 1
where DivElecId = @DivElecID and
VotesReceived =
(Select max(a.VotesReceived) from dbo.Candidate a
where a.DivElecID = @DivElecID)

Cheers
Sila
 
update dbo.Candidate
set Winner = 0
where DivElecId = @DivElecID and
VotesReceived <>
(Select max(a.VotesReceived) from dbo.Candidate a
where a.DivElecID = @DivElecID)
 



Assume max(a.VotesReceived) never be 0, try this:

update dbo.Candidate
set winner = a.VotesReceived/(Select max(a.VotesReceived) from dbo.Candidate a where a.DivElecID = @DivElecID)
where DivElecId = @DivElecID

Or max(a.VotesReceived) can be 0, everybody are winners!

update dbo.Candidate
set winner =
( case when
VotesReceived = (Select max(a.VotesReceived) from dbo.Candidate a where a.DivElecID = @DivElecID)
then 1
else 0
end )
where DivElecId = @DivElecID

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top