sql update command with skip primary key duplicates - sql-update

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

Related

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.

Django ORM make Union Query with column not in common in both tables, set value of not in common column as null

Hi i want to make a query in Djang ORM
like this
Select Col1, Col2, Col3, Col4, Col5 from Table1
Union
Select Col1, Col2, Col3, Null as Col4, Null as Col5 from Table2
as you see Col4, Col5 are not in common but they will return null instead in Table2.
Table1_qs = Table1.objects.all()
Table2_qs = Table2.objects.all()
Table1_qs.values('Col1', 'Col2','Col3','Col4','Col5').union(Table2_qs.values('Col1', 'Col2','Col3','Null as Col4','Null as Col5'))
How can i make the query in Django?
the solution is made possible by Value and annotate.
here is how.
let say Col4 is type IntegerField,
and Col5 is type CharField
from django.db.models import Value, IntegerField, CharField
Table1_qs = Table1.objects.all()
Table2_qs = Table2.objects.all()
Table1_qs = Table1_qs.values('Col1', 'Col2','Col3','Col4','Col5')
Table2_qs = Table2_qs.values('Col1', 'Col2','Col3').annotate(
Col4=Value(None, output_field=IntegerField()),
Col5=Value(None, output_field=CharField()) )
unioned_query = Table1_qs.union(Table2_qs)
please note:
1: columns type must be the same as each.
2: and they must be in same order as well.
the problem that arise is within foreign-key. as only the id (primary key) of them will be returned when using Values() on a query-set!
I hope Django add a way to get them as usual objects too.

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

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;
;

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

IF + AND / OR logic inside of a query

below is an example document I have shared:
https://docs.google.com/spreadsheets/d/1WuQIqn8DA12R0mNFGMdjJahQ0eNoxKODpSwopk7KoYU/edit#gid=0
My data is simple table:
I want to do the following:
For starting cell K7 on patient tab
I want to query the call log tab for
two main conditions.
Query select loqic: return rows D,E,F,A when certain conditions are met:
if text colC equals text in patient tab cell c7 AND col D says "No beds Available" And colI shows time left to calling greater than 0
OR If not than:
if col B=cell H3 in patient tab, and Col C= Cell C7 in patient tab
Thank you for your help
My example could help you.
Suppose you have a small data, like this, columns A:D:
Then you may use query state with two or more OR conditions, but insert them into parentheses. Sample formula:
=QUERY({A:D},"select Col1, Col2, Col3, Col4 where (Col1 < 7 and Col3 = 'c') or (Col2 = 'a' and Col4 > 0)")
To use Col1, Col2, Col3... notation inside query, data must be inside {}