sql oracle update - sql

I have this SQL...
UPDATE table1 t1
SET (t1.wert) =
(select t2.bezeichnung from table2 t2
where t1.id = t2.cpbezeichnung)
where t1.id = t2.cpbezeichnung
... which I cant run because it says me that it doesnt know t2.cpbezeichnung in line #5.
How can I fix it?

Table with alias t2 is not defined for UPDATE query, so it's clearly not known at line 5. Table t2 is defined only inside subquery on lines 3 and 4.
What exactly are you trying to achieve with condition on line 5?
If do you want prevent setting NULL into t1.wert for rows where there is no appropriate record in table t2, then you need to replace condition on line 5
UPDATE table1 t1
SET (t1.wert) =
(select t2.bezeichnung from table2 t2 where t1.id = t2.cpbezeichnung)
where t1.id IN (SELECT t2.cpbezeichnung from table2)
This will set values in t1.wert only for records where t1.id exists in t2.cpbezeichnung.

The t2 alias (along with table2) is only visible in the subquery. My guess is that you want
UPDATE table1 t1
SET t1.wert = (select t2.bezeichnung
from table2 t2
where t1.id = t2.cpbezeichnung)
where exists (select 1
from table2 t2
where t1.id = t2.cpbezeichnung)
which updates every row where there is a match between the two tables. If that's not what you want, posting a test case would be helpful.

When using correlated subqueries you cannot use an alias from the inner query in the outer query.
The outer query knows nothing about the inner query except its results.
Ref

You will have to use a Inner join on the two tables.
Something like
UPDATE table1 t1 INNERJOIN table t2 ON your_table_condition SET t1.wert = (select t2.bezeichnung from t2 where t1.id = t2.cpbezeichnung) where t1.id = t2.cpbezeichnung

Related

How to fetch * records from a table which are not present in other two tables

I want to fetch all records with all columns of a table, Records which are not in the other 2 tables. Please help.
I have tried below query, it is working fine for comparing one column. But I want to compare 5 columns.
select * from A
WHERE NOT EXISTS
(select * from B b where b.id=a.id) AND
NOT EXISTS
(select * from C c where c.id=a.id)
A general solution might look like:
SELECT t1.*
FROM table1 t1
WHERE NOT EXISTS (SELECT 1 FROM table2 t2 WHERE t2.id = t1.t2_id) AND
NOT EXISTS (SELECT 1 FROM table3 t3 WHERE t3.id = t1.t3_id);
This assumes that you want to target table1 for records, ensuring that no matches can found in table2 and table3.
I prefer this approach:
SELECT t1.*
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t1.id = t2.t1_id
LEFT JOIN table3 AS t3
ON t1.id = t3.t1_id
WHERE t2.id IS NULL
AND t3.id IS NULL;
While this might be a bit less intuitive than using sub queries I think odds of making mistakes with aliases are less likely as in the example below, which happens more often than you might expect:
SELECT *
FROM table1
WHERE NOT EXISTS (SELECT 1 FROM table2 WHERE id = id) AND
NOT EXISTS (SELECT 1 FROM table3 WHERE id = id);
To your question about checks on 5 columns, that can still be done using either of these methods by adding conditions either in the left joins or in the where clause of each sub query.

postgres select values from a table with an extra dependency

So, I would like the next query for postgres:
SELECT name
FROM Table1 as T1
WHERE T1.id = (
SELECT id
FROM Table2 AS T2
WHERE T2.active=true)
So, I need to get all the values from the first table, whose id matches the ones set as active in another table.
You don't need a sub query for this. Use a join. It will be a lot more efficient.
SELECT T1.name
FROM Table1 as T1
INNER JOIN Table2 as T2 ON T2.id = T1.id AND T2.active=true
The equality operator imposes that the subquery should return a single record. You want IN instead, which accepts a resultset:
SELECT T1.name
FROM Table1 as T1
WHERE T1.id IN (SELECT id FROM Table2 AS T2 WHERE T2.active)
This can also be expressed with EXISTS:
SELECT T1.name
FROM Table1 as T1
WHERE EXISTS (SELECT 1 FROM Table2 AS T2 WHERE T2.id = T1.id AND T2.active)
Note that in Postgres condition T2.active = true can be shortened T2.active.
For performance, you want an index on Table2(id, active) and another on Table1(id).

One update SQL query from three tables

I have three tables and I have to write one query to update table 1 row from table 3 and the only matching columns I have is in table 2.
Table 1 which has incorrect data:
Table 3 has the correct data:
I did try to write a query and execute it but it gives me an error saying there are too many rows too select which is true I do have many rows to correct but it still wouldn't correct. What do you think I should do. This is my query so far.
UPDATE Table1
SET Table1.Number = (SELECT Table3.Number
FROM Table2
FULL OUTER JOIN Table1 ON Table1.ID = Table2.ID
FULL OUTER JOIN Table3 ON Table3.Signin = Table2.Signin
WHERE (Table2.ID = Table1.ID)
AND (Table1.Number = 'xxx'))
WHERE (Tale1.Number = 'xxx')
In Where clause of JOIN query need to modify as multiple records are generating by inappropriate condition.Try to use Table3 components instead of using Table1 in joining query where clause.
UPDATE Table1
SET Table1.NUMBER = (SELECT table3.NUMBER FROM Table1 FULL OUTER JOIN Table2
ON Table1.ID = Table2.ID
FULL OUTER JOIN Table3
ON Table2.SIGNIN = Table3.SIGNIN
WHERE Table3.SIGNIN = 100) // This is the point where you need to modify your code
WHERE Table1.ID = 1;
ONLINE DEMO HERE
It actually worked after I removed this line from my query.
FULL OUTER JOIN Table1 ON table1.ID = Table2.ID
Thanks for the help.
You are fairly close. When doing the update though unless you are wanting to clear value for t1.number when a record is not matched in t3, you will want to use INNER JOIN. FULL OUTER JOIN would mean you are trying to update rows in t1 that don't exist but a LEFT JOIN you would update t1.number to NULL if a record in t3 doesn't exist.
UPDATE t1
SET t1.Number = t3.Number
FROM
Table1 t1
INNER JOIN Table2 t2
ON t1.Id = t2.Id
INNER JOIN Table3 t3
ON t2.Signin = t.3.Signin
WHERE
t1.number <> t3.number
--Or if you have nulls something like
--ISNULL(t1.number,'xxx') <> ISNULL(t3.number,'xxx')
-- if you only want to update when t1.number = 'xxx' then
--t1.number = 'xxx'
t1,t2,t3 are table aliases that I created by adding the alias after table name. By using join syntax rather than a sub select you simplify your were conditions. In sql-sever if more than 1 record in t2 & t3 match it will select one row randomly in the case of a one to many relationship. If you want a specific record when not one to one relation you can use window functions and common table expressions (cte) to limit t3 to the exact record you want to use.

Oracle SQL correlated update low performance

I have to update table(table3) with data from two other tables.
These two tables (table1,table2) are both describing single object, therefore table1.id equals table2.id for that single object.
Table 3 is a many-to-many mapping table for that object and another one which is not important here. Into that mapping table I need to put some data from table1 and table2.
I am trying to update table3 with correlated query, but it takes a very long time to complete. I ran this query on group of ~500.000 records and it runs for over an hour now.
Am I doing something wrong here?
UPDATE table3 t3
SET (t3.some_value1, t3.other_value1, t3.some_value2, t3.other_value2) =
( SELECT t1.some_value1, t1.other_value1, t2.some_value2, t2.other_value2
FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id
WHERE t3.fk = t1.id)
WHERE EXISTS (SELECT 1 FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id WHERE t3.fk = t1.id);
SELECT * FROM table3;
SQL Fiddle
Use this:
UPDATE
(SELECT
T3.SOME_VALUE1 TARGET1,
T3.OTHER_VALUE1 TARGET2,
T3.SOME_VALUE2 TARGET3,
T3.OTHER_VALUE2 TARGET4,
T1.SOME_VALUE1 SOURCE1,
T1.OTHER_VALUE1 SOURCE2,
T2.SOME_VALUE2 SOURCE3,
T2.OTHER_VALUE2 SOURCE4
FROM
TABLE1 T1
JOIN TABLE2 T2
ON T1.ID = T2.ID
JOIN T3
ON T3.FK = T1.ID)
SET
TARGET1 = SOURCE1,
TARGET2 = SOURCE2,
TARGET3 = SOURCE3,
TARGET4 = SOURCE4;
If you face ORA-01779 cannot modify a column which maps to a non key-preserved table then try using UPDATE /*+ BYPASS_UJCV */ or add a unique index on T1.ID, T2.ID, T3.FK
The answer provided by #SriniV uses a technique called an Updatable Join View. In later versions of Oracle, this can be replaced with a MERGE:
MERGE INTO t3
USING (
SELECT t1.id
t1.some_value1,
t1.other_value1,
t2.some_value2,
t2.other_value2
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.id
WHERE 1=1
AND t3.fk = t1.id
) a ON (
t3.fk = a.id
)
WHEN MATCHED THEN UPDATE
SET t3.some_value = a.some_value,
t3.other_value1 = a.other_value1
t3.some_value2 = a.some_value2
t3.other_value2 = a.other_value2

SQL Query: select all the records from table1 which exist in table2 + all the records from table2 which don't exist in table1

consider the following example.
I have to select all the records from table1 which exist in table2 also + all the records from table2 which don't exist in table1 but exist in table2 and have IsActive=1 and status not null.
I initially tried it with a join but how to do the later part where I have to select the records which don't exist in table 1 ? I have to do it inside a single query presumably with a SQL view.
Edit
I need to combine the results like a UNION of 2 tables, so incase of rows absent in table1 but present in table2, the columns of belonging to table1 would be blank.
Here's an example query:
select *
from Table2 t2
left join
Table1 t1
on t1.id = t2.id
where t1.id is not null
or (isActive = 1 and status is not null)
The first line of the where clause takes care of "all the records from table1 which exist in table2". The second line is for "don't exist in table1 but exist in table2 and have IsActive=1 and status not null".
You will need an outer join here.
http://msdn.microsoft.com/en-us/library/ms187518.aspx
Is this it? Not sure if I got right what you want to do.
SELECT
*
FROM
Table1 t1
JOIN
Table2 t2 ON (t1.ID = t2.ID OR (t1.ID IS NULL AND t2.isActive = 1 AND t2.Status IS NOT NULL))