How to insert UNION data into a table - sql

I would like to use UNION on two tables in order to combine a similar field, then insert that UNION data into a different table.
Example:
Table1 has the following fields:
x
y
z
Table2 has the following fields:
x
w
v
I would like to perform UNION on x in order to insure there are no duplicate rows, then put that data in another table.
Example:
I would like MainTable to have the following fields:
x
y
z
w
v
As you can tell, all of the fields from both Table1 and Table2 exist in MainTable, but x has had UNION performed on it.
This SQL code does not work in a query, however, and is giving me Syntax Error in FROM Clause:
INSERT INTO MainTable(x)
SELECT x
FROM (Table1)
UNION
SELECT x
FROM (Table2)

Try:
INSERT INTO MainTable(x)
FROM
(
SELECT x
FROM (Table1)
UNION
SELECT x
FROM (Table2)
) as t

Related

Insert multiple rows into a table from two different tables in oracle?

I'm trying to insert multiple rows into my table using select but I'm getting not enough values error.
My query:
Insert into c(x, y) select * from a union all select * from d;
table a and b contains 2 records each and table c has one record.
try like below by specifying both column names
Insert into c(x, y)
select col1,col2 from a
union all
select col1,col2 from d
for union all both tables have the same number of colums and their data type also need to be same
List the columns explicitly:
Insert into c (x, y)
select col1, col2
from a
union all
select col1, col2
from d;
If one of tables has only one column, then use a placeholder for the value:
Insert into c (x, y)
select col1, col2
from a
union all
select col1, NULL
from d;

How to use CASE or IF-statement in Postgres to select from different table?

I want to make a selection from one of many tables. This selection depends on some condition. How can I make it?
I suppose it should be some like this (but it doesn't work):
CASE x
WHEN x=1 THEN
select Id,Name from table1
WHEN x=2 THEN
select Id,Name from table2
WHEN x=3 THEN
select Id,Name from table3
END CASE;
Inefficient solution the queries all 3 tables, but immitates a switch statement in code (assuming the retrieved columns are equivalent)
declare #inputValue int = 1
SELECT * FROM (
SELECT 1 [key], Id from table1
UNION ALL
SELECT 2, Id from table2
UNION ALL
SELECT 3, Id from table3
) x
where x.[key] = #inputValue
One way to do it:
SELECT id, name
FROM table1
WHERE x = 1
UNION ALL
SELECT id, name
FROM table2
WHERE x = 2
UNION ALL
SELECT id, name
FROM table3
WHERE x = 3
Only one table's data will be returned (if x is any of those values).

Copy multiple columns from a table to another table with extra columns

I want to copy records from one table to another table with extra columns.
The table columns looks like this
Table1: A,B,C,...,X
Table2: A,B,C,...,X,Y,Z
I know that i can do it in the following way
Insert into table2
Select
A,
B,
C,
...
X,
'1',
'1'
from table1
But it is a very inefficient way if the table contains lots of columns. I need to do the similar procedure for several tables. Is there a better way to do it?
I tried the following but it doesn't work
insert into table2 select * , '1', '1' from table1;
The thing you tried should work, just add alias.
INSERT INTO table2
SELECT t1.*,
'1',
'1'
FROM table1 t1;
Insert into table2 ( A, B, C, ... , X, Y , Z) SELECT ( A, B, C, ... , X, '1' , '1') from table1 ;
In this case Y & Z are Strings (characters).

Select from a subset of data

I am working on a query in MS SQL Server 2014
That basically is a lot of unions
select x, y
where a = b
union
select x, y
where a = b
union
select x, y
where a = b
It works fine, however the where clauses are identical on every select. And for ease of maintenance I was wondering if there is a better, cleaner way to do this.
I was thinking of somehow selecting the data with the where clauses first then doing all the other queries only on this data.
But im open to any ideas on how to improve this query.
;WITH Test AS
(
SELECT x, y
UNION
SELECT x, y
UNION
SELECT x, y
)
SELECT * FROM Test
WHERE a = b
You could use a sub query and take the where clause outside of it for ease of maintenance.
Just make sure you bring all the columns through in the sub query that you will need in the where clause. Eg
SELECT * FROM
(
SELECT x,y,a,b FROM table1
union
SELECT x,y,a,b FROM table2
UNION
SELECT x,y,a,b FROM table3
)subquery
WHERE a=b
Select *
From
(
select x, y
union
select x, y
union
select x, y
) MyDerivedTable
Where ...
Make sure to include the columns you need to filter in the select statement of the tables inside the derived table.

SQL Server: Find Values that don't exist in a table

I have a list or set of values that I would like to know which ones do not currently exist in a table. I know I can find out which ones do exist with:
SELECT * FROM Table WHERE column1 IN (x,x,x,x,x)
The set is the values I am checking against. Is there a way to find out which values in that set do not exist in column1? Basically, I'm looking for the inverse of the sql statement above.
This is for a report, so all I need is the values that don't exist to be returned back.
I have and could do this with a left join and putting the values in another table, but the values I check are always different and was hoping to find a solution that didn't involve clearing a table and inserting data first. Trying to find a better solution for me if one exists.
You can also use EXCEPT as well as the OUTER JOIN e.g.
SELECT * FROM
(
SELECT -1 AS N
UNION
SELECT 2 AS N
) demo
EXCEPT
SELECT number
FROM spt_values
WITH q(x) AS
(
SELECT x1
UNION ALL
SELECT x2
UNION ALL
SELECT x3
)
SELECT x
FROM q
WHERE x NOT IN
(
SELECT column1
FROM [table]
)
Put the values you want to check for in a table A
LEFT OUTER JOIN the table A against your Table WHERE Table.column1 IS NULL
SELECT column1
FROM A
LEFT OUTER JOIN
Table
ON A.column1 = Table.column1
WHERE Table.column1 IS NULL
This will only show the rows that exist in A but not in Table.
As you want some of the values from the set in the result, and you can't take them from the table (as you want the ones that doesn't exist there), you have to put the set in some kind of table or result so that you can use that as source.
You can for example make a temporary result, that you can join against the table to filter out the ones that does exist in the table:
select set.x
from (
select 1 as x union all
select 2 union all
select 3 union all
select 4 union all
select 5
) as set
left join Table as t on t.column1 = set.x
where t.columnn1 is null
One way you can do it is:
SELECT * FROM Table WHERE column1 NOT IN(...);
Use the NOT operator:
SELECT * FROM Table WHERE column1 NOT IN (x,x,x,x,x)