Oracle DELETE sql with JOIN doesn't work - sql

My delete statement returns a 933 error in Oracle, I'm not sure what is wrong-
DELETE b
from temp a
JOIN
fact_tab b
on a.col1 = b.col1
and a.col2 = b.col2
and a.col3 = b.col3;
Both tables dont have a primary key. select statement on the same thing works-
select *
from temp a
JOIN
fact_tab b
on a.col1 = b.col1
and a.col2 = b.col2
and a.col3 = b.col3;

try this
DELETE FROM
fact_tab
WHERE
EXISTS
(
SELECT
1
FROM
temp
WHERE
temp.col1 = fact_tab.col1 AND
temp.col2 = fact_tab.col2 AND
temp.col2 = fact_tab.col2
)

Oracle doesn't allow JOIN in a DELETE statement directly like that.
You could do the delete in the following way:
DELETE
FROM fact_tab
WHERE ROWID IN
(SELECT b.rowid
FROM temp a
JOIN fact_tab b
ON a.col1 = b.col1
AND A.col2 = b.col2
AND A.col3 = b.col3
);
You could also use WHERE EXISTS clause.

Related

How to combine CASE, INNER JOIN, and GROUP BY

I'm dealing with an issue; I need to join two tables, group by their ID's and use CASE statement to compare values from those 2 tables. I have been trying to use a temp table and then SELECT from it.
Purpose is to test if values in CORE correspond to values in MART.
Ideally I want to have one query, where I will see column CORE_X_MART and can use where statement on it.
Group by is essential because otherwise I have ID duplicates in the temporary table.
My code:
drop table if exists #tNDWH_4034
select a.ID, b.ID, a.col2 as MART_Value, b.col2 as CORE_Value,
case when a.col2 = b.col2 then 'Match' else 'Mismatch' end as CORE_X_MART
into #tNDWH_4034
from tab1 as a
inner join tab2 as b on a.ID = b.ID
where a.CurrentFlag = 1
group by a.ID, b.ID;
select * from #tNDWH_4034
where CORE_X_MART = 'Mismatch';
I'm using SQL server.
You don't need temp table. You can go for derived table to achieve the purpose, to have them in a single query.
SELECT * FROM
(select a.ID, b.ID, a.col2 as MART_Value, b.col2 as CORE_Value,
case when a.col2 = b.col2 then 'Match' else 'Mismatch' end as CORE_X_MART
from tab1 as a
inner join tab2 as b
on a.ID = b.ID
where a.CurrentFlag = 1
group by a.ID, b.ID) as t
WHERE t.CORE_X_MART = 'Mismatch'
maybe:
select a.ID, b.ID, a.col2 as MART_Value, b.col2 as CORE_Value,
case when a.col2 = b.col2 then 'Match' else 'Mismatch' end as CORE_X_MART
into #tNDWH_4034
from tab1 as a
inner join tab2 as b on (a.ID = b.ID)
where a.CurrentFlag = 1
group by a.ID, b.ID, a.col2, b.col2
,case when a.col2 = b.col2 then 'Match' else 'Mismatch' end --this line is probably not required
You don't need the temp table, group by, or the case either. You are looking for mismatches only so just use the not equal to operator <> to filter your results.
select distinct a.ID, a.col2 as MART_Value, b.col2 as CORE_Value
from tab1 as a
inner join tab2 as b on a.ID = b.ID
where a.CurrentFlag = 1
and a.col2 <> b.col2

Can you use an AND statement in a JOIN in SQL

Suppose you have two tables A and B and you are trying to write a JOIN query, is the following possible:
SELECT A.col1, B.col1
FROM A JOIN B on (A.col2 = B.col2 AND B.col3 = 'hello')
Will this return a table of col1 from table A and col2 from table B where there is a match in the second column across the tables and the third column of table B is 'hello'?
I.e. it will only return rows that are matching in col2 and this is further reduced to the cases where col3 in table B is 'hello'?
Yes. You can use:
Below will Join the Records in B table (Col3='hello') with A:
SELECT A.col1, B.col1
FROM A JOIN B on (A.col2 = B.col2 AND B.col3 = 'hello')
Below will Join all Records in B table with A, And performing where at Result of A and B:
SELECT A.col1, B.col1
FROM A JOIN B on A.col2 = B.col2
WHERE B.col3 = 'hello'
Both will give the same result when no other tables joined.
Yes you can.
You can specify any kind of boolean condition in the ON clause.
It is not mandatory that any column is involved in the condition so all of the following are valid:
SELECT A.col1, B.col1 FROM A JOIN B on 1=1
SELECT A.col1, B.col1 FROM A JOIN B on B.col3 = 'hello'
SELECT A.col1, B.col1 FROM A JOIN B on (A.col2 = B.col2 AND B.col3 = 'hello')
SELECT A.col1, B.col1 FROM A JOIN B on (A.col2 = B.col2 AND B.col3 = C.col3)
SELECT A.col1, B.col1 FROM A LEFT JOIN B on (C.col3 = 'bye')
But pay attention, if you limit the condition to only key fields the optimizer engine will improve the performances very much.
For an inner join, these two statements are equivalent:
SELECT A.col1, B.col1
FROM A JOIN
B
ON A.col2 = B.col2 AND B.col3 = 'hello';
and:
SELECT A.col1, B.col1
FROM A JOIN
B
ON A.col2 = B.col2
WHERE B.col3 = 'hello';
Both should have the same execution plans as well.
Some people prefer putting filtering conditions in the WHERE clause, so the query is more clear about "conditions between tables" versus "filters on the result set". I tend to agree with this sentiment, although I'm not dogmatic about it.
OUTER JOINs are different. For an outer join, it makes a big different where the conditions go. In that case, you generally do not have a choice, so you use ON or WHERE to get the logic that you want.

Left Join Table with Exclusive OR

Is there a way to join 2 tables together on one and only one of the possible conditions? Joining on condition "a" or "b" could duplicate rows, but I'm looking to only join once. I came up with a potential solution, but I'm wondering if there is a more slick way to do it.
For example:
SELECT *
FROM TableA a
LEFT JOIN TableB b
ON a.col1 = b.col1
OR (a.col1 != b.col1 AND a.col2 = b.col2)
This would join the tables on col1 OR col2 BUT NOT BOTH. Is there a cleaner way of doing this?
Not more efficient but I think more clear
SELECT *
FROM TableA a
LEFT JOIN TableB b
ON (a.col1 = b.col1 or a.col2 = b.col2)
AND NOT (a.col1 = b.col1 and a.col2 = b.col2)
Your method works. If you only want one (or a handful) of columns from b, I would suggest:
SELECT a.*, COALESCE(b.col3, b2.col3)
FROM TableA a LEFT JOIN
TableB b
ON a.col1 = b.col1 LEFT JOIN
TableB b2
ON a.col1 <> b2.col1 AND a.col2 = b2.col2;
Removing the OR from the JOIN conditions allows the optimizer to generate a better execution plan.

Oracle try second condition only if first one gave nothing in join

I'm looking in Oracle for a way to do something like if first condition has no match, do the second one. Let's get to an example.
I have this JOIN in a query :
SELECT A.COL1, A.COL2, B.COL1, B.COL2
FROM A
FULL JOIN (SELECT COL1, COL2 FROM B)
ON B.COL1 = A.COL1
OR B.COL2 = A.COL2
And you see, with this OR condition I can have two created joins for the same B object. And I don't want that. What I would like is something like :
if no result with B.COL1 = A.COL1
then let's try a join with B.COL2 = A.COL2
You need to include the negation of the condition B.COL1 = A.COL1 in the second filter. Naively, this is B.COL1 <> A.COL1 but this does not handle NULL values so you need to use B.COL1 <> A.COL1 OR B.COL1 IS NULL OR A.COL1 IS NULL like this:
SELECT A.COL1, A.COL2, B.COL1, B.COL2
FROM A
FULL JOIN (SELECT COL1, COL2 FROM B)
ON ( B.COL1 = A.COL1
OR ( ( B.COL1 <> A.COL1 OR B.COL1 IS NULL OR A.COL1 IS NULL )
AND B.COL2 = A.COL2
)
)
If B.COL1 = A.COL1 then return row. Or, if B.COL1 <> A.COL1 but B.COL2 = A.COL2 also return row.
SELECT A.COL1, A.COL2, B.COL1, B.COL2
FROM A
FULL JOIN (SELECT COL1, COL2 FROM B) B
ON (B.COL1 = A.COL1)
OR (B.COL1 <> A.COL1 and B.COL2 = A.COL2)

Updating a table with in clause Error

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 ...
)