Insert into table without duplicates - sql

I have a Table A from where I have to copy Data to Table B. Now problem is In both table A and Table B there is a column ID which is primary key and can't be null.Table A is having Duplicates. Can any one tell me How to insert Data into Table B from Table A without Duplicates?

It would be something like
INSERT INTO TableA(ID) SELECT DISTINCT ID FROM TableB B LEFT JOIN TableA A ON A.ID = B.ID WHERE A.ID IS NULL

You can use the DISTINCT function in a select statement to remove duplicates.
In the example I'm going to assume that both tables have 3 columns called ID, Name and Surname:
insert into tableB (ID, Name, Surname)
select
distinct(ID) as ID
,Name
,Surname
from tableA
;
Please note that the DISTINCT function will provide distinct rows.

Related

Select between multiple lines

I have this situation:
I need to select data from tableA where its foreign key refers to tableB
like: tableA.tableB_id = tableB id informed
OR
the second restriction refers to postal code informed on tableB, there is initial postal code and final postal code in tableB, and a single postal code on tableA.
Example:
CREATE TABLE tableA (
id integer PRIMARY KEY,
tableB_id INTEGER,
postalCode varchar(10)
);
CREATE TABLE tableB (
id integer PRIMARY KEY,
InitialPostalCode varchar(10),
FinalPostalCode varchar(10)
);
Table B is a LIST of locations with multiple initial and final postal codes
I need to select from tableA where tableB_id = tableB.id OR tableA.cep is between tableB.InitialPostalCode and tableB.FinalPostalCode
SELECT
id,
postalCode
FROM
tableA
WHERE
tableB_id = ID
OR postalCode BETWEEN...
SELECT
InitialPostalCode,
FinalPostalCode
FROM
tableB
WHERE
id = some ID;
how can I merge these two queries?
thanks everyone!
Is this what you want?
SELECT a.id, a.postalCode
FROM tableA a
WHERE EXISTS (SELECT 1 FROM tableB b WHERE b.id = a.tableB_id) OR
EXISTS (SELECT 1 FROM tableB b WHERE a.cep BETWEEN b.InitialPostalCode AND b.FinalPostalCode);
I prefer to use EXISTS because:
You only want columns from tableA, so tableB does not need to be in the FROM clause.
Each subquery can be optimized with appropriate indexes.
There is no reason to remove duplicates after the processing.
OR in ON clauses can be very inefficient.

Insert new/Changes from one table to another in Oracle SQL

I have two tables with same number of columns :-Table A and Table B
Every day I insert data from Table B to Table A. now the insert query is working
insert into table_a (select * from table_b);
But by this insert the same data which was inserted earlier that is also getting inserted. I only want those rows which are new or are changed from the old data. How can this be done ?
You can use minus:
insert into table_a
select *
from table_b
minus
select *
from table_a;
This assumes that by "duplicate" you mean that all the columns are duplicated.
If you have a timestamp field, you could use it to limit the records to those created after the last copy.
Another option is, assuming that you have an primary key (id column in my example) that you can use to know whether a record has already been copied, you can create a table c (with the same structure as a and b) and do the following:
insert into table c
select a.* from table a
left join table b on (a.id=b.id)
where b.id is null;
insert into table b select * from table c;
truncate table c;
You need to adjust this query in order to use the actual primary key.
Hope this helps!
If the tables have a primary or unique key, then you could leverage that in an anti-join:
insert into table_a
select *
from table_b b
where not exists (
select null
from table_a a
where
a.pk_field_1 = b.pk_field_1 and
a.pk_field_2 = b.pk_field_2
)
You don't say what your key is. Assuming you have a key ID, that is you only want ID's that are not already in Table A. You can also use Merge-Statement for this:
MERGE INTO A USING B ON (A.ID = B.ID)
WHEN NOT MATCHED THEN INSERT (... columns of A) VALUES (... columns of B)

SQL: Single select query from 2 Non-Joining tables

I have 2 tables which doesn't have any references to each other and I'm trying to create a third table (for reference lookup) by selecting from fields from both tables.
TableA has an A_ID
TableB has a B_ID
I want to create Table C which has a 1 to 1 reference between A_ID to B_ID
where A_ID = FirstID and B_ID = SecondID, I can't join the 2 tables because there's nothing in common.
Something like:
Insert INTO [TableC]
(FirstID, SecondID)
SELECT
A_ID As FirstID,
(Select B_ID From TableB)
FROM TableA
Basically we are creating a relationship right now with Table C so that we can use it to reference the two tables in the future using the their IDs.
Assuming TableA and TableB truly have nothing in common, then what you want is the Cartesian product, that is, for every row in A times every row in B.
INSERT INTO TableC(FirstID,SecondID)
SELECT A_ID,B_ID
FROM TableA
CROSS JOIN TableB
Perhaps what you really want is to join them by ROW_NUMBER().
INSERT INTO TableC(FirstID,SecondID)
SELECT A_ID,B_ID
FROM (SELECT A_ID,ROW_NUMBER() OVER (ORDER BY whatever) as rownumA FROM TableA) a
FULL OUTER JOIN (SELECT B_ID,ROW_NUMBER() OVER (ORDER BY whatever) as rownumB FROM TableB) b ON a.rownumA=b.rownumB

SQL Select Into Field

I want to accomplish something of the following:
Select DISTINCT(tableA.column) INTO tableB.column FROM tableA
The goal would be to select a distinct data set and then insert that data into a specific column of a new table.
SELECT column INTO tableB FROM tableA
SELECT INTO will create a table as it inserts new records into it. If that is not what you want (if tableB already exists), then you will need to do something like this:
INSERT INTO tableB (
column
)
SELECT DISTINCT
column
FROM tableA
Remember that if tableb has more columns that just the one, you will need to list the columns you will be inserted into (like I have done in my example).
You're pretty much there.
SELECT DISTINCT column INTO tableB FROM tableA
It's going to insert into whatever column(s) are specified in the select list, so you would need to alias your select values if you need to insert into columns of tableB that aren't in tableA.
SELECT INTO
Try the following...
INSERT INTO tableB (column)
Select DISTINCT(tableA.column)
FROM tableA
The goal would be to select a distinct data set and then insert that data into a specific column of a new table.
I don't know what the schema of tableB is... if table B already exists and there is no unique constraint on the column you can do as any of the others suggest here....
INSERT INTO tableB (column)Select DISTINCT(tableA.column)FROM tableA
but if you have a unique constraint on table B and it already exists you'll have to exclude those values already in table B...
INSERT INTO tableB (column)
Select DISTINCT(tableA.column)
FROM tableA
WHERE tableA.column NOT IN (SELECT /* NOTE */ tableB.column FROM tableB)
-- NOTE: Remember if there is a unique constraint you don't need the more
-- costly form of a "SELECT DISTICT" in this subquery against tableB
-- This could be done in a number of different ways - this is just
-- one version. Best version will depend on size of data in each table,
-- indexes available, etc. Always prototype different ways and measure perf.

SQL insert statement

I want to do the following:
I have a table called Name which has an ID field.
I have another blank table called Transcript
I want to take the ID#s from Name and insert them into Transcript where they do not exist.
Secondly I want to create 10 records with a different COURSE# value in the Transcript table.
Therefore for each Name.ID I would like 10 records in Transcript.ID with a different value under course # ie; 101,201,301
Something like this might work:
INSERT INTO TableB
SELECT TableA.id FROM TableA
LEFT OUTER JOIN TableB
ON TableA.name = TableB.name
WHERE TableB.id IS null
Another query that will return the same thing
INSERT INTO TABLEB
SELECT TableA.ID FROm TableA WHERE ID NOT IN (SELECT Id FROM TableB)
Assuming that you are getting your course numbers from some outside courses table, here is the t-sql:
INSERT INTO transcript (name_id, course_id)
SELECT n.name_id, c.course_id
FROM name n
CROSS APPLY courses c
WHERE n.last_name = 'xxx'
This will insert all of the courses in the table for all of the names found by the where clause.