SQL: copy new values from one SQL table to another - sql

I am trying to copy values from one SQL table to another. The problem I have is that some of the values already exists in both tables and I only want to copy unique values. The below code should show what I ideally want (but which doesn't work).
INSERT INTO newTable (column1, column2, column3,ID)
SELECT column1, column2, column3,ID
FROM oldTable
WHERE Newtable.ID <> oldTable.ID
Can someone present a solution that works? Thanks in advance.

If you are using SQL server 2008+ you can use an except statement:
INSERT INTO newTable
(
column1,
column2,
column3,
ID
)
SELECT column1,
column2,
column3,
ID
FROM oldTable
EXCEPT
SELECT column1,
column2,
column3,
ID
FROM newTable;

You can try to use NOT exists on where clause.
INSERT INTO newtable
(column1,
column2,
column3,
id)
SELECT column1,
column2,
column3,
id
FROM oldtable o
WHERE NOT EXISTS (SELECT 1
FROM newtable t
WHERE t.id = o.id);
SQL-Server SQLFIDDLE
My-sql SQLFIDDLE
Oracle SQLFIDDLE

It really depends on the number of records you're dealing with.
This guy has benchmarked all the different ways of doing it. Which way is "better" depends on the number of records.
Also, if this is an ongoing process, you'll be better off with an INSERT trigger so you can just add the new records when they happen instead of needing to query the whole table every time.

Related

SQL Query - Ensuring my code is correct/Assistance with multiple queries in one Query (Subquerying)

I have a query today regarding SQL.
Basically here is what I am trying to do (this will also be useful for a couple other tables I have in this DB)
Table 1 = Members
Table 2 = Payments
Essentially trying to insert record into 1 table however have the query also copy over a memberID field if it's present for the individual.
INSERT INTO Payments (FirstName, LastName, PaymentMade)
VALUES ('', '', ''); AND UPDATE Payments
SET Payments.MemberID = Members.MemberID
FROM Members INNER JOIN Members ON Payments.MemberID = Members.MemberID;
Question is: Have I performed this correctly or have I missed a critical step here?
Many thanks! :)
I'm guessing you want to insert data from Table1 into Table2.
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
Use a Where condition for filtering out unwanted records.
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

Copy from one table with auto increment column

I want to copy the data from one table1 into table2. table1 does not have one of the columns that exists in table2 but it needs to be unique for my purpose.
My code is this so far:
set #uidfield=1000;
insert into table2 (column1, colulmn2, column3)
select column1, column2, (#uidfield := #uidfield+1)
from table1
This will copy about 6000 records, I don't have a good way to test my SQL statement, and I want to make sure its somewhat right before I send it to my SR analyst.
Thanks!
Are you looking for this?
insert into table2 (column1, colulmn2, column3)
select column1, column2,
1000 + row_number() over (order by (select null))
from table1;
This would be the SQL Server equivalent of your syntax, if I interpret your syntax as MySQL syntax.

How to delete the rownames column from a table in SQL Server?

I want to transfer rows from one table to another. However, one table has rownames while the other doesn't.
The error I get is:
Incorrect syntax near the word 'from'
How to handle this situation?
You can try:
INSERT INTO Table1 (Column1, Column2, Column3)
SELECT Column1, Column2, Column3
FROM Table2
WHERE Table2.Column1 = 'Test'
IF the columns are same in the both the table :
INSERT INTO table2
SELECT * FROM table1
WHERE condition;
If you want to transfer few rows but the the column names are different :
INSERT INTO table2 (column1, column2, column3) SELECT (column1, column2, column3) FROM table1 WHERE condition;

Extracting data from SQL Server with no duplicate of one particular column

Similar questions have been asked before and I tried all those solutions. Unfortunately none of them worked for me. Here is my requirement. There is a table with column1 (which is PK), column2, column3, column4, column5. Other than the first column (PK), all columns may have duplicate values in different rows. However, I want to pull a list of all rows with just one condition, column2 must not repeat. If there are multiple rows with duplicate values in column2, I just want any of the rows (say the first one, which can be done using min(column1)) and disregards the rows that has same values in column2.
I tried group by, didn't work because group by requires me to put all columns in group by and that results in a combination of all columns being unique.
EDIT
Thank you everybody for putting me on the right track. I tried many things and finally I think I found the right answer. Please comment if you see any issues with it.
select * from myTable where column1 in (select MIN(column1) from myTable group by column2)
To get the complete row with the minimum column1 per value of column2, you can use analytic functions to assign an order of the rows ordered by column1 partitioning by column2.
Then you can just pick the rows with row number 1 (the first row per partition) and you'll get the complete existing row with the smallest value of column1;
WITH cte AS (
SELECT column1, column2, column3, column4, column5,
ROW_NUMBER() OVER (PARTITION BY column2 ORDER BY column1) rn
FROM test
)
SELECT column1, column2, column3, column4, column5
FROM cte
WHERE rn=1;
An SQLfiddle to test with.
Just apply MIN() to all the other columns you want to retrieve, and use group by on the one column you want to be unique.
SELECT MIN(column1), column2, MIN(column3), MIN(column4), MIN(column5)
FROM your_table
GROUP BY column2
Update
As mentioned in the comments, the above solution does not return a complete row, but the minimum values across all columns in the group. To retrieve a complete row, based on the lowest value in column1:
SELECT column1, column2, column3, column4, column5
FROM your_table t1
WHERE t1.id = (
SELECT TOP 1 t2.id
FROM your_table t2
WHERE t2.column2 = t1.column2
ORDER BY t2.column1
)

appending 2 columns into one list in sql

I have 2 tables and each table has some 3 columns. i want to get one column such that one column from each table are apended one after the other
eg:- suppose one column in a table contains hai, how, are, you.
and another column in another column contains i, am, fine.
i want a query which gives hai, how, are, you,i,am,fine. in just one column
can anybody give a query for this in sql...
If I understand your schema correctly you have this
Table1: Column1
hai,
how,
are,
you.
Table2: Column2
i,
am,
fine.
Do This:
Insert Into Table1 (Column1)
Select Column2 From Table2
You will get this:
Table1: Column1
hai,
how,
are,
you.
i,
am,
fine.
If you have 3 Columns
Then just do this:
Insert Into Table1 (Column1, Column2, Column3) //the (Column1, Column2, Column3) is not neccessary if those are the only columns in your Table1
Select Column1, Column2, Column3 From Table2 //the Select Column1, Column2, Column3 could become Select * if those are the only columns of your Table2
EDIT: Do this if you don't want to modify any tables.
Select Column1, Column2, Column3
From Table1
UNION ALL
Select Column1, Column2, Column3
From Table2
Your question isn't very clear. One interpretation of it is that you want to UNION the two:
select column
from table1
union
select column
from table2;
If you really want all rows from both tables (and not the distinct values), UNION ALL will be faster than UNION.
If you want the rows in a certain order be sure to specify an ORDER BY clause.