Click here to Skip to main content
15,891,873 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
I work on SQL server 2012 I need to delete rows that not have parent code type and parent code value from table Trade code

drop table #MappingCodeValue
drop table #TradeCode
create table #MappingCodeValue
 (
 id int identity (1,1),
 ParentCodeType  nvarchar(50),
 ParentCodeValue  nvarchar(50),
 ChildCodeType  nvarchar(50),
 ChildCodeValue  nvarchar(50)
 )
 INSERT INTO #MappingCodeValue
 (ParentCodeType,ParentCodeValue,ChildCodeType,ChildCodeValue)
 VALUES
 ('ECCS-US','AB123-US','ECCS-URB','AB123-URB'),
 ('ECCS-US','AB555-US','ECCS-URB','AB555-URB'),
 ('ECCS-US','AB666-US','ECCS-URB','AB666-URB'),
 ('ECCS-US','AB778-US','ECCS-URB','AB778-URB'),
 ('HTS-US','AB900-US','SCHEDUALB','AB900-URB')

 --select * from #MappingCodeValue
 CREATE TABLE #TradeCode
 (
 TradeCodeId int identity(1,1),
 PartId  int,
 CodeType  nvarchar(50),
 CodeValue nvarchar(50),
 PartDone  bit
 )
 insert into #TradeCode(PartId,CodeType,CodeValue,PartDone)VALUES
 (1222,'ECCS-US','AB123-US',null),
 (1255,'ECCS-US','AB555-US',null),
 (1222,'ECCS-URB','AB123-URB',1),
 (1255,'ECCS-URB','AB555-URB',1),
 (1444,'ECCS-URB','AB666-URB',1),
 (1931,'ECCS-URB','AB778-URB',1),
 (7000,'ECCS-URB','AB778-URB',1),
 (8000,'ECCS-URB','AB778-URB',1),
 (800,'SCHEDUALB','AB900-URB',1),
 (900,'SCHEDUALB','AB900-URB',1),
 (1255,'HTS-US','AB900-US',null),
 (909,'HTS-US','AB900-US',null)

3 and 4 not deleted because every part have code type from and code value from is mapping
and is correct
so i will not delete
it is generated from this lines
('ECCS-US','AB123-US','ECCS-URB','AB123-URB'),
('ECCS-US','AB555-US','ECCS-URB','AB555-URB'),

so part have two side from and to so i will not delete
i will delete parts that have from and have to
so it is not have parent to give me result
but records or parts have parent and also child will not delete
and this apply on 3 and 4



Expected Result after delete is

TradeCodeId	PartId	CodeType	CodeValue	PartDone
1	                1222	ECCS-US	        AB123-US	null
2	                1255	ECCS-US	        AB555-US	null
3	                1222	ECCS-URB	AB123-URB	  1
4	                1255	ECCS-URB	AB555-URB	  1
11	                1255	HTS-US	        AB900-US	null
12	                909	HTS-US	        AB900-US	null

File sharing and storage made simple[^]

What I have tried:

SQL
;with mycte as (
 select Partid from #TradeCode t where PartDone=1
 group by Partid 
having (count(*)=1)
)

delete t 
from  #TradeCode t 
join mycte m on m.Partid=t.Partid


select * from #TradeCode
Posted
Updated 15-Apr-20 1:29am
v2
Comments
Richard Deeming 14-Apr-20 8:56am    
Once again, absolutely unclear.

Why are records 3 and 4 still in your "expected output"? There are no records in #MappingCodeValue where the ParentCodeType and ParentCodeValue match the CodeType and CodeValue for those records.

Edit your question and explain the precise relationship between these tables, and the precise conditions for deciding whether or not you keep the record.

Pardon me, I have no SQL editor near me but the query will look roughly as below
SQL
delete from #TradeCode as t
  left join #MappingCodeValue as m
  on (t.codetype = m.parentcodetype) and (t.codevalue = m.parentcodevalue)
where m.parentcodetype is null and m.parentcodevalue is null

The trick is that left join allows to select those values that do not have matching parentcodes in #MappingCodeValue table
 
Share this answer
 
Comments
ahmed_sa 14-Apr-20 7:15am    
thank you for reply it is not give me correct result
remaining record number 5 and 6
as link
https://www.mediafire.com/view/7vjbonu46astw64/DataSheet.png/file
i do
delete t from #TradeCode t
left join #MappingCodeValue as m
on (t.codetype = m.parentcodetype) and (t.codevalue = m.parentcodevalue)
where m.parentcodetype is null and m.parentcodevalue is null
select * from #TradeCode

TradeCodeId PartId CodeType CodeValue partlevel PartDone
1 1222 ECCS-US AB123-US 0 NULL
2 1255 ECCS-US AB555-US 0 NULL
11 1255 HTS-US AB900-US 0 NULL
12 909 HTS-US AB900-US 0 NULL
ahmed_sa 14-Apr-20 9:13am    
I modify my original post if any more details i will explain to you
I solved using this code
--match CodeValue and ParentCodeValue?
SQL
;with cte1 as(
select t.TradeCodeId ,t.PartId ,t.CodeType ,t.CodeValue ,t.PartDone ,m.ParentCodeValue  from #TradeCode t
left join #MappingCodeValue m
on t.CodeValue  =m.ParentCodeValue 
),
cte2 as(
select TradeCodeId ,PartId ,CodeType ,CodeValue ,PartDone from cte1 where PartId in(
select Partid from #TradeCode 
group by Partid 
having count(PartId)=1)
and PartDone =1
and ParentCodeValue is null
)
delete t 
from  #TradeCode t 
join cte2 m on m.Partid=t.Partid

thanks
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900