tags:

views:

37

answers:

2

Hey there

I’ve got a table like this:

Create Table PersonAgent (
    PersonID    varchar2(10) not null,
    AgentID     varchar2(10) not null,
    Address     varchar2(50),
    City        varchar2(50),
    Country     varchar2(50)
)

Well I need to generate this table new, coze some data are incorrect.

If PersonID and AgentID are the same then I can take the other attributes as they are but if they are not the same then I need to read from another table.

Little Example:

INSERT INTO PersonAgent_copy(PersonID, AgentID, Address, City, Country)
Select Pa.Persid, Pa.Agentid, 
    (Case
      When Pa.Personid = Pa.Agentid
      Then pa.Address
      ELSE (SELECT p.Address
          FROM Person p
          Where Pa.Agentid = P.Personid)),
    (Case
      When Pa.Personid = Pa.Agentid
      Then pa.City
      ELSE (SELECT p.City
          FROM Person p
          Where Pa.Agentid = P.Personid)),
    (Case
      When Pa.Personid = Pa.Agentid
      Then pa.Country
      ELSE (SELECT p.Country
          From Person P
          Where Pa.Agentid = P.Personid))
FROM PersonAgent pa

(There are some more attributes that works the same)

What is the fastest way to do this?

+2  A: 

You can try this

INSERT INTO PersonAgent_copy(PersonID, AgentID, Address, City, Country)
Select Pa.Persid, Pa.Agentid, pa.Address, pa.City, pa.Country
FROM PersonAgent pa
where Pa.Personid = Pa.Agentid 
union all 
Select Pa.Persid, Pa.Agentid, p.Address, p.City, p.Country
FROM PersonAgent pa, Person p 
where Pa.Personid <> Pa.Agentid and Pa.Agentid = P.Personid

I am not tested this, but you can try it.

Trurl
Ahh damit why didnt i get this idea... :dThx mate!
Auro
When you want to copy much data use not a INSERT statement. Better is "CREATE TABLE PersonAgent_copy AS SELECT ...". That is much more faster!
Tim Krüger
Looks intresting Tim, does it copy FK and PKs?
Auro
@Auro: No, it does not create constraints or indexes. However, if you're creating the table, it may be faster to create the those after you insert the data in any case.
Allan
this maybe i will think about it if my solution is too slow. thx!
Auro
+1  A: 

You'd be a lot better off doing it this way:

INSERT INTO PersonAgent_copy(PersonID, AgentID, Address, City, Country)
select pa.persid, pa.agentid,
    Case
      When Pa.Personid = Pa.Agentid
      Then pa.Address
      ELSE p.Address
      END,
    Case
      When Pa.Personid = Pa.Agentid
      Then pa.City
      ELSE p.City
      END,
    Case
      When Pa.Personid = Pa.Agentid
      Then pa.Country
      ELSE p.Country
      END
from
   PersonAgent pa 
   left outer join
   Person p
   on pa.agent_id = p.person_id

The reason for this is that, with your method, every time personid and agentid are the same three queries have to be run against the Person table. This can add up very quickly. With my method, the Person table is queried once.

Allan
i think the performance between your sample and Trurl are nearly same
Auro
I meant that this is better than your original solution, not the other answer.
Allan