How do you use the results from a query to automatically update a table - sql

Basically, I have a calculated field I am creating using a select query. I want to use the results of this calculation to update a column in an existing table. How do I do that in the same query? How do I use the results of a calculated field to automatically update my table?

Use the select as the value in an update statement.
UPDATE <table> SET <column> = (SELECT ...)

Related

How to show specific value in a column - sql (redshift)

I have a situation where I want to show a value that corresponds to one field in a table but in all rows. What is better is to show you an example in a screenshot:
What I want is to have the value of 3807 in every row? How can I do that?
THank you in advance.
Try using MAX as an analytic function:
SELECT
site_id,
bt_max_speed,
MAX(bt_max_speed_on_site_coverage) OVER (PARTITION BY site_id) bt_max_speed_on_site_coverage
FROM yourTable
ORDER BY site_id;
If you simply want to update one column so it has same value for all rows you can just use UPDATE without any conditions
UPDATE <table_name> SET <column_name> = <value>
In your case it would look like this, but with replaced with actual name of your table
UPDATE <table_name> SET bt_max_speed_on_site_coverage = 3807

Update redshift column value with modified data from other column

I have a redshift table which is used for tracking, and as a result its pretty huge. I need to update one column after applying some text operations and extracting a value from another column.
The query that I have managed to write works only for one row.
UPDATE schema.table_name SET data_id = (SELECT split_part(regexp_substr(data_column,'pattern=[^&]*'),'=',2)::BIGINT FROM schema.table_name where id = 1620) WHERE id = 1620;
How do I get it to work for every row in the table.
UPDATE
schema.table_name
SET
data_id = SPLIT_PART(REGEXP_SUBSTR(data_column, 'pattern=[^&]*'),'=',2)::BIGINT;
Just don't put WHERE id = 1620; at end of update query.
Updates are not efficient in Redshift. If you have a huge table and you intend to update every single row, you should instead copy the data (with the updated column) to a new table and then flip them.

SQL update set table if value in table A is equals to value in table B

this query is working fine.
UPDATE data
SET unit_id='a3110a89'
WHERE unit_id='7d18289f';
Now, I need to run this query over 30 times
so I made csv file import it to the DB with this command:
COPY mytable FROM 'D:/test.csv' WITH CSV HEADER DELIMITER AS ','
Now I have table called my table with 2 columns OLD and NEW
i want to search the table "data" in column unit_id anywhere there if the value equals to the value in table "mytable.old" replace it with the value "mytable.new" on the same row.
I tried to run this query but I get an error:
UPDATE data
SET unit_id=(SELECT mytable."old" FROM public.mytable)
WHERE unit_id=(SELECT mytable."new" FROM public.mytable)
error:
more than one row returned by a subquery used as an expression
I think i'm just trying to do it in the wrong way...
thx for the help!
by the way Im using PostgreSQL
Your subqueries need to be correlated to the outer update:
UPDATE data
SET unit_id = (SELECT mytable."new" FROM public.mytable where data.old = mytable.old)
WHERE unit_id in (SELECT mytable."old" FROM public.mytable);
That is, set the unit_id to the "new" value, when you find the "old" value in the table.
Can you try like this,
UPDATE data A
SET A.unit_id=B.old
FROM (SELECT mytable."old",mytable."new" FROM public.mytable) B
WHERE A.unit_id=B.new
UPDATE data A
SET unit_id = B."old"
FROM public.mytable B
WHERE A.unit_id = B."new"
;
BTW: it looks like you also have old and new swapped in your question. Do you really want A's value to be set to B's old field?

select statement - update the value of one field with a fixed value

I have a select statement that returns a few fields from a table.
I want to update only the results of that select, giving a fixed value in one field.
I though of that, but it doesn't work:
UPDATE
(SELECT * from table.... where...)
SET field1=1
You didn't need a SELECT, just use a WHERE clause directly with the UPDATE to do this only for the rows that statify the condition in the WHERE clause:
UPDATE t
SET field1 = 1
FROM table AS t
WHERE ...
If your are using t-sql
UPDATE
SET field = fixed value
from tablename
where filed....

SQL SELECT or INSERT INTO query

I'm working with SQL Server 2000. I need to take the results from one column (VALIMIT) and insert them into another column (VALIMIT2012) in the same table (lending_limits).
My question is do I need to do a SELECT query first, or do I just start with an INSERT INTO query and what the proper syntax would be for the INSERT INTO query.
You can do this with an UPDATE statement:
update lending_limits
set VALIMIT2012 = VALIMIT
Neither. You don't insert columns, you insert rows, so what you want is an update:
update SomeTable
set VALIMIT2012 = VALIMIT
Note: It looks like you have one column per year, which is bad database design. If you have different data for each year, you should put that in a separate table, so that you get the year as data, not part of the column name.
UPDATE TableName SET VALIMIT2012 = VALIMIT