select data from 8 tabels into one temp table - tsql

I need to insert data from several tables with all the same field names into one temp table, i know i can use cursor/loop to do this, i wanted to know is there a quicker way of doing this.
select from table 1, table 2, table 3, into #temptable.

select * into #temptable from table1
insert into #temptable select * from table2
insert into #temptable select * from table3
The first query creates the temp table on insert, the rest just keep adding data.

Related

Liquibase insert select multiple rows postgres

I want to insert into table1 multiple rows from table2. The problem is that I have some fields in table1 that I want to compute, and some rows that I want to select from table2. For example something like this:
insert into table1 (id, selectField1, selectField2, constant)
values ((gen_random_uuid()), (select superField1 from table2), (select superField2 from table2), 'test');
So the logic is to select superField1 and superField2 from all the rows in the table2 and insert them into table1 with constant value test and generated uids. superField1 and superField2 should be from the same row in table2 when inserting in table1. How can I achieve something like this using liquibase?
P.S: I'm using <sql> tag since it's easier to implement using SQL than using XML changeset, but if you know how to do it in XML that would be appreciated too, but just in SQL will be enough too. DBMS is postgres.
Don't use the VALUES clause if the source is a SELECT statement:
insert into table1 (id, selectField1, selectField2, constant)
select gen_random_uuid(), superField1, superField2, 'test'
from table2;

How to use the same common table expression in two consecutive psql statements?

I'm trying to perform a pretty basic operation with a few steps:
SELECT data from table1
Use id column from my selected table to remove data from table2
Insert the selected table from step 1 into table2
I would imagine that this would work
begin;
with temp as (
select id
from table1
)
delete from table2
where id in (select id from temp);
insert into table2 (id)
select id from temp;
commit;
But I'm getting an error saying that temp is not defined during my insert step?
Only other post I found about this is this one but it didn't really answer my question.
Thoughts?
From Postgres documentation:
WITH provides a way to write auxiliary statements for use in a larger
query. These statements, which are often referred to as Common Table
Expressions or CTEs, can be thought of as defining temporary tables
that exist just for one query.
If you need a temp table for more than one query you can do instead:
begin;
create temp table temp_table as (
select id
from table1
);
delete from table2
where id in (select id from temp_table);
insert into table2 (id)
select id from temp_table;
commit;

Redshift move data from one table to another table

insert into table1 select * from table2
tabl1 have one addition column as compare to table2.
how can i move the data of other columns from table2 to table1 without set the all columns name individually in the insert query for Redshift ?
Is there any idea ?
If you really want to do this you have to put all extra columns at the table2 at the end of the column list and then you'll be able to set nulls or values after select star like this:
insert into table1
select *, null, null, null
from table2

Create a new table from Union two tables with union in postgres

I would like to create a new table as the result of the union of two tables without duplicates. I searched in stackoverflow and I found a question with exactly what I want but using mysql Create a new table from merging two tables with union.
Solution in mysql
CREATE TABLE new_table
SELECT * FROM table1
UNION
SELECT * FROM table2;
I tried to do something similar but I got:
SQL error.
I would like to achieve this if is possible with an statement similar to mysql.
I know that if you create a new table first with the fields that I want. I can do a select into this table over the union of this tables. If there aren't other option well I have to do something like this.
But in summary If possible to do something similar to the question with mysql in postgres. I would like to use syntactic sugar to do that
Thanks in advance
Update
In order to clarify I have two table with equal structure
TABLE1(id,field1,field2,field3)
TABLE2(id,field1,field2,field3)
and The table that I want
TABLE3(id,field1,field2,field3)
Notice that I tried
CREATE TABLE new_table as
SELECT * FROM table1
UNION
SELECT * FROM table2;
and it works but didn't put the fields in the correct place for example put field3 of table 1 in field 1 of table_result
You are missing the AS keyword:
CREATE TABLE new_table
AS
SELECT * FROM table1
UNION
SELECT * FROM table2;
If you need the columns in a specific order, then specify them in the select:
CREATE TABLE new_table
AS
SELECT id, column1, column2, column3
FROM table1
UNION
SELECT id, column1, column2, column3
FROM table2;
More details in the manual:
https://www.postgresql.org/docs/current/static/sql-createtableas.html

Insert data into table effeciently, postgresql

I am new to postgresql (and databases in general) and was hoping to get some pointers on improving the efficiency of the following statement.
I am inserting data from one table to another, and do not want to insert duplicate values. I have a rid (unique identifier in each table) that are indexed and are Primary Keys.
I am currently using the following statement:
INSERT INTO table1 SELECT * FROM table2 WHERE rid NOT IN (SELECT rid FROM table1).
As of now the table one is 200,000 records, table2 is 20,000 records. Table1 is going to keep growing (probably to around 2,000,000) and table2 will stay around 20,000 records. As of now the statement takes about 15 minutes to run. I am concerned that as Table1 grows this is going to take way to long. Any suggestions?
This should be more efficient than your current query:
INSERT INTO table1
SELECT *
FROM table2
WHERE NOT EXISTS (
SELECT 1 FROM table1 WHERE table1.rid = table2.rid
);
insert into table1
select t2.*
from
table2 t2
left join
table1 t1 on t1.rid = t2.rid
where t1.rid is null