Skip to content
Advertisement

insert Into and Union of Tables

I’m using MS-Access. I have a table A looks like :

 id isin    typ1    typ2                                                                                 
  1 aa      typA    typB                                                                                 
 2  bb      typD    typC

I need to insert into a table B that looks like:

id  isin    fld     value                                                                                
 1   aa     typ1    typA                                                                                 
 2   aa     typ2    typB                                                                                 
 3   bb     typ1    typD                                                                                 
 4   bb     typ2    typC

Can I do it with an SQL instruction?

Advertisement

Answer

If the id field in table B is an Autonumber and ordering of the values is important, then you can use

insert into B (isin, fld, [value])
SELECT isin, fld, [value] from
  (select isin, "typ1" as fld, typ1 as [value] from A
  union all
  select isin, "typ2" as fld, typ2 from A) as R
order by isin, fld

keep in mind that the value field name is a reserved keyword in MS Access.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement