I want to duplicate a row record in same table but with modifications on some column values. I know i can duplicate a row record by using the following script.
INSERT INTO table_name(
column_name1, column_name2, column_name3 ....
)
SELECT column_name1, column_name2, column_name3 ....
FROM table_name WHERE id=1;
But it will duplicate the whole row. For modification i further need to add update script.
So my question is, is there any simpler way to handle my scenario. Since the table in which i am working have around 40 columns, so i think this way is not feasible.
Any new ideas are most welcome.
Thanks in advance.
To select duplicate values, you need to create groups of rows with the same values and then select the groups with counts greater than one. You can achieve that by using GROUP BY and a HAVING clause.
On the pgAdmin 4 GUI Tool page, it seems the only proposed way to copy is to click on this button : Click the Copy icon to copy the currently selected row.
Use the context menu on the same table, to get another script: "Script Table as | SELECT To | New Query Window". This will be a totally standard select list, with all your fields listed out. Copy the whole query and paste it in over the VALUES clause in your first query window. This will give you a complete INSERT ...
Getting duplicate rows in postgresql table can be accomplished by using multiple methods each is explained with an example. view source print? We have chosen duplicate row by counting the number of rows for each studentid and chosen the rows having count > 1.
The first two rows are duplicates (except for the DogId column, which is the table’s primary key, and contains a unique value across all rows). The last three rows are also duplicates (except for the DogId column).
The first two rows are duplicates, and the last three rows are duplicates. That’s because all three columns contain the same values in each duplicate row. We can use the following query to see how many rows are duplicates: SELECT PetId, PetName, PetType, COUNT (*) AS "Count" FROM Pets GROUP BY PetId, PetName, PetType ORDER BY PetId;
For almost identical rows (identical except for one or more properties): Select one of the rows according to some criteria and delete the remaining ones. That is what my article is about. 1) How to find duplicates? Imagine you have a table containing some data on employees of a company.
Change values of columns directly in SELECT, the aditional update is needless, just like in the below example
INSERT INTO table_name(
column_name1, column_name2, column_name3 ....
)
SELECT column_name1,
'New string value of column 2',
column_name3,
......
......
1234 as new_val_of_col_25,
column_name26,
......
FROM table_name WHERE id=1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With