How to Copy Data from One Column to Another in the Same Table

How can I copy data from one column to another in the same table?

How about this

UPDATE table SET columnB = columnA;

This will update every row.

SQL - Copy data from one column to another in the same table

What you want is much simpler:

UPDATE "Invoice" 
SET "example2" = "example1";

Note: I would strongly encourage you to remove the double quotes! Don't escape column names -- it just makes it harder to write and to read the code.

Copy data from one column to other column (which is in a different table)

In SQL Server 2008 you can use a multi-table update as follows:

UPDATE tblindiantime 
SET tblindiantime.CountryName = contacts.BusinessCountry
FROM tblindiantime
JOIN contacts
ON -- join condition here

You need a join condition to specify which row should be updated.

If the target table is currently empty then you should use an INSERT instead:

INSERT INTO tblindiantime (CountryName)
SELECT BusinessCountry FROM contacts

Copy values from one column to another in the same table

Short answer for the code in question is:

UPDATE `table` SET test=number

Here table is the table name and it's surrounded by grave accent (aka back-ticks `) as this is MySQL convention to escape keywords (and TABLE is a keyword in that case).



BEWARE!

This is pretty dangerous query which will wipe everything in column test in every row of your table replacing it by the number (regardless of it's value)

It is more common to use WHERE clause to limit your query to only specific set of rows:

UPDATE `products` SET `in_stock` = true WHERE `supplier_id` = 10

Copy a column to another column within a same table in oracle db. Do I need to specify which data goes where?

The operation will be done on the same row for each rows.

Be aware that if you do not want to update all the table rows then you need to add a WHERE clause.

How to copy the combined value of two column to one Column in same table

Use CONCAT function of mysql:

UPDATE table_name SET column1 = CONCAT(column2, ' ', column3);

Copy Data from one column to another column in a different table using MySQL

The fix was simple,

Just wrap the query inside:

SET AUTOCOMMIT = 0;
SET FOREIGN_KEY_CHECKS = 0;
SET UNIQUE_CHECKS = 0;

# YOUR QUERIES HERE...

SET AUTOCOMMIT = 1;
SET FOREIGN_KEY_CHECKS = 1;
SET UNIQUE_CHECKS = 1;

The the data goes in, only difficulty is the large size of the dataset but breaking it into chunks helped alot.

How to copy the previous row value of the same column?

This simpler query should do it:

SELECT specimen, day, period, area 
, count(*) FILTER (WHERE step)
OVER (PARTITION BY specimen, day ORDER BY period) AS mov
FROM (
SELECT *
, lag(area) OVER (PARTITION BY specimen, day ORDER BY period) <> area AS step
FROM tbl
) sub
ORDER BY specimen, day; -- optional

If period is an incremented number without gaps for every (specimen, day), this is equivalent:

SELECT t1.*
, count(*) FILTER (WHERE t1.area <> t2.area)
OVER (PARTITION BY t1.specimen, t1.day ORDER BY t1.period) AS mov
FROM tbl t1
LEFT JOIN tbl t2 ON t2.specimen = t1.specimen
AND t2.day = t1.day
AND t2.period = t1.period - 1
;

db<>fiddle here

Not sure which is faster. An index on (specimen, day, period) would help (a lot) either way.

See:

  • Form groups of consecutive rows with same value
  • Select longest continuous sequence

About the aggregate FILTER clause:

  • Aggregate columns with additional (distinct) filters

Should be fastest:

  • For absolute performance, is SUM faster or COUNT?


Related Topics



Leave a reply



Submit