ahmed elbarbary

ahmed elbarbary

  • 618
  • 1.6k
  • 287.6k

How to delete from trades table when Part id not exist on mapping tabl

Mar 14 2021 9:30 PM
How to delete from trades table when Part id not exist on mapping table and have count =1 and on code type 3030?
 
How to delete from table trades where
Part id not exist on mapping table and have count =1 and on code type 3030?
I work on sql server 2012 I face issue i can't delete partid from table trades table where Partid not exist on mapping table and have count =1 and on code type = 3030
so as example partid 2200 not exist on table mapping because code type 8080 and code type 3030 not exist on table mapping and on same time it have count 1 and his code type 3030
so How to write query make delete from table trades where
Part id not exist on mapping table and have count =1 and on code type 3030?
 
  1. create table #trades  
  2.    (  
  3.    TradeCodesId int identity(1,1),  
  4.    PartId int,  
  5.    CodeTypeId int,  
  6.    Code int,  
  7.    PartLevel int  
  8.    )  
  9.    insert into #trades(PartId,CodeTypeId,Code,PartLevel)  
  10.    values  
  11.    (1348,9090,13456,0),  
  12.    (1348,7070,13000,0),  
  13.    (1387,9090,13456,0),  
  14.    (1387,7070,13000,0),  
  15.    (1390,8080,13456,0),  
  16.    (1390,3030,19000,0),  
  17.    (1800,8080,13570,0),  
  18.    (1800,3030,28000,0),  
  19.    (2200,3030,74000,0),  
  20.    (2500,3030,13570,0),  
  21.    (2950,3030,74000,0),  
  22.    (2580,3030,13570,0)  
  23.            
  24.               
  25.     create table #mapping  
  26.     (  
  27.     MapId int,  
  28.     CodeTypeFrom int,  
  29.     CodeTypeTo int,  
  30.     CodeValueFrom int,  
  31.     CodeValueTo int  
  32.     )  
  33.     insert into #mapping(MapId,CodeTypeFrom,CodeValueFrom,CodeTypeTo,CodeValueTo)  
  34.     values  
  35.     (3030,9090,13456,7070,13000),  
  36.     (3035,9095,13570,7075,14000)  
  37.   
  38. expected result  
  39.   
  40.  TradeCodesId    PartId    CodeTypeId    Code    PartLevel  
  41.  9    2200    3030    74000    0  
  42.  10    2500    3030    13570    0  
  43.  11    2950    3030    74000    0  
  44.  12    2580    3030    13570    0  
 

Answers (2)