Update table with values of another table - oracle

I'm trying to update a table via subquery but i get following error:
ORA-01427: single-row subquery returns more than one row
01427. 00000 - "single-row subquery returns more than one row"
I would like to insert the content of a third table in the column.
My query:
UPDATE GH212_TABLE1
SET GH212_LINK = (SELECT GH201_TABLE1.GH201_ATTRIBUTEX
FROM GH210_TABLE3
LEFT JOIN GH201_TABLE1
ON GH210_TABLE3.GH210_GH201_ID = GH201_TABLE1.GH201_ID
INNER JOIN GH212_TABLE1
ON GH212_TABLE1.GH212_GH210_ID = GH210_ID
WHERE GH212_TABLE1.GH212_GH210_ID=GH210_TABLE3.GH210_ID
GROUP BY GH201_ATTRIBUTEX)
WHERE GH212_TABLE1.GH212_ATTRIBUTEY='11';
I'm not sure how to link the tables right, so that i get the attribute from one specific object.
Any help would be much appreciated.
Cheers,
Fabi
EDIT: Thank you for your reply! I was well of the Problem with the multiple rows, but i somehow couldn't resolve it. Even when i tried the distinct query. But the solution from William Robertson seems to work, thanks a lot!!

A bit of a guess as I don't have your data or business logic, but perhaps you wanted something more like the following. I've removed the second instance of gh212_table1 from the subquery and instead linked t3 back to the table being updated:
update gh212_table1 t
set gh212_link =
( select distinct t1.gh201_attributex
from gh210_table3 t3
left join gh201_table1 t1 on t1.gh201_id = t3.gh210_gh201_id
where t3.gh210_id = t.gh212_gh210_id )
where gh212_table1.gh212_attributey = '11';
There could still be a problem with this, though, if there are rows in gh212_table1 for which the subquery finds no row, as for those rows gh212_link will be set to null. If that is a problem, you might try rewriting it as a merge:
merge into gh212_table1 tgt
using ( select distinct t3.gh210_id, t1.gh201_attributex
from gh210_table3 t3
left join gh201_table1 t1 on t1.gh201_id = t3.gh210_gh201_id ) src
on ( src.gh210_id = tgt.gh212_gh210_id )
when matched then update
set tgt.gh212_link = src.gh201_attributex;
As you are fetching gh201_attributex from an outer join, it will be null whenever there is no row in gh201_table1 for a gh210_table3 row. Is that what you want? In the merge version, if you make it an inner join then the merge will only be applied where a row exists.

SELECT GH201_TABLE1.GH201_ATTRIBUTEX
FROM GH210_TABLE3
LEFT JOIN GH201_TABLE1
ON GH210_TABLE3.GH210_GH201_ID = GH201_TABLE1.GH201_ID
INNER JOIN GH212_TABLE1
ON GH212_TABLE1.GH212_GH210_ID = GH210_ID
WHERE GH212_TABLE1.GH212_GH210_ID=GH210_TABLE3.GH210_ID
GROUP BY GH201_ATTRIBUTEX
This query may give you multiple row values, due to this you are getting this exception
ORA-01427: single-row subquery returns more than one row 01427. 00000 - "single-row subquery returns more than one row"
Ensure that your sub-query return single row value.

Related

Update statement with joins in Oracle

I need to update one column in table A with the result of a multiplication of one field from table A with one field from table B.
It would be pretty simple to do this in T-SQL, but I can't write the correct syntax in Oracle.
What I've tried:
UPDATE TABLE_A
SET TABLE_A.COLUMN_TO_UPDATE =
(select TABLE_A.COLUMN_WITH_SOME_VALUE * TABLE_B.COLUMN_WITH_PERCENTAGE
from TABLE_A
INNER JOIN TABLE_B
ON TABLE_A.PRODUCT_ID = TABLE_B.PRODUCT_ID
AND TABLE_A.SALES_CHANNEL_ID = TABLE_B.SALES_CHANNEL_ID)
WHERE TABLE_A.MONTH_ID IN (201601, 201602, 201603);
But I keep getting errors. Could anybody help me, please?
I generally prefer to use the below format for such cases since this will ensure there's no update performed if there's no data in the table(query extracted temp table) whereas in the above solution provided by Brian Leach will update the new value as null if there's no record present in the 2nd table but exists in the first table.
UPDATE
(
select TABLE_A.COLUMN_TO_UPDATE
, TABLE_A.PRODUCT_ID
, TABLE_A.COLUMN_WITH_SOME_VALUE * TABLE_B.COLUMN_WITH_PERCENTAGE as value
from TABLE_A
INNER JOIN TABLE_B
ON TABLE_A.PRODUCT_ID = TABLE_B.PRODUCT_ID
AND TABLE_A.SALES_CHANNEL_ID = TABLE_B.SALES_CHANNEL_ID
AND TABLE_A.MONTH_ID IN (201601, 201602, 201603)
) DATA
SET DATA.COLUMN_TO_UPDATE = DATA.value;
This solution can cause key preserved value issues which shouldn't be an issue here since i expect a single row in both the tables for one product(ID).
More on Key Preserved table concept in inner join can be found here
https://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:548422757486
#Jayesh Mulwani raiesed a valid point, this will set the value to null if there is no matching record. This may or may not be the desired result. If it isn't, and no change is desirect, you can change the select statement to:
coalesce((SELECT table_b.column_with_percentage
FROM table_b
WHERE table_a.product_id = table_b.product_id AND table_a.sales_channel_id = table_b.sales_channel_id),1)
If this is the desired outcome, Jayesh's solution will be more efficient as it will only update matching records.
UPDATE table_a
SET table_a.column_to_update = table_a.column_with_some_value
* (SELECT table_b.column_with_percentage
FROM table_b
WHERE table_a.product_id = table_b.product_id
AND table_a.sales_channel_id = table_b.sales_channel_id)
WHERE table_a.month_id IN (201601, 201602, 201603);

ORACLE - Update Multiple Columns with Values from Nested Join

I'm practically new in using oracle and I bumped into a blocker. Below is the query that I created based on what I have researched online to update multiple columns of a table with values from a nested join statement.
UPDATE
(
SELECT
A.COLUMN1 OLD_COLUMN1,
BC.COLUMN1 NEW_COLUMN1,
A.BALANCE OLD_COLUMN2,
BC.COLUMN2_MIN NEW_COLUMN2,
A.COLUMN3 OLD_COLUMN3,
BC.COLUMN3 NEW_COLUMN3
FROM TABLE_A A
INNER JOIN
(
SELECT B.TWWID,
B.ITEMDATE,
B.COLUMN2_MIN,
C.COLUMN3,
C.COUNTRYID,
C.COLUMN1
FROM TABLE_B B
LEFT OUTER JOIN TABLE_C C
ON TO_CHAR(B.ID) = TO_CHAR(C.ID)
) BC
ON A.ID = BC.ID
AND A.DATE = BC.DATE
)ABCUPDATE
SET ABCUPDATE.OLD_COLUMN1 = ABCUPDATE.NEW_COLUMN1,
ABCUPDATE.OLD_COLUMN2 = ABCUPDATE.NEW_COLUMN2,
ABCUPDATE.OLD_COLUMN3 = ABCUPDATE.NEW_COLUMN3;
Selecting the sub-query returns the expected results but when I run the update script as a whole an error is returned.
ORA-01779: cannot modify a column which maps to a non key-preserved
table
Can anyone please explain why I encounter this error and what adjustments can I do to the script to make it work?
Thanks in advance!

Oracle Update query with joins

I do know update clause doesn't work with joins in Oracle.
update table1 Pr
set code = (select t2.class_attr_value from table2 t2
where class_attr_name = 'sample' and Pr.epcclass_id = t2.epcclass_id)
I would be thankful if someone can help me modify my query so that I don't get the error of SQL Command not ended properly.
Your Query seems okay to me I just added Table Alias. Your query will update all records in table1. What error you are getting...??
Suggestions,
a) Unless it's the intent that you want to update all records, add a where clause in the query to avoid updating all records...
b) If you are getting (ORA-01427: single-row subquery returns more than one row) then means corelated sub query (within brackets) is missing some condition to make it fetch only 1 row per epcclass_id.
update table1 Pr
set Pr.code = (select t2.class_attr_value
from table2 t2
where t2.class_attr_name = 'sample'
and t2.epclass_id = Pr.epcclass_id
);
Try this:
UPDATE table1 Pr INNER JOIN table2 t2
ON t2.class_attr_name = 'sample' AND Pr.epcclass_id = t2.epcclass_id
SET Pr.code = t2.class_attr_value

Oracle - single-row subquery returns more than one row

I have a query which I'm trying to get it working on oracle but get the following error
SQL Error: ORA-01427: single-row subquery returns more than one row
The query is:
UPDATE TARGET A
SET A.COL1=(SELECT B.COL1 FROM SOURCE B)
WHERE A.COL2=(SELECT B.COL2 FROM SOURCE B)
Basically, I am trying to update only those records which match in the join.
I'd be extremely grateful for any help.
Your UPDATE statement needs to be a correlated update-- you need some key in A that tells you which row in B to go to in order to retrieve the new COL1 value. Your WHERE clause should also be an IN or an EXISTS since it needs to return multiple rows. Assuming COL2 is the key (based on your WHERE clause), I'm guessing that you want
UPDATE target a
SET a.col1 = (SELECT b.col1
FROM source b
WHERE a.col2 = b.col2)
WHERE EXISTS( SELECT 1
FROM source b
WHERE a.col2 = b.col2 )
If that is not what you're looking for, posting some table definitions, sample data, and the expected output would be quite helpful.

PL SQL - Join 2 tables and return max from right table

Trying to retrive the MAX doc in the right table.
SELECT F43.PDDOCO,
F43.PDSFXO,
F43.PDLNID,
F43.PDAREC/100 As Received,
F431.PRAREC/100,
max(F431.PRDOC)
FROM PRODDTA.F43121 F431
LEFT OUTER JOIN PRODDTA.F4311 F43
ON
F43.PDKCOO=F431.PRKCOO
AND F43.PDDOCO=F431.PRDOCO
AND F43.PDDCTO=F431.PRDCTO
AND F43.PDSFXO=F431.PRSFXO
AND F43.PDLNID=F431.PRLNID
WHERE F431.PRDOCO = 401531
and F431.PRMATC = 2
and F43.PDLNTY = 'DC'
Group by
F43.PDDOCO,
F43.PDSFXO,
F43.PDLNID,
F43.PDAREC,
F431.PRAREC/100
This query is still returning the two rows in the right table. Fairly new to SQL and struggling with the statement. Any help would be appreciated.
Without seeing your data it is difficult to tell where the problem might so I will offer a few suggestions that could help.
First, you are joining with a LEFT JOIN on the PRODDTA.F4311 but you have in the WHERE clause a filter for that table. You should move the F43.PDLNTY = 'DC' to the JOIN condition. This is causing the query to act like an INNER JOIN.
Second, you can try using a subquery to get the MAX(PRDOC) value. Then you can limit the columns that you are grouping on which could eliminate the duplicates. The query would them be similar to the following:
SELECT F43.PDDOCO,
F43.PDSFXO,
F43.PDLNID,
F43.PDAREC/100 As Received,
F431.PRAREC/100,
F431.PRDOC
FROM PRODDTA.F43121 F431
INNER JOIN
(
-- subquery to get the max
-- then group by the distinct columns
SELECT PDKCOO, max(PRDOC) MaxPRDOC
FROM PRODDTA.F43121
WHERE PRDOCO = 401531
and PRMATC = 2
GROUP BY PDKCOO
) f2
-- join the subquery result back to the PRODDTA.F43121 table
on F431.PRDOC = f2.MaxPRDOC
AND F431.PDKCOO = f2.PDKCOO
LEFT OUTER JOIN PRODDTA.F4311 F43
ON F43.PDKCOO=F431.PRKCOO
AND F43.PDDOCO=F431.PRDOCO
AND F43.PDDCTO=F431.PRDCTO
AND F43.PDSFXO=F431.PRSFXO
AND F43.PDLNID=F431.PRLNID
AND F43.PDLNTY = 'DC' -- move this filter to the join instead of the WHERE
WHERE F431.PRDOCO = 401531
and F431.PRMATC = 2
If you provide your table structures and some sample data, it will be easier to determine the issue.

Resources