Question : Update the first record from a returned set of records

Hi all,

I'm trying to set a flag on any one of a series of records which are returned.  The flag is simply to set whether an individual is a key contact (value 0 or 1 - 1 being key contact).  Each company I have can have only one key contact & there are many without even that one: which is what I am trying to rectify.

So, I can extract the list of companies which lack a key contact from the company table and can list all contacts from that company in the same select... What I cannot seem to do is just update the first contact from that group of contacts. :(

To figure out all the companies without a keycontact I can grab them from:

select distinct companyno from company
where companyno not in
(select distinct companyno from contact
where keycontact = 1)

The distinct in the embedded select is redundant, but I'd rather keep it in there. :)

Obviously the company & the contact tables each have a companyno field which are linked.  Any aid gladly recieved.

Answer : Update the first record from a returned set of records

This will update the minimum contactno for each companyno presuming that the contactno is unique in the contact table.

update contact
set keycontact = 1
where contactno in (select min(contactno) from contact where companyno not in (select distinct companyno from contact where keycontact = 1) group by companyno)

Random Solutions  
 
programming4us programming4us