UPDATE
(
SELECT
a.COL1
FROM
TABLE1 a,
TABLE2 b,
TABLE3 c
WHERE
a.name = b.name
c.ccol = b.ccol AND
AND b.col1 = 'anyvalue'
AND a.col2 = 'anothervalue'
) u
SET
u.COL1 = 'VALUE'
This query does not work, since TABLE1 does not contains PK. How to write such a query ?
The following should achieve what it looks like you are trying to achieve above:
UPDATE TABLE1
SET COL1 = 'VALUE'
WHERE EXISTS
( SELECT 1
FROM TABLE2 B
INNER JOIN TABLE3 C
ON B.Ccol = C.Ccol
WHERE b.Name = Table1.Name
AND b.Col1 = 'AnyValue'
AND c.Col1 = 'AnotherValue'
)
I've never worked in oracle, but try something like this. Not having a pk shouldn't be an issue, as long as your joins & where statements are correct.
This is the SQL equivalent of what you're asking for
UPDATE u
SET u.COL1 = 'VALUE'
FROM Table1 AS u
INNER JOIN Table2 AS b ON u.name = b.name
INNER JOIN Table3 AS c ON c.ccol = b.ccol
WHERE b.col1 = 'anyvalue' AND u.col2 = 'anothervalue'
Related
Currently SQL statement as below:
SELECT result FROM tableA WHERE
field1 = (SELECT x FROM tableB WHERE field3 = "XXX") OR
field2 = (SELECT x FROM tableB WHERE field3 = "XXX");
Can simplify the two select statementSELECT x FROM tableB WHERE field3 = "XXX" ?
Have using JOIN and currently the statement as below:
SELECT resultB from tableA a join
tableB b on
b.field4 = a.field1 or
b.field4 = a.field2 where
b.field3 = "XXX";
Further on, "XXX" is from below SQL statement:
SELECT DISTINCT(resultA) FROM tableC c WHERE c.field5 is false;
How to combine this to having result as below:
resultA(1), resultB(1)
resultA(2), resultB(2)
resultA(3), resultB(3)
...
Thanks.
You can join the tables.
SELECT
a.result
FROM tableA a
join tableB b on b.x = a.field1 or b.x = a.field2
where b.field3 = 'XXX'
https://www.w3schools.com/sql/sql_join.asp
You can do it with EXISTS:
SELECT a.result
FROM tableA a
WHERE EXISTS (
SELECT 1
FROM tableB b
WHERE b.field3 = 'XXX' AND b.x IN (a.field1, a.field2)
)
Although you can "simplify" the logic, if you care about performance that is probably not a good route. Instead, I would suggest using exists twice:
SELECT a.result
FROM tableA a
WHERE EXISTS (SELECT 1
FROM tableB b
WHERE b.x = a.field1 AND b.field3 = 'XXX'
) OR
EXISTS (SELECT 1
FROM tableB b
WHERE b.x = a.field2 AND b.field3 = 'XXX'
);
This can make use of an index on tableB(x, field3).
I'm trying to update the value in column 'ID' from table 1 with the value in column 'ID' from table 2 - Only if they do not match. I think I have everything except the set statement down.
I'm wondering if this is the best way to go about it and how to format the sub-query for this type of problem
update table1 B
set B.id = (select A.id
from table2 A
where B.num = A.num
and B.name = A.name)
where B.num = A.num
and B.name = A.name
and B.id <> A.id
;
Maybe something like this?
update B
set B.id=A.Id
from table1 B
join table2 A
on B.num=A.num
and B.name=A.name
and B.id<>A.id
Oracle does not support join in an update. But you can use two subqueries:
update table1 B
set id = (select A.id
from table2 A
where B.num = A.num and
B.name = A.name
)
where exists (select A.id
from table2 A
where B.num = A.num and
B.name = A.name and
B.id <> A.id
);
Use the MERGE command.
Here is a basic example:
MERGE INTO table1 a
USING (SELECT bb.asdf,cc.zxcv
from table2 bb,table3 cc
where bb.field1=cc.field1) b
ON (a.asdf=b.asdf)
WHEN MATCHED THEN
UPDATE SET a.zxcv=b.zxcv;
I have SELECT statement with LEFT JOIN and joined tables are sub-queries. And Oracle could not recognize alias of the first sub-query in the second one. It works for DB2 but does not work for Oracle.
How I can implement it or rewrite my query?
SELECT *
FROM
(SELECT E.C3 AS COLUMN3
, E.C4 AS COLUMN4
FROM TBL_1 D
, TBL_2 E
WHERE D.C6 = E.C6 ) B
LEFT JOIN TABLE
(SELECT C.C1
FROM TBL_3 C
WHERE
C.C7 = 'hello'
AND B.C3 = C.C8
UNION ALL
SELECT C.C1
FROM TBL_3 C
WHERE
C.C7 = 'world'
AND B.C4 = C.C8
) A
ON 1 = 1
Oracle error message:
ORA-00904: "B"."C3": invalid identifier
You can simplify this query to the following, removing the sub-queries:
Select A.Col1, B.Col2
From tbl_AJoin A
Left Join tbl_BJoin B On A.col1 = B.col1
You have a syntax error. This:
select * from (select col1 from tbl_Ajoin) A
left join table (select col2 from tbl_Bjoin where A.col1 = tbl_Bjoin.col1) B
ON 1 = 1
should be this:
select * from (select col1 from tbl_Ajoin) A
left join (select col2 from tbl_Bjoin where A.col1 = tbl_Bjoin.col1) B
ON 1 = 1
or more specifically, this:
left join table (select
should not have the word table. It should be this:
left join (select
This is my query that does not work in Netezza:
UPDATE TABLE1 A
SET A.COL1= (SELECT DISTINCT B.COL1 FROM TABLE2 B WHERE B.ID= A.ID AND B.DeptID=104)
WHERE A.DeptID=3
How do I re-write this query?
Please help.
UPDATE TABLE1 A
SET A.COL1 = B.COL1
FROM TABLE2 B
WHERE
A.ID = B.ID AND
A.DeptID = 3 AND
B.DeptID = 104;
UPDATE
TABLE1 a,
TABLE2 b
SET
a.COL1 = 'VALUE'
WHERE
a.FK = b.PK
AND b.COL2 IN ('A subquery')
If I am using this update statement and the subquery in the IN clause does not return any rows, I get an error. How do I avoid that? (Oracle 10g)
You likely can rewrite this to an EXISTS query, depending on the exact details of your subquery:
UPDATE TABLE1 a, TABLE2 b SET a.COL1 = 'VALUE'
WHERE a.FK = b.PK AND EXISTS (select 1 from OTHERTABLE O where B.COL2=O.COL2)
UPDATE
TABLE1 a
SET
a.COL1 = 'VALUE'
WHERE
a.FK IN (SELECT b.PK FROM b WHERE b.COL2 in ....)
Is your subquery in quotes?
UPDATE
TABLE1 a,
TABLE2 b
SET
a.COL1 = value
WHERE
a.FK = b.PK
AND b.COL2 IN (
SELECT col2
FROM ...
WHERE ...
)