Update dpending on combination of two columns

Hi
I have a table UserCompany:
CompanID
UserID
DefaultCompany

A user can have serveral rows in the table, connected to several Companies.
I want to update the row with the min(CompanyID) and set defauly = true:

I can select the rows:

Select min(CompanyID) as comp,UserID
from UserCompany uc
where DefaultCompany <> 1
group by UserID

But can seem to figure out how to update the same rows?




stalebAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
here we go:
UPDATE uc
   SET DefaultCompany = 1
FROM UserCompany uc
 WHERE uc.CompanyID = ( SELECT MIN(i.CompanyID)
                 FROM UserCompany i
                 WHERE i.UserID = uc.UserID
                )

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stalebAuthor Commented:
Thanks

I was so close, but still so far away
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.