2

I was trying to update table columns from another table.

Person Table

In person table, there can be multiple contact persons with same inst_id.

I have a firm table, which will have latest 2 contact details from person table.

I am expecting the firm tables as below:

Firm Table

If there is only one contact person, update person1 and email1. If there are 2, update both. If there is 3, discard the 3rd one.

Can someone help me on this?

5
  • What do you mean by : latest 2 contact details ? Commented Nov 24, 2014 at 10:03
  • top 2 will be fine. (with highest ids). Commented Nov 24, 2014 at 10:05
  • Are you using MySQL or SQL Server (as the tsql tag would indicate)? Commented Nov 24, 2014 at 10:20
  • Please update your question so that we can understand what you need to do. Please, show what you have tried, even if it's very basic, and what you wanted to achieve. Commented Nov 24, 2014 at 10:20
  • I am using SQL server Commented Nov 24, 2014 at 10:31

2 Answers 2

2

This should work:

;with cte (rn, id, inst_id, person_name, email) as (
    select row_number() over (partition by inst_id order by id) rn, * 
    from person
    )

update f
set 
  person1 = cte1.person_name, 
  email1  = cte1.email, 
  person2 = cte2.person_name, 
  email2  = cte2.email
from firm f
left join cte cte1 on f.inst_id = cte1.inst_id and cte1.rn = 1
left join cte cte2 on f.inst_id = cte2.inst_id and cte2.rn = 2

The common table expression (cte) used as a source for the update numbers rows in the person table, partitioned by inst_id, and then the update joins the cte twice (for top 1 and top 2).

Sample SQL Fiddle

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

Comments

0

I think you don't have to bother yourself with this update, if you rethink your database structure. One great advantage of relational databases is, that you don't need to store the same data several times in several tables, but have one single table for one kind of data (like the person's table in your case) and then reference it (by relationships or foreign keys for example).

So what does this mean for your example? I suggest, to create a institution's table where you insert two attributes like contactperson1 and contactperson2: but dont't insert all the contact details (like email and name), just the primary key of the person and make it a foreign key.

So you got a table 'Person', that should look something like this:

ID   INSTITUTION_ID   NAME      EMAIL
1        100          abc    [email protected]
2        101          efg    [email protected]
3        101          ijk    [email protected]
4        101          rtw    [email protected]
... 

And a table "Institution" like:

ID   CONTACTPERSON1  CONTACTPERSON2
100        1             NULL
101        2              3
...

If you now want to change the email adress, just update the person's table. You don't need to update the firm's table.

And how do you get your desired "table" with the two contact persons' details? Just make a query:

SELECT i.id, p1.name, p1.email, p2.name, p2.email
FROM institution i LEFT OUTER JOIN person p1 ON (i.contactperson1 = p1.id)
     LEFT OUTER JOIN person p2 ON (i.contactperson2 = p2.id)

If you need this query often and access it like a "table" just store it as a view.

1 Comment

Thanks for the advice. But, the firm is actually a huge legacy table. I created only the person table and my requirement is to update firm with new data.

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.