Delete Records from table on the basis of inner join - sql

I have two tables in oracle DB called collection and collection_h. I have to delete all the records from collection_h which has the same below fields in the collection table.
I have to delete all the records from collection_h table that comes out as a result of the below query:
select * from collection inner join collection_h on
collection.pos_protocol_id = collection_h.pos_protocol_id and
collection.terminal_pos_number = collection_h.terminal_pos_number and
collection.cb_file_number = collection_h.cb_file_number and
collection.cb_block_number = collection_h.cb_block_number and
collection.is_stan_batch = collection_h.is_stan_batch and
collection.is_transaction_date = collection_h.is_transaction_date and
collection.is_stan_trans = collection_h.is_stan_trans;

Delete where exists
delete
from collection as c
where exists (
select 1
from collection_h as h
where h.pos_protocol_id = c.pos_protocol_id
and h.terminal_pos_number = c.terminal_pos_number
and h.cb_file_number = c.cb_file_number
and h.cb_block_number = c.cb_block_number
and h.is_stan_batch = c.is_stan_batch
and h.is_transaction_date = c.is_transaction_date
and h.is_stan_trans = c.is_stan_trans
);
Simplified test on db<>fiddle here
But if the columns can have NULL's in the matching rows
delete
from collection as c
where exists (
select 1
from collection_h as h
where decode(h.pos_protocol_id, c.pos_protocol_id, 0, 1) = 0
and decode(h.terminal_pos_number, c.terminal_pos_number, 0, 1) = 0
and decode(h.cb_file_number, c.cb_file_number, 0, 1) = 0
and decode(h.cb_block_number, c.cb_block_number, 0, 1) = 0
and decode(h.is_stan_batch, c.is_stan_batch, 0, 1) = 0
and decode(h.is_transaction_date, c.is_transaction_date, 0, 1) = 0
and decode(h.is_stan_trans, c.is_stan_trans, 0, 1) = 0
);

Delete x from collection X inner join collection_h Y on
X.pos_protocol_id = Y.pos_protocol_id and
X.terminal_pos_number = Y.terminal_pos_number and
X.cb_file_number = Y.cb_file_number and
X.cb_block_number = Y.cb_block_number and
X.is_stan_batch = Y.is_stan_batch and
X.is_transaction_date = Y.is_transaction_date and
X.is_stan_trans = Y.is_stan_trans;

Related

sql how to add inner join and multiple conditions to filter

I have this query
SELECT DISTINCT IP.IRId
FROM cmp.NPTable NP
INNER JOIN IPTable IP ON IP.IPtId = NP.IPd
LEFT JOIN IPCTable IPC ON IPC.IPId = NP.IPId
WHERE NP.PCN Id = #PCNId
AND (IP.IsCompliant = 1 AND IPC.CheckId = 1) OR (IP.IsCompliant = 0 AND IPC.CheckId = 1)
This is not working correcty
THe IPTable either has IsCompliant = null, 0 or 1 -- this is basically to indicate true or false
IPC.CheckId is either 1 or 2 -- this is the primary ID in this table
The only match integer match is the value 1 between the two tables.
I only want to bring back rows if IsCompliant = 1 and CheckId = 1
else if IsCompliant is null or 0, then CheckId = 2.
The clause I added to my where which is
AND (IP.IsCompliant = 1 AND IPC.CheckId = 1) OR (IP.IsCompliant = 0 AND IPC.CheckId = 1)
This does not work. Help. Will be very appreciated.
Thanks
WHERE NP.PCN Id = #PCNId
AND
(
(IP.IsCompliant = 1 AND IPC.CheckId = 1) OR
((IP.IsCompliant is null or IP.IsCompliant = 0) AND IPC.CheckId = 2)
)

Bring a row that is not in one of the 2 tables

I have this query in sql
SELECT
SI_Articulo = COALESCE(t.SI_Articulo, c.SI_Num_Articulo),
SI_Ubicacion = COALESCE(t.SI_Ubicacion, c.SI_Ubicacion),
SI_OV = COALESCE(c.SI_OV,''),
SI_Ubicacion_Fisica = COALESCE(c.SI_Ubicacion_Fisica,''),
SI_Existencia = COALESCE(t.SI_Existencia, 0),
SI_Cantidad = COALESCE(c.SI_Cantidad, 0),
SI_Cantidad2 = (SELECT COALESCE(c2.SI_Cantidad,0)
FROM SI_Conteo c2
WHERE c2.SI_Num_Conteo = 2 /*AND c2.SI_Num_Articulo = 200002*/
AND t.SI_Ubicacion = c2.SI_Ubicacion),
SI_Dif = COALESCE(c.SI_Cantidad, 0) - COALESCE(t.SI_Existencia, 0),
SI_Dif_Dinero = (COALESCE(c.SI_Cantidad,0) - COALESCE(t.SI_Existencia,0)) * COALESCE(m.SI_Costo_Promedio,0)
FROM SI_Inventario_Teorico_QAD t
LEFT JOIN SI_Conteo c
ON (t.SI_Articulo = c.SI_Num_Articulo
AND c.SI_Num_Conteo = 1
AND t.SI_Ubicacion = c.SI_Ubicacion)
INNER JOIN SI_Maestro_Ref_QAD m
ON (t.SI_Articulo = m.SI_Num_Articulo
OR c.SI_Num_Articulo = m.SI_Num_Articulo)
WHERE c.SI_Num_Articulo = 200002
OR t.SI_Articulo = 200002
Which brings me the next result
img1
My problem is that in the SI_Conteo table I have the same reference added, but it does not exist in the SI_SI_Inventario_Teorico_QAD table so it does not show it to me.
This is the one that does not bring me
img2
According to your problem that the reference in the table "SI_Conteo" and not in the table "SI_Inventario_Teorico_QAD",
you may need to use right join like:
SI_Conteo right join SI_Inventario_Teorico_QAD
or use left like:
SI_Inventario_Teorico_QAD left join SI_Conteo

Catching last datetime after update wont result

I have the following code:
UPDATE sm
SET sm_fecha_venc = (SELECT MIN(nd_fecha_venc) FROM [db_facturacion].[dbo].[tb_notas_debito]
WHERE sm_codigo = nd_num_servicio
-- AND nd_referencia = sm_cod_producto
AND nd_num_factura = sm_cod_factura
AND nd_estado = 'G')
,sm_fecha_ult_pago = (SELECT MAX(nd_fecha_pago) FROM [db_facturacion].[dbo].[tb_notas_debito]
WHERE sm_codigo = nd_num_servicio
-- AND nd_referencia = sm_cod_producto
AND nd_num_factura = sm_cod_factura
AND nd_estado = 'C')
,sm_fecha = GETDATE()
,sm_cod_factura_ren = #i_num_factura
OUTPUT DELETED.sm_fecha_venc AS FECHA_VENC_OLD,
DELETED.sm_fecha_ult_pago AS FECHA_ULT_PAGO_OLD,
DELETED.sm_fecha AS FECHA_OLD,
DELETED.sm_cod_factura_ren AS COD_FACTURA_REN_OLD
INTO #TABLATABLA --
FROM [db_facturacion].[dbo].[tb_servicios] sm --WITH(NOLOCK)
INNER JOIN #servicios AS T ON sm_codigo = num_servicio
WHERE sm_tipo_bien_protegido = 1
AND [sm_estado] = 1
--AND sm.sm_cod_forma_contrato = 1
AND sm.sm_tipo_inventario = tipo_inventario
/*===========================
INSERTED DELETED
=============================*/
UPDATE db_facturacion.[dbo].[tb_log_cambio_servicio]
SET cs_fecha_venc_old = FECHA_VENC_OLD
,cs_fecha_ult_pago_old = FECHA_ULT_PAGO_OLD
,cs_fecha_old = FECHA_OLD
,cs_cod_factura_ren_old = COD_FACTURA_REN_OLD
FROM #TABLATABLA
WHERE cs_codigo = #ID_ULTIMO_ING (LAST ##IDENTITY)
Im trying to save the last dates into a log table, just in case I have to return those dates back.
The code works good, just that it's catching the actual date and inserted in the log table.
What am I missing?

SELECT FROM depending on a parameter in SQL

I want to obtain from a parameter, #Display the value 0, 1 or 2. When #Display = 0, I want to display all the items for which ec.IsEquipmentRelated is true. When #Display = 0, I want to display all the items for which ec.IsEquipmentRelated is false and when #Display = 2, I want to display all the items for which ec.IsEquipmentRelated is true OR false. How can I implement this in the FROM section?
ALTER PROCEDURE [dbo].[Downtime_GetNewCause_EquimentLocation]
#DisplayInactive bit = 0,
#SortOrder INT = 0,
#Diplay INT = 0
AS
-- Main Data source
SELECT ic.IncidentCauseID
, 'EquimentRelated' = COALESCE(ec.IsEquipmentRelated, 0)
, ic.NewIncidentCauseID
, ic.DisplayName
, ic.IsLegacy
, el.EquipmentLocationID
, el.ShopID
, ec.EquipmentClassID
, ec.EquipmentAbbr
, el.ClassSequenceNumber
, el.EquipmentComponent
, el.CompSequenceNumber
, ic.IsActive
FROM Downtime_IncidentCauses ic
LEFT JOIN Downtime_EquipmentLocations el ON ic.EquipmentLocationID = el.EquipmentLocationID
LEFT JOIN Downtime_EquipmentClasses ec ON el.EquipmentClassID = ec.EquipmentClassID AND
CASE WHEN #Diplay = 0 THEN ...
CASE WHEN #Diplay = 1 THEN ...
CASE WHEN #Diplay = 2 THEN ...
This should work:
INNER JOIN Downtime_EquipmentClasses ec
ON (el.EquipmentClassID = ec.EquipmentClassID)
AND ( (#Display = 0 AND ec.IsEquipmentRelated = 1)
OR (#Display = 1 AND ec.IsEquipmentRelated = 0)
OR (#Display = 2) )
Do it in a WHERE clause:
WHERE (#Display = 0 AND ec.IsEquipmentRelated = 'True')
OR (#Display = 1 AND ec.IsEquipmentRelated = 'False')
OR #Display = 2

sql: where subquery not null

I have the following sql query and I want to filter the results where the alias imagefile is null, but I can't get it to work. it's kinda basic sql... sorry for that!
SELECT Categorie.CategorieID, Categorie.Highlight, CategorieTaal.CategorieNaam,
(SELECT TOP (1) ImageFile
FROM Artikel WHERE (CategorieID = Categorie.CategorieID)
AND (Onzichtbaar = 0)
AND (NietBestelbaar = 0)
AND (Voorraad = - 1000 OR Voorraad > LevertijdDrempel)
ORDER BY Volgnummer, ArtikelID DESC) AS 'imagefile'
FROM Categorie INNER JOIN
CategorieTaal ON
Categorie.CategorieID = CategorieTaal.CategorieID
WHERE (Categorie.CategorieGroepID = #catgroepid)
AND (Categorie.Onzichtbaar = 0)
AND (CategorieTaal.TaalCode = #tc)
ORDER BY Categorie.Volgnummer, CategorieTaal.CategorieNaam
You might want to try this:
SELECT Categorie.CategorieID, Categorie.Highlight, CategorieTaal.CategorieNaam,
FROM Categorie
INNER JOIN
CategorieTaal ON
Categorie.CategorieID = CategorieTaal.CategorieID
WHERE (Categorie.CategorieGroepID = #catgroepid)
AND (Categorie.Onzichtbaar = 0)
AND (CategorieTaal.TaalCode = #tc)
AND NOT EXISTS (SELECT 1 ImageFile
FROM Artikel WHERE (CategorieID = Categorie.CategorieID)
AND (Onzichtbaar = 0)
AND (NietBestelbaar = 0)
AND (Voorraad = - 1000 OR Voorraad > LevertijdDrempel))
ORDER BY Categorie.Volgnummer, CategorieTaal.CategorieNaam
You can optimize this by using an inner join again, in lieu of trying to use a subquery twice:
SELECT
c.CategorieID,
c.Highlight,
ct.CategorieNaam,
a.ImageFile
FROM
Categorie c
INNER JOIN CategorieTaal ct ON
c.CategorieID = ct.CategorieID
INNER JOIN
(select
CategorieID,
ImageFile,
row_number() over (partition by CategorieID) as rownum
from
Artikel
where
Onzichtbaar = 0
and NietBestelbaar = 0
and (Voorraad = -1000 OR Voorraad > LevertijdDrempel)) a ON
c.CategorieID = a.CategorieID
and a.rownum = 1
WHERE
c.CategorieGroepID = #catgroepid
AND c.Onzichtbaar = 0
AND ct.TaalCode = #tc
ORDER BY c.Volgnummer, ct.CategorieNaam
Since you're using SQL Server (or at least I think you are, with your top and whatnot), you can take advantage of row_number. This will bring back just the ImageFile you need, without having to do two correlated subqueries (usually performance killers).
Also, here you only have to maintain that subquery in one place, not in two different parts of your query.
found it!!
SELECT Categorie.CategorieID, Categorie.Highlight, CategorieTaal.CategorieNaam,
(SELECT TOP (1) ImageFile
FROM Artikel
WHERE (CategorieID = Categorie.CategorieID)
AND (Onzichtbaar = 0)
AND (NietBestelbaar = 0)
AND (Voorraad = - 1000
OR Voorraad > LevertijdDrempel)
ORDER BY Volgnummer, ArtikelID DESC) AS 'imagefile'
FROM Categorie
INNER JOIN CategorieTaal ON Categorie.CategorieID = CategorieTaal.CategorieID
WHERE (Categorie.CategorieGroepID = #catgroepid)
AND (Categorie.Onzichtbaar = 0)
AND (CategorieTaal.TaalCode = #tc)
AND ((
SELECT TOP (1) ImageFile
FROM Artikel AS Artikel_1
WHERE (CategorieID = Categorie.CategorieID)
AND (Onzichtbaar = 0)
AND (NietBestelbaar = 0)
AND (Voorraad = - 1000
OR Voorraad > LevertijdDrempel)
) IS NOT NULL)
ORDER BY Categorie.Volgnummer, CategorieTaal.CategorieNaam