SQL selection in the field

I want to do the following:

Select DISTINCT(tableA.column) INTO tableB.column FROM tableA 

The goal will be to select a single data set and then insert that data into a specific column of the new table.

+4
source share
4 answers

You are there very much.

 SELECT DISTINCT column INTO tableB FROM tableA 

It will be inserted into any column (s) specified in the selection list, so you will need to tableB your selection values ​​if you need to insert into tableB columns that are not in tableA .

SELECT INTO

+4
source
 SELECT column INTO tableB FROM tableA 

SELECT INTO will create the table as it inserts new records into it. If this is not what you want (if tableB already exists), you will need to do something like this:

 INSERT INTO tableB ( column ) SELECT DISTINCT column FROM tableA 

Remember that if tableb has more columns, which is only one, you will need to specify the columns into which you will be inserted (for example, I did this in my example).

+7
source

Try the following ...

 INSERT INTO tableB (column) Select DISTINCT(tableA.column) FROM tableA 
+2
source
The goal is to select a separate data set and then insert this data into a specific column of the new table.

I don’t know what tableB schema is ... if table B already exists and there is no unique restriction on the column that you can do, as any of the others suggests.

INSERT INTO tableB (column)Select DISTINCT(tableA.column)FROM tableA

but if you have a unique constraint for table B and it already exists, you will have to exclude these 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.

0
source

All Articles