tags:

views:

91

answers:

2

I have two query results from different tables like below.

ID  F1  F2  F3      ID  F3  F4  F5
S1  d11  d12  d13      S2  d23  d24  d25
S2  d21  d22  d23      S3  d33  d34  d35
S3  d31  d32  d33      S4  d43  d44  d45

ID and F# are field names, the others are values.
I want to get a new result in a similar form to the following from the results above.

ID  F1  F2  F3  F4  F5
S1  d11  d12  d13
S2  d21  d22  d23
S2        d23  d24  d25
S3  d31  d32  d33
S3        d33  d34  d35
S4        d43  d44  d45

Is this possible in SQL? I'm programming in VBA with ADO.
I've never done such a task before.

Edit: The fields of the first two tables are dynamic. I can't predict how many there are or what they are.

+7  A: 
select ID, F1, F2, F3, NULL as F4, NULL as F5
from Table1
union all
select ID, NULL as F1, NULL as F2, F3, F4, F5
from Table2
RedFilter
+1: Too easy ;)
Juliet
Thank you, but it's not so easy. The first two tables are query results from Excel tables which grow with time, and the query strings to get them are built programmingly. I can't predict how many fields there will be. There can be F6, F7, ... They are dynamic.
phoenies
@phoenies: when you don't know the schema before-hand, then SQL is pretty much the wrong language to use. *Or* you will have to write code that **generates** the SQL according to some mechanism.
Joachim Sauer
A: 

Maybe you could do a full outer join on two sub-queries that generate a fake join key that is never equal. This way you could use a select * to get any columns that exist in the two tables.

select * from 
(select 1 as MY_ID, Table1.* from Table1) AS A
FULL OUTER JOIN
(select 2 as MY_ID, Table2.* from Table2) AS B
ON A.MY_ID = B.MY_ID
Jesse