insert into table without the intersection part - sql

In case I have two SQL tables ,table_a and table_b, both are same , except they may contain different data, I want to insert into the table_b all the rows from table_b that does not exist in table_a already, how should the query look like ? The tables contain id1 and id2 columns only.
Please tell me if my question is not clear
Insert into table_a ...
Thank you

; with cte_left as
(
select id1+id2
from table_a
where id1+id2 not in (select id1+id2 from table_b)
)
insert into table_b
select * from cte_left

You could use the EXCEPT operator:
INSERT INTO table_a (id1, id2)
SELECT id1, id2
FROM (
SELECT id1, id2
FROM table_b
EXCEPT
SELECT id1, id2
FROM table_a
) except_gry
SQL Fiddle demo here

Insert into table_a ( id1, id2 )
select b.id1, b.id2
from table_b b
left outer join table_a a on a.id1 = b.id1 and a.id2 = b.id2
where a.id1 is null

You can use not exist in your query:
insert into table_a(id1,id2)
select id1,id2
from table_b
where not exists(select id1,id2 from table_a)

Related

select query respecting conditions

i have my table containing 4 Columns (id, val1, val2, val3).
Does anyone knows how to select rows where val3 is the same where val1 is different.
for example
row1: (id1, user1, matheos, cvn)
row2: (id2, user2, matheos, cvn)
row3: (id3, user3, Claudia, bnps)
then i return the row1 and row2.
Your explanation is not entirely clear, but the following query will find matching rows according to the criteria you specified:
select a.*, b.*
from my_table a
join my_table b on b.val3 = a.val3
and b.val2 <> a.val2
and b.id < a.id
In order to produce the rows separately, you can also do:
select *
from my_table a
where exists (
select null from my_table b where b.val3 = a.val3 and b.val2 <> a.val2
)
Based on your explanation, you can try this:
select distinct t1.* from mytable t1
JOIN mytable t2 where t1.val3 = t2.val3
and t1.val1 != t2.val1;
Demo: SQL Fiddle

Insert into with output clause

INSERT INTO Table1(group, account)
OUTPUT inserted.Id, B.title, B.amount
INTO Table2(id2, title, amount)
SELECT A.*,
B.title,
B.amount,
B.id2
FROM Table1 AS A
LEFT OUTER JOIN
(SELECT title,
amount,
id2
FROM Table2) AS B
ON A.id = B.id2
i'm stuck with this..i have two join tables and what i want is to copy the same set of data from table1 to itself and copy the new id of the newly copied data from table1 to table2 column id2 by using OUTPUT clause.
but now with the query above i cant get through the column that i needed..how can i insert column B.title & B.amount to table2 ?
If table 1 and table 2 have a 1:1 relationship, and no foreign key exists between the two then you could do this in a single statement:
MERGE Table1 AS a
USING
( SELECT A.[group], A.account, B.title, B.amount, B.id2
FROM Table1 AS A
LEFT OUTER JOIN Table2 AS B
ON A.id = B.id2
) AS b
ON 1 = 0
WHEN NOT MATCHED THEN
INSERT ([group], account)
VALUES (b.[group], b.account)
OUTPUT inserted.Id, B.title, B.amount
INTO Table2(id2, title, amount);
Example on SQL Fiddle
Realistically though, if your tables are related they should have a foreign key, and in most cases they won't be 1:1, rather 1:n.
In which case you would still need to use MERGE to caputre both the new ID and the old ID, but you would then need to capture this mapping in a temporary table before performing a second insert to Table2:
DECLARE #Map TABLE (OldID INT NOT NULL, NewID INT NOT NULL);
MERGE Table1 AS a
USING
( SELECT A.ID, A.[group], A.account
FROM Table1 AS A
) AS b
ON 1 = 0
WHEN NOT MATCHED THEN
INSERT ([group], account)
VALUES (b.[group], b.account)
OUTPUT inserted.Id, b.ID
INTO #Map(NewID, OldID);
INSERT Table2 (id2, title, amount)
SELECT m.NewID, b.title, b.amount
FROM #Map AS m
INNER JOIN Table2 AS b
ON b.ID2 = m.OldID;
Example on SQL Fiddle

insert into one table from another

I want to insert some data of column1 from Table_B to Table_A if the data in Table_B does not exist in Table_A. For example, 'Headache' is in column1 of Table_B but not in column1 of Table_A. Thanks. I wrote the SQL below, but it did not work:
insert into Table_A(column1)
select column1
from Table_B
where column1 not in (select column1 from Table_A)
Try This:
INSERT INTO Table_A(column1)
SELECT B.column1
FROM Table_B B
LEFT JOIN Table_A A ON B.column1 = A.column1
WHERE A.column1 IS NULL
Insert into Table_A(column1)
select column1 from Table_B
left join Table_A on Table_B.column1 = Table_A.column1
where A.column1 is null
With no sample data it is hard to say for sure, but my best guess would be that you have a NULL value in table_A.Column1. if you did have a null values, your query would be equivalent to something like:
SELECT Column1
FROM Table_B
WHERE Column1 NOT IN (1, 2, 3, NULL);
Which is equivalent of:
SELECT Column1
FROM Table_B
WHERE Column1 <> 1
AND Column1 <> 2
AND Column1 <> 3
AND Column1 <> NULL;
Since Column1 <> NULL is not true, the query returns no results. The most syntactically similar way to achieve the desired result where you might have NULL columns is using NOT EXISTS:
INSERT INTO Table_A(column1)
SELECT Column1
FROM Table_B AS B
WHERE NOT EXISTS (SELECT 1 FROM Table_A AS A WHERE A.Column1 = B.Column1);
However, another method you could use is:
INSERT INTO Table_A(column1)
SELECT Column1
FROM Table_B AS B
LEFT JOIN Table_A AS A
ON A.Column1 = B.Column1
WHERE A.Column1 IS NULL;
In this by left joining to table_A then stating that A.Column1 has to be NULL, you are removing any records that already exist in Table_A.
I prefer the former (NOT EXISTS), because I think the intent is much more clear, but if you use MySQL the latter will perform better
Or you could also use:
INSERT INTO Table_A(column1)
SELECT Column1
FROM Table_B AS B
WHERE B.Column1 IS NOT NULL
AND B.COlumn1 NOT IN (SELECT A.Column1 FROM Table_A AS A WHERE A.Column1 IS NOT NULL);
What about using a MERGE statement?
MERGE INTO TABLE_A a
USING TABLE_B b ON (a.column1=b.column1)
WHEN NOT MATCHED THEN
INSERT (column1) VALUES (b.column1);

SQL join with distinct column on one table

Maybe I'm searching using the wrong words because I can't find the answer elswhere, but I need to join two tables but make sure the ID from one of the tables is distinct. Something like the below:
SELECT B.COLUMN_A, B.COLUMN_B, B.COLUMN_C
FROM TABLE1 A
JOIN TABLE2 B
ON (Distinct) A.COLUMN_A = B.COLUMN_A;
The value A.COLUMN_A from TABLE1 needs to be DISTINCT.
I've tried the below but that didn't work:
SELECT B.COLUMN_A, B.COLUMN_B, B.COLUMN_C
FROM TABLE1 A
JOIN (SELECT DISTINCT COLUMN_A FROM TABLE2) B
ON A.COLUMN_A = B.COLUMN_A;
I keep getting a ORA-00904: invalid identifer error on B.COLUMN_C. If I try to use ) AS B then I get a ORA-00905: missing keyword error.
If you don't care about the other values, use group by
SELECT b.column_a, b.column_b, b.column_c
FROM table1 a
JOIN (
SELECT column_a, max(column_b) as column_b, max(column_c) as column_c
FROM table2
GROUP BY column_a
) b ON a.column_a = b.column_a
Use a ROW_NUMBER to get a single row per COLUMN_A:
SELECT *
FROM table1 A
JOIN
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY COLUMN_A ORDER BY COLUMN_A) AS rn
FROM table2
) B
ON A.column_a = B.column_a
AND B.rn = 1
Maybe you need something like this:
select * from
(
select column_a,column_b,column_c
from
(
select column_a,column_b,column_c, count(1) over (partition by column_a) as num
from tableB
)
where num = 1
)tB
inner join tableA
using (column_a)
The double nesting is not necessary, but I hope it makes the query more readable
If you need col_a, col_b, col_c and want to ensure col_a never repeats and col_b, col_c values are not germane, then :
SELECT col_a, col_b, col_c
FROM table2
WHERE rowid in ( SELECT min(rowid)
FROM table2 A , table1 B )
WHERE B.col_a = A.col_a
GROUP BY A.col_a )
In above you choose one distinct row of Table2 that is also present in Table1. Then using that row's id you select all three columns.
You are not selecting any of the columns from TABLE1, so your join to (distinct) TABLE1 records is really just a semi-join, which is most easily expressed as:
SELECT B.COLUMN_A, B.COLUMN_B, B.COLUMN_C
FROM TABLE2 B
WHERE EXISTS ( SELECT 'at least one row in table1'
FROM TABLE1 A
WHERE A.COLUMN_A = B.COLUMN_A );

How to insert Distinct Records from Table A to Table B (both tables have same structure)

I want to insert only Distinct Records from Table "A" to Table "B". Assume both the tables has same structure.
If by DISTINCT you mean unique records that are on TableB that aren't already in TableA, then do the following:
INSERT INTO TableB(Col1, Col2, Col3, ... , Coln)
SELECT DISTINCT A.Col1, A.Col2, A.Col3, ... , A.Coln
FROM TableA A
LEFT JOIN TableB B
ON A.KeyOfTableA = B.KeyOfTableB
WHERE B.KeyOfTableB IS NULL
INSERT INTO B SELECT DISTINCT * FROM A
You might not want the id column of the table to be part of the distinct check, so use this solution if that's the case: https://stackoverflow.com/a/5171345/453673
INSERT INTO TableB
(Col1, Col2, ...)
SELECT DISTINCT Col1, Col2, ...
FROM TableA
INSERT INTO TableB
SELECT *
FROM TableA AS A
WHERE NOT EXISTS(SELECT * FROM TableB AS B WHERE B.Field1 = A.Field1)
-- If need: B.Field2 = A.Field2 and B.Field3 = A.Field3