Converting a SQL SELECT query into a DELETE query - sql

How would I turn this select query into a delete query? (deleting records from tbl_a_D_B_t_Items)
SELECT *
FROM
tbl_a_D_B_t_Items
LEFT JOIN
tbl_tr_D_Items
ON
tbl_a_D_B_t_Items.D_t_ItemID=tbl_tr_D_Items.D_t_ItemID
WHERE
(
tbl_tr_D_Items.D_t_ItemID
Is Null
)
;

DELETE X FROM tbl_a_D_B_t_Items X
LEFT JOIN
tbl_tr_D_Items Y
ON
X.D_t_ItemID=Y.D_t_ItemID
WHERE
(
Y.D_t_ItemID
Is Null
)

I would suggest not exists:
DELETE FROM tbl_a_D_B_t_Items a
WHERE NOT EXISTS (SELECT 1
FROM tbl_tr_D_Items d
WHERE a.D_t_ItemID = d.D_t_ItemID
);

Maybe this better:
DELETE FROM tbl_a_D_B_t_Items
WHERE D_t_ItemID NOT IN
( SELECT D_t_ItemID FROM tbl_tr_D_Items)

Related

Why is my query inserting the same values when I have added a 'not exists' parameter that should avoid this from happening?

My query should stop inserting values, as the not exists statement is satisfied (I have checked both tables) and matching incidents exist in both tables, any ideas why values are still being returned?
Here is the code:
INSERT INTO
odwh_system.ead_incident_credit_control_s
(
incident
)
SELECT DISTINCT
tp.incident
FROM
odwh_data.ead_incident_status_audit_s ei
INNER JOIN odwh_data.ead_incident_s tp ON ei.incident=tp.incident
WHERE
ei.status = 6
OR
ei.status = 7
AND NOT EXISTS
(
SELECT
true
FROM
odwh_system.ead_incident_credit_control_s ead
WHERE
ead.incident = tp.incident
)
AND EXISTS
(
SELECT
true
FROM
odwh_work.ead_incident_tp_s tp
WHERE
tp.incident = ei.incident
);
dont reuse table aliases
use sane aliases
avoid AND/OR conflicts; prefer IN()
INSERT INTO odwh_system.ead_incident_credit_control_s (incident)
SELECT -- DISTINCT
tp.incident
FROM odwh_data.ead_incident_s dtp
WHERE NOT EXISTS (
SELECT *
FROM odwh_system.ead_incident_credit_control_s sic
WHERE sic.incident = dtp.incident
)
AND EXISTS (
SELECT *
FROM odwh_work.ead_incident_tp_s wtp
JOIN odwh_data.ead_incident_status_audit_s dis ON wtp.incident = dis.incident AND dis.status IN (6 ,7)
WHERE wtp.incident = dtp.incident
);

ERROR SQL Server ONLY one expression can be specified in the select list when the subquery is not introduced with exists

I am trying to run my query but I get an error.
This is my query:
if exists (select CODE_ISIN
from cte
where code_ISIN not in (select [STATUT_TITRE], [CODE_ISIN]
from TT_TITRE A
inner join TT_STATUT_TITRE B on A.TITRE_ID = B.TITRE_ID))
begin
select 'ko'
end
begin
select 'ok'
end
Remove [STATUT_TITRE] from sub-query as it will accept only one expression :
select c.CODE_ISIN
from cte c
where code_ISIN not in (select [CODE_ISIN] -- only one expression needed
from TT_TITRE A inner join
TT_STATUT_TITRE B
on A.TITRE_ID = B.TITRE_ID
);
I would suggest to use NOT EXISTS instead :
where not exists (select 1
from TT_TITRE A inner join
TT_STATUT_TITRE B
on A.TITRE_ID=B.TITRE_ID
where CODE_ISIN = c.CODE_ISIN
);

Oracle SQL XOR condition with > 14 tables

I have a question on sql desgin.
Context:
I have a table called t_master and 13 other tables (lets call them a,b,c... for simplicity) where it needs to compared.
Logic:
t_master will be compared to table 'a' where t_master.gen_val =
a.value.
If record exist in t_master, retrieve t_master record, else retrieve 'a' record.
I do not need to retrieve the records if it exists in both tables (t_master and a) - XOR condition
Repeat this comparison with the remaining 12 tables.
I have some idea on doing this, using WITH to subquery the non-master tables (a,b,c...) first with their respective WHERE clause.
Then use XOR statement to retrieve the records.
Something like
WITH a AS (SELECT ...),
b AS (SELECT ...)
SELECT field1,field2...
FROM t_master FULL OUTER JOIN a FULL OUTER JOIN b FULL OUTER JOIN c...
ON t_master.gen_value = a.value
WHERE ((field1 = x OR field2 = y ) AND NOT (field1 = x AND field2 = y))
AND ....
.
.
.
.
Seeing that I have 13 tables that I need to full outer join, is there a better way/design to handle this?
Otherwise I would have at least 2*13 lines of WHERE clause which I'm not sure if that will have impact on the performance as t_master is sort of a log table.
**Assume I cant change any schema.
Currently I'm not sure if this SQL will working correctly yet, so I'm hoping someone can guide me in the right direction regarding this.
update from used_by_already's suggestion:
This is what I'm trying to do (comparison between 2 tables first, before I add more, but I am unable to get values from ATP_R.TBL_HI_HDR HI_HDR as it is in the NOT EXISTS subquery.
How do i overcome this?
SELECT LOG_REPO.UNIQ_ID,
LOG_REPO.REQUEST_PAYLOAD,
LOG_REPO.GEN_VAL,
LOG_REPO.CREATED_BY,
TO_CHAR(LOG_REPO.CREATED_DT,'DD/MM/YYYY') AS CREATED_DT,
HI_HDR.HI_NO R_VALUE,
HI_HDR.CREATED_BY R_CREATED_BY,
TO_CHAR(HI_HDR.CREATED_DT,'DD/MM/YYYY') AS R_CREATED_DT
FROM ATP_COMMON.VW_CMN_LOG_GEN_REPO LOG_REPO JOIN ATP_R.TBL_HI_HDR HI_HDR ON LOG_REPO.GEN_VAL = HI_HDR.HI_NO
WHERE NOT EXISTS
(SELECT NULL
FROM ATP_R.TBL_HI_HDR HI_HDR
WHERE LOG_REPO.GEN_VAL = HI_HDR.HI_NO
)
UNION ALL
SELECT LOG_REPO.UNIQ_ID,
LOG_REPO.REQUEST_PAYLOAD,
LOG_REPO.GEN_VAL,
LOG_REPO.CREATED_BY,
TO_CHAR(LOG_REPO.CREATED_DT,'DD/MM/YYYY') AS CREATED_DT,
HI_HDR.HI_NO R_VALUE,
HI_HDR.CREATED_BY R_CREATED_BY,
TO_CHAR(HI_HDR.CREATED_DT,'DD/MM/YYYY') AS R_CREATED_DT
FROM ATP_R.TBL_HI_HDR HI_HDR JOIN ATP_COMMON.VW_CMN_LOG_GEN_REPO LOG_REPO ON HI_HDR.HI_NO = LOG_REPO.GEN_VAL
WHERE NOT EXISTS
(SELECT NULL
FROM ATP_COMMON.VW_CMN_LOG_GEN_REPO LOG_REPO
WHERE HI_HDR.HI_NO = LOG_REPO.GEN_VAL
)
Full outer joins used to exclude all matching rows can be an expensive query. You don't supply much detail, but perhaps using NOT EXISTS would be simpler and maybe it will produce a better explain plan. Something along these lines.
select
cola,colb,colc
from t_master m
where not exists (
select null from a where m.keycol = a.fk_to_m
)
and not exists (
select null from b where m.keycol = b.fk_to_m
)
and not exists (
select null from c where m.keycol = c.fk_to_m
)
union all
select
cola,colb,colc from a
where not exists (
select null from t_master m where a.fk_to_m = m.keycol
)
union all
select
cola,colb,colc from b
where not exists (
select null from t_master m where b.fk_to_m = m.keycol
)
union all
select
cola,colb,colc from c
where not exists (
select null from t_master m where c.fk_to_m = m.keycol
)
You could union the 13 a,b,c ... tables to simplify the coding, but that may not perform so well.

With exists vs without exists in oracle

How would my output change if I remove exists statement from the following query.
UPDATE AR_CRDT C
set (OPERATOR_ID,UPDT_TS) = ( SELECT AD.OPERATOR_ID,sysdate
FROM
(
Select A.*
FROM AR1_CUSTOMER_CREDIT A
)AD
WHERE AD.CREDIT_ID = C.CRDT_ID
)
where exists ( SELECT 1
FROM
(
Select A.*
FROM AR1_CUSTOMER_CREDIT A
)AD
WHERE AD.CREDIT_ID = C.CRDT_ID
);
If you remove the exists, then OPERATOR_ID and UPDT_TS would be set to NULL for rows where the conditions do not match.
Why would you write this with the extra subqueries?
UPDATE AR_CRDT C
SET (OPERATOR_ID, UPDT_TS) = (SELECT AD.OPERATOR_ID, sysdate
FROM AR1_CUSTOMER_CREDIT A
WHERE A.CREDIT_ID = C.CRDT_ID
)
WHERE EXISTS (SELECT 1
FROM AR1_CUSTOMER_CREDIT A
WHERE A.CREDIT_ID = C.CRDT_ID
);
The EXISTS currently causes the updates to AR_CRDT to be limited to those records where the AR_CRDT record is associated with an AR1_CUSTOMER_CREDIT record.
Removing that clause will mean that ALL records of AR_CRDT will be updated. Some will have values for the sub-query and some will be NULL where there is no link between AR_CRDT and AR1_CUSTOMER_CREDIT.

select query with Not IN keyword

I have two Tables as below..
tbPatientEncounter
tbVoucher
when i execute select query as below
Select EncounterIDP,EncounterNumber from tbPatientEncounter
it returens me 180 rows. and
Select VoucherIDP,EncounterIDF from tbVoucher
above query returns me 165 rows.
but i want to execute select query that returns me data like EncounterIDP not in tbVoucher, for that i have tried below Select query...
Select * from tbPatientEncounter pe
where pe.EncounterIDP not in
(Select v.EncounterIDF from tbVoucher v )
it doesn't returns any row. in first image it shows EncounterIDP 9 in tbPatientEncounter, but it not inserted in tbVoucher for that i have tried select Query like
Select * from tbVoucher where EncounterIDF = 9
it returns me 0 rows.
My question is what is wrong with my above Not In Query.?
In all likelihood, the problem is NULL values in tbVoucher. Try this:
Select *
from tbPatientEncounter pe
where pe.EncounterIDP not in (Select v.EncounterIDF
from tbVoucher v
where v.EncounterIDF is not NULL
)
Are you comparing the correct fields in tbVoucher?
Try using a left join
Select EncounterIDP,EncounterNumber from tbPatientEncounter
left join tbVoucher on EncounterIDP = EncounterIDF
where EncounterIDF is null
Call me a skeptic because I don't see anything wrong with your query. Is this really all in the query or did you simplify it for us?
Select * from tbPatientEncounter pe
where pe.EncounterIDP not in
(Select v.EncounterIDF from tbVoucher v )