This question already has answers here:
Insert, on duplicate update in PostgreSQL?
(18 answers)
How to UPSERT (MERGE, INSERT ... ON DUPLICATE UPDATE) in PostgreSQL?
(7 answers)
Closed 9 years ago.
i'm manually updating table in my DB using import functionality in PostgreSQL. Working with large number of data i came across an issue of duplicating primary keys. I am looking for a script to upload only values that do not violate primary key assumption, and those that to violate are to be ignored (not uploaded or updated).
I have already seen a code that would kind of do what i need however not quite sure if it will work for me.
Columns i am working with are:
acc_ph_id (primary_key);acc_ph_no;ph_type;ph_flag
Any suggestions will be highly appreciated as i am rather new to Postgresql in general.
Upload the table into a staging table with no constraints.
Then load the table into the full table eliminating duplicates:
insert into real_table(acc_ph_id, acc_ph_no, ph_type, ph_flag)
select distinct on (acc_ph_id) acc_ph_id, acc_ph_no, ph_type, ph_flag
from staging_table
order by acc_ph_id;
EDIT:
Oh, if the problem is the keys that already exist, then do:
insert into real_table(acc_ph_id, acc_ph_no, ph_type, ph_flag)
select distinct on (acc_ph_id) acc_ph_id, acc_ph_no, ph_type, ph_flag
from staging_table st
where not exists (select 1
from real_table rt
where rt.acc_ph_id = st.acc_ph_id
)
order by acc_ph_id;
Related
This question already has answers here:
ERROR 1062 (23000): Duplicate entry '2147483647' for key 'PRIMARY'
(3 answers)
Closed 2 years ago.
yesterday I had the problem that I couldn't insert multiple values into my columns. It turned out that the problem was that my key was missing an auto_increment. I've been trying to to the same thing(inserting multiple values into columns) now on another table but for some reason it doesn't work for me. I made the same changes as I did to the other table which works now. I can't imagine what I could've missed, can anyone help me with this?
Here the table specification and error:
When you mark the PK as auto-increment you do not have to insert values to that entry anymore as it will be incremented automatically, therefore will not trigger any duplicate problems due to being different to the previous PK entries. I suggest you to add only the values of the non PK columns.
INSERT INTO tbllaptops(L_ProzTyp, L_Akku, L_MietgebuehrProTag) -> VALUES ('i56600', '8000mha', '8.5')
This question already has answers here:
How can I remove duplicate rows?
(43 answers)
Closed 4 years ago.
I have a SQL Server table with ~100 columns including the columns Id and CreationDate. Due to a bad constraint in its initial design, there are now many duplicate rows (i.e. rows whose values are identical across ALL columns).
Can you suggest a script to remove those duplicate rows?
Also, what would be a script to select all distinct Ids with the latest CreationDate?
Thanks
You can use the following script to remove duplicate rows from a Microsoft SQL Server table:
SELECT DISTINCT *
INTO duplicate_table
FROM original_table
GROUP BY key_value
HAVING COUNT(key_value) > 1
DELETE original_table
WHERE key_value
IN (SELECT key_value
FROM duplicate_table)
INSERT original_table
SELECT *
FROM duplicate_table
DROP TABLE duplicate_table
When this script is executed, it follows these steps:
It moves one instance of any duplicate row in the original table to a duplicate table.
It deletes all rows from the original table that also reside in the duplicate table.
It moves the rows in the duplicate table back into the original table.
It drops the duplicate table.
This question already has answers here:
Upserting in MS-access
(6 answers)
Closed 6 years ago.
I know this question must have been asked a million times but I have yet to find a "NON-VBA" solution to this in MS Access 2007 SQL.
I am using 2 Tables TBLDestination and TBLSource. I have to update the destination table records from source table records when a matching ID is found. For a non-matching ID (i.e. new ID) I want to insert the new record
(Please refer the tables below).
-----TBLSource-------
ID (match if ID exists in Destination table)
EmpName
EmpAdd
---TBLDestination-----
ID
EmpName (to be updated/inserted)
EmpAdd (to be updated/inserted)
Salary
Access DOES have the equivalent using an UPDATE with a LEFT JOIN. See here.
MS Access doesn't have an equivalent to UPSERT where you can do both in a single query.
However, you can get the same effect by doing an UPDATE query where you join to the table you want to update from. Then you do an INSERT query where you OUTER JOIN to the table and return only those where it doesn't exist.
This question already has answers here:
How to put more than 1000 values into an Oracle IN clause [duplicate]
(11 answers)
Closed 9 years ago.
I have a file SRQ which is having 10000 SRQ_ID which are unique.
I have one table(TABLE1) which is having 2 columns namely SRQ_ID,WORK_ID .
I needs to write a query which will search the table(TABLE1) for all the SRQ_ID's in the file SRQ and will display the output with corresponding WORK_ID.
I tried the below code. But IN clause is only applicable for 1000 records. How to run the same if I have 100k records?
select WO_ID
from TABLE1
where SRQ_ID in ('B6512DF0','5838FABC','EC5D804C','074DD65C')
Is there a reason you can't just do a join between the tables using SRQ_ID?
select wo_id from table1 join srq using srq_id
This will give you the work id for all rows that have a srq_id value in the srq table.
If the file s on the database server then you could access it as an external table, and join the two. Otherwise, I'd suggest bulk loading the codes into a global temporary table and performing the join against that.
In case you don't like to create a temporary table, you can use a nested table:
CREATE OR REPLACE TYPE VARCHAR_TABLE_TYPE AS TABLE OF VARCHAR2(20);
select WO_ID
from TABLE1
where SRQ_ID MEMBER OF VARCHAR_TABLE_TYPE('B6512DF0','5838FABC','EC5D804C','074DD65C');
But I don't know the limit for initializing a nested table. Oracle documentation says: "Because a nested table does not have a declared size, you can put as many elements in the constructor as necessary."
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL Server: Is it possible to insert into two tables at the same time?
Select from one table and insert to another two table
I have two tables with one-to-one relationship
according to the result of a select statement I need to insert the resultant values in those tables
I really need to avoid cursors, do you know how I could insert in both tables with one select statement , or do you know some other solution that is better in performance, the table I'm querying is expected to get really huge and looping through it would be bad
thanks in advance