Merge 3 tables in one table - postgresql

I have 3 tables (t1, t2, t3) in postgresql. they all have same number of columns (168 cols) with same type and overall 300k rows.
How can I add all of them in one table?

insert into t4
select * from t1
union all
select * from t2
union all
select * from t3
or, if you want to create table during select:
select * into t4 from t1
union all
select * from t2
union all
select * from t3;
SQL FIDDLE EXAMPLE

Related

postgresql How to share cte among different tables in plain sql?

Say select id from some_expensive_query is the cte I want to share. Currently I write two sql in a transaction:
with t as (select id from some_expensive_query) select * from t1 join t on t.id =t1.id;
with t as (select id from some_expensive_query) select * from t2 join t on t.id =t2.id;
As you can see, the cte is executed twice but I want something like:
t = select id from some_expensive_query;
select * from t1 join t on t.id =t1.id;
select * from t2 join t on t.id =t2.id;
for portability, I don't want to use pgsql or functions, anyway to solve this?
Why don't you use union all ?
with t as (select id from some_expensive_query)
select * from t1 join t on t.id =t1.id
union all
select * from t2 join t on t.id =t2.id;

Postgres join involving tables having join condition defined on an text array

I have two tables in postgresql
One table is of the form
Create table table1(
ID serial PRIMARY KEY,
Type []Text
)
Create table table2(
type text,
sellerID int
)
Now i want to get all the rows from table1 which are having type same that in table2 but the problem is that in table1 the type is an array.
In case the type in the table has an identifiable delimiter like ',' ,';' etc. you can rewrite the query as regexp_split_to_table(type,',') or versions later than 9.5 unnest function can be use too.
For eg.,
select * from
( select id ,regexp_split_to_table(type,',') from table1)table1
inner join
select * from table2
on trim(table1.type) = trim(table2.type)
Another good example can be found - https://www.dbrnd.com/2017/03/postgresql-regexp_split_to_array-to-split-string-using-different-delimiters/
SELECT
a[1] AS DiskInfo
,a[2] AS DiskNumber
,a[3] AS MessageKeyword
FROM (
SELECT regexp_split_to_array('Postgres Disk information , disk 2 , failed', ',')
) AS dt(a)
You can use the ANY operator in the JOIN condition:
select *
from table1 t1
join table2 t2 on t2.type = any (t1.type);
Note that if the types in the table1 match multiple rows in table2, you would get duplicates (from table1) because that's how a join works. Maybe you want an EXISTS condition instead:
select *
from table1 t1
where exists (select *
from table2 t2
where t2.type = any(t1.type));

Create table from union of two CTE

How can we create a table from the following query that unions 2 CTEs?
with a as (.....),
b as (.....)
Select *
FROM a
UNION
SELECT *
FROM b

How do I join two tables with same column names keeping values from one the tables

I have these two tables:
Table1
-------
id|a|b|c|d|
1 |0|1|0|6|
and
Table2
-------
id|a|c|
1 |3|2|
How do I join these two tables keeping the values from table2 but also the columns from table1, so that the table would look like this afterwards:
TableJoined
-------
id|a|b|c|d|
1 |3|1|2|6|
Tried with
SELECT * FROM Table2 a JOIN Table1 b WHERE a.id = b.id;
hoping that the first table mentioned would be the overrider
Or I guess you could do:
SELECT b.id, a.a, b.b, a.c, b.d FROM Table2 a JOIN Table1 b WHERE a.id = b.id;
You can achieve that by qualifying the * with the table alias:
SELECT a.* FROM Table2 a JOIN Table1 b WHERE a.id = b.id;
But you should never use * in a SELECT list except in ad-hoc queries. (The exception is count(*) which is OK to use).

CloudKit - How to perform Not Exists query?

I want to perform a database query in CloudKit like in SQL:
SELECT *
FROM table1 t1
WHERE NOT EXISTS
(SELECT *
FROM table2 t2
WHERE t1.userid = t2.userid)
Do you know how to do that?
Try
SELECT *
FROM table1 t1
WHERE t1.userid NOT IN(
SELECT t2.userid
FROM table2 t2);
or
SELECT t1.userid
FROM table1 t1
MINUS
SELECT t2.userid
FROM table2 t2;