I have 50 datasets. Each dataset has different records of 3 variables each. I want to compile one dataset that all records from 50 datasets for those 3 variables.
Example is Table 1:
var1 | var2 | var3 |
---|---|---|
val1 | val2 | val3 |
I have 50 datasets. Each dataset has different records of 3 variables each. I want to compile one dataset that all records from 50 datasets for those 3 variables.
Example is Table 1:
var1 | var2 | var3 |
---|---|---|
val1 | val2 | val3 |
Table 2:
var1 | var2 | var3 |
---|---|---|
val2 | val3 | val4 |
Table 3:
var1 | var2 | var3 |
---|---|---|
val5 | val1 | val4 |
I have 50 such tables and I want end result as:
var1 | var2 | var3 |
---|---|---|
val1 | val2 | val3 |
val2 | val3 | val4 |
val5 | val1 | val4 |
I assigned table 1 as my final table and tried inserting into other tables.
insert into final_table
select var1,var2,var3 from table2
Is there a way for me to repeat this operation without having to copy and paste this 50+ times?
Share Improve this question edited yesterday DarkBee 15.6k8 gold badges70 silver badges115 bronze badges asked Feb 6 at 21:01 user10969476user10969476 113 bronze badges 4 |1 Answer
Reset to default 0Some artisanal solutions, from less to more automatized;
depending on the frequency you need to execute it, maybe a fully automatized solution is not worth it.
With a spreadsheet
In a spreadsheet where you paste the table names into column A, put the following formula in B1 then extend formula to the bottom:
="insert into final_table select var1, var2, var3 from "&A1&";"
Then copy-paste column B to DBVisualizer and run them.
N.B.: you can do this with any scripting language you're at ease with.
With copy-paste in DBVisualizer
Same as with a spreadsheet, except the formula is done in SQL.
But see my comment, we need to know your DB engine and table structure to be able to answer.
In 1 consolidated instruction in the DB
See my comment too, we have to know the DB server and table structure.
UNION ALL
(includes duplicates); there are lots of guides on the topic, example – vakio Commented 2 days ago