How to update multiple columns in same update statement with one column depends upon another new column new value in Redshift - sql-update

I want to update multiple columns in same update statement with one column depends upon another new column new value.
Example:
Sample Data: col1 and col2 is the column names and test_update is the table name.
SELECT * FROM test_update;
col1 col2
col-1 col-2
col-1 col-2
col-1 col-2
update test_update set col1 = 'new', col2=col1||'-new';
SELECT * FROM test_update;
col1 col2
new col-1-new
new col-1-new
new col-1-new
What I need to achieve is col2 is updated as new-new as we updated value of col1 is new.
I think may be its not possible in one SQL statement. If possible How can we do that, If its not What is best way of handling this problem in Data Warehouse environment, like execute multiple update 1st on col1 and then on col2 or any other.
Hoping my question is clear.

You cannot update the second column based on the result of updating the first column. However this can be achieved in a single by "pre-calculating" the result you want and then updating based on that.
The following update using a join is based on the example provided in the Redshift documentation:
UPDATE test_update
SET col1 = precalc.col1
, col2 = precalc.col2
FROM (
SELECT catid
, 'new' AS col1
, col1 || '-new' AS col2
FROM test_update
) precalc
WHERE test_update.id = precalc.id;
;

Related

Update multiple rows with differing values against a subset of a table

We are trying to update around 3000 rows in a table of roughly 300,000+ entries, with each update value being unique. The problem is it takes around an hour, and we want to see how we can efficiently optimize the query. Is there any way to create a temporary subset of the table that still references the original table?
Currently, we are doing this (x3000):
UPDATE table SET col1 = newVal1 WHERE col1 = oldVal1
UPDATE table SET col1 = newVal2 WHERE col1 = oldVal2
UPDATE table SET col1 = newVal3 WHERE col1 = oldVal3
.... etc (x3000)
There is probably an issue with trying to update a col that we are also searching whereby, causing more tax on the query, but this is a necessity as it's the only way to track the exact row item.
Using Mysql Workbench and InnoDB Engine
We are debating whether indexing col1 would be effective, since we are also writing over col1.
We have also tried to use a View of just the rows we want to update, but there is no difference in how long it takes.
Any suggestions?

How to add column with query folding using snowflake connector

I am trying to add a new column to a power query result that is the result of subtracting one column from another. according to the power bi documentation basic arithmetic is supported with query folding but for some reason it is showing a failure to query fold. I also tried simply adding a column populated with the number 1 and it still was not working. Is there some trick to getting query folding a new column to work on snowflake?
If the computation is made based only on data from source, then it could be computed during table import as SQL Statement:
SELECT col1, col2, col1 + col2 AS computed_total
FROM my_table_name
EDIT:
The problem with this solution is that native SQL statement for snowflake is only supported on PBI desktop and I want to have this stored in a dataflow (so pbi web client) for reusability and other reasons.
Option 1:
Create a view istead of table at source:
CREATE OR REPLACE VIEW my_view
AS
SELECT col1, col2, col1 + col2 AS computed_total
FROM my_table_name;
Option 2:
Add computed column to the table:
ALTER TABLE my_table_name
ADD COLUMN computed_total NUMBER(38,4) AS (col1 + col2);

Count and filter on the basis of third column in informatica

Like I have a question
Col1 Col2 Col3
45321_320 A Y
45321_320 A N
76453-10 A Y
45638_80 A Y
So we need to count the no of rows that have same col1 for example the first two rows should be considered as count=2 and rest as count=1 and after that count=2 or more that records need to filtered out on the basis of Col3=Y, so how we can do that in informatica
https://i.stack.imgur.com/JkxnG.png
This is little tricky. Pls follow below steps.
Sort the data base on col1.
Use agg to aggregate. Create a new col called count_col1.
create another col, cnt_col3_y = count(*, col3=y)
Join agg output with sorter output based on col1.
Put a filter. Logic should be
iif( count_col1>1 and cnt_col3>0, false, true)
Link output of filter to target.
This will generate output like below.
Col1 Col2 Col3
76453-10 A Y
45638_80 A Y
If you want different output let me know.

Exasol Update Table using subselect

I got this statement, which works in Oracle:
update table a set
a.attribute =
(select
round(sum(r.attribute1),4)
from table2 p, table3 r
where 1 = 1
and some joins
)
where 1 = 1
and a.attribute3 > 10
;
Now I would like to do the same statement in Exasol DB. But I got error [Code: 0, SQL State: 0A000] Feature not supported: this kind of correlated subselect (Session: 1665921074538906818)
After some research, I found out you need to write the query in following syntax:
UPDATE table a
set a.attribute = r.attribute2
FROM table a, table2 p, table3 r
where 1 = 1
and some joins
and a.attribute3 > 10;
The problem is I can't take sum of r.attribute2. So I get unstable set of rows. Is there any way to do the first query in Exasol DB?
Thanks for help guys!
Following SQL UPDATE statement will work for cases if JOIN between table1 and table2 are 1-to-1 (or if there is a 1-to-1 relation between target table and resultset of JOINs)
In this case target table val column is updated otherwise an error is returned
UPDATE table1 AS a
SET a.val = table2.val
FROM table1, table2
WHERE table1.id = table2.id;
On the other hand, if the join is causing multiple returns for single table1 rows, then the unstable error raised.
If you want to sum the column values of the multiplying rows, maybe following approach can help
First sum all rows of table2 in bases of table1 and use this sub-select as a new temp table, then use this in UPDATE FROM statement
UPDATE table1 AS a
SET a.val = table2.val
FROM table1
INNER JOIN (
select id, sum(val) val from table2 group by id
) table2
ON table1.id = table2.id;
I tried to solve the issue using two tables
In your case probably you will use table2 and table3 in the subselect statement
I hope this is the answer you were looking for

sql update command with skip primary key duplicates

I have two tables T1(col1,col2,col3) and T2(col4,col5,col6)
Only for T1 , col1 is primary key.
I need to update col1=col4, col2 = col5, col3=col6 where col1=col4 or col1=col5
There are chances that primary key is getting duplicated... and update command does not work
basically update primary key with out duplicates in it.
You do not need to update col1 as you are updating the row with same primary key.
col1 is primary key of T1, so it won't be duplicated.
The query should be UPDATE T1 SET col2=col5, col3=col6 WHERE col1=col4
in case of col1 != col4 and col1 = col5 please execute the query SELECT * FROM T1 WHERE col1 = col4
if the number of rows in result array > 0 then skip