0

I need to create one master table from 5 tables, the difficulty is that the same column across the tables may have a different name. so for instance

For simplicity I`m just going to give an example for 2 tables

+----+----+
| 1  | 2  |
+----+----+
| PO | P  |
| VE | V  |
| TE | TE |
| LO | LO |
| IN |    |
| D  |    |
| X  |    |
| Y  |    |
|    | A  |
|    | B  |
|    | C  |
+----+----+

so as you can see PO doesn`t have the same column name as the corresponding value in table 2 yet they are the same record. I need to aggregate these 2 tables into one master.

What I did was began with the table that has the most repeated columns and I am trying to merge the other tables into it. When there is a column only found on one table I want the other fields to display null. Also I don't want any duplicates. Hope someone can help me out!

Cheers

4
  • 1
    By "Variables" do you mean column names? Table data? Commented Aug 11, 2014 at 16:16
  • The list of letters you posted doesn't make any sense. Please provide the table strucutre and your expected result. Commented Aug 11, 2014 at 16:16
  • My apologies, kind of new to posting here. long time viewer! and by variables i did mean column names. Commented Aug 11, 2014 at 16:24
  • sensefulsolutions.com/2010/10/format-text-as-table.html ... Commented Aug 11, 2014 at 16:25

1 Answer 1

1

yet they are the same record.

No, they are not.

They could, however, represent different views of the same business entities. To "merge" them you must first specify what the JOIN criterion between them shall be.

Given it is

one.PO = two.P.

Then you must write a SQL statement like

SELECT one.PO AS ID, 
       one.VE, 
       /*same for TE, LO, IN, D, X, Y, */ 
       two.A, 
       two.B, 
       two.C
  INTO t_what_the_frak_the_new_table_shall_be_called
  FROM t_what_the_frak_table_1_is_called AS one,
       JOIN t_what_the_frak_table_2_is_called AS two 
       ON one.PO = two.P;
GO
Sign up to request clarification or add additional context in comments.

7 Comments

why did you set one.PO as ID?
is there a way to insert all fields without actually referencing them (you commented that i need to do it for all columns)
FirstQuestion: Because you don't us details about your data structure, thus I have to guess. Second: Sort of. But that would be way beyond the SQL skill you have yet demonstrated here.
I get that, but I mean what was the point of setting it as ID? you never used "ID" again
Would it become clearer if I call it "foobar" instead?
|

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.