2017-02-08 91 views
0

如果在t1上不存在相同的itemid,storeid,MSRTime的值,并且在t3上存在相同的itemid,storeid,MSRTime的值,并且状态为D.在下面的例子中,我应该能够删除t2的第二行,但不是第一行。SQL Server:基于另一个表的值从表中删除

表1:T1

itemid |storeid|MSRTime 
x  y  z 

表2:T2

itemid |storeid|MSRTime 
    x  y  z 
    a  b  c 

表3:T3

itemid |storeid|MSRTime|status 
    x  y  z D 
    a  b  c D 

我试图做此使用加入,但我不能达到期望的结果。请帮忙。
谢谢。

回答

1

你几乎可以编写查询完全按照你描述的那样:

declare @t1 table(itemid varchar(7),storeid varchar(9),MSRTime varchar(3)) 
insert into @t1(itemid,storeid,MSRTime) values 
('x','y','z') 
declare @t2 table(itemid varchar(7),storeid varchar(9),MSRTime varchar(3)) 
insert into @t2(itemid,storeid,MSRTime) values 
('x','y','z'), 
('a','b','c') 
declare @t3 table(itemid varchar(7),storeid varchar(9),MSRTime varchar(3),status varchar(4)) 
insert into @t3(itemid,storeid,MSRTime,status) values 
('x','y','z','D'), 
('a','b','c','D') 

delete from t2 
from @t2 t2 
    inner join 
    @t3 t3 
     on 
      t2.itemid = t3.itemid and 
      t2.storeid = t3.storeid and 
      t2.MSRTime = t3.MSRTime and 
      t3.status = 'D' 
where 
    not exists (
     select * 
     from @t1 t1 
     where t1.itemid = t2.itemid and 
      t1.storeid = t2.storeid and 
      t1.MSRTime = t2.MSRTime 
    ) 

select * from @t2 

结果:

itemid storeid MSRTime 
------- --------- ------- 
x  y   z 
+0

我认为这可能为我工作。谢谢你的帮助。当我在真实案例中测试时,我会接受答案。 – Amir

+0

谢谢你它为我的魅力工作。 – Amir

0

应该是这样的

-- delete t2 
select * 
from table2 t2 
JOIN table3 t3 on t2.itemid = t3.itemid and 
        t2.storeid = t3.storeid and 
        t2.MSRTime = t3.MSRTime 
LEFT JOIN table1 t1 on t2.itemid = t1.itemid and 
         t2.storeid = t1.storeid and 
         t2.MSRTime = t1.MSRTime 
where t1.itemID IS NULL 

运行SELECT第一,如果它给你回右排,只是取消注释删除,你是好去

0

我已经创建了整个脚本供您参考。请为您的方案使用最后一个DELETE查询。这将做到这一点。

CREATE TABLE #T1 
(itemid VARCHAR(10) 
,storeid VARCHAR(10) 
,MSRTime VARCHAR(10)) 

INSERT INTO #T1 VALUES ('x','y','z') 

SELECT * FROM #T1 

GO 

CREATE TABLE #T2 
(itemid VARCHAR(10) 
,storeid VARCHAR(10) 
,MSRTime VARCHAR(10)) 

INSERT INTO #T2 VALUES ('x','y','z'),('a','b','c') 

SELECT * FROM #T2 

GO 

CREATE TABLE #T3 
(itemid VARCHAR(10) 
,storeid VARCHAR(10) 
,MSRTime VARCHAR(10) 
,status VARCHAR(10)) 

INSERT INTO #T3 VALUES ('x','y','z','D'),('a','b','c','D') 

SELECT * FROM #T3 

GO 

DELETE M 
FROM #T2 AS M INNER JOIN 
(SELECT itemid,storeid,MSRTime FROM 
(SELECT itemid,storeid,MSRTime FROM #T3 WHERE status='D') T1 
INTERSECT 
(SELECT itemid,storeid,MSRTime FROM 
      (SELECT * FROM #T2 
      EXCEPT 
      SELECT * FROM #T1) T2)) X 
ON X.itemid = M.itemid AND X.storeid = M.storeid AND X.MSRTime = M.MSRTime   

GO 
0

不知道这是否您的环境相匹配,但就方案而言可能是有益的限制,可以比较结果与加入,只有那些在状态有d的值。我也会尝试用Coalese制作一个复合键,这样你就不必在三个单独的连接上进行匹配。

例如 -

itemid |storeid|MSRTime|Key x y z xyz a b c abc

+0

谢谢你的建议。但这个例子是我的问题的一般表述。还有其他领域,我需要检查。 – Amir

+0

为什么你不能简单地在连接上使用AND函数来匹配IE DELETE t2 FROM table2 t2 INNER JOIN table1 t1 ON t1.itemid = t2.itemid AND t1.storied = t2.storied AND t1.MSRTime = t2。 MSRTime –

相关问题