存在表
create table DQD_BSA_FILES_ALL_TEST
(
settle_dt VARCHAR2(24),
trans_id VARCHAR2(9),
sys_tra_no VARCHAR2(18),
term_id VARCHAR2(24),
mchnt_cd VARCHAR2(45),
trans_st VARCHAR2(15),
resp_cd4 VARCHAR2(6),
cross_dist_in VARCHAR2(6),
trans_at VARCHAR2(6)
)
--测试数据(正式数据表中上千万,因此SQL必须考虑效率哦):
insert into DQD_BSA_FILES_ALL_TEST (SETTLE_DT, TRANS_ID, SYS_TRA_NO, TERM_ID, MCHNT_CD, TRANS_ST, RESP_CD4, CROSS_DIST_IN, TRANS_AT)
values ('20140409', 'R22', '628963', '80000000', '443701094980169', '10000', '00', '0', 358);
insert into DQD_BSA_FILES_ALL_TEST (SETTLE_DT, TRANS_ID, SYS_TRA_NO, TERM_ID, MCHNT_CD, TRANS_ST, RESP_CD4, CROSS_DIST_IN, TRANS_AT)
values ('20140409', 'S22', '628963', '80000000', '443701094980169', '10010', '00', '0', 162);
insert into DQD_BSA_FILES_ALL_TEST (SETTLE_DT, TRANS_ID, SYS_TRA_NO, TERM_ID, MCHNT_CD, TRANS_ST, RESP_CD4, CROSS_DIST_IN, TRANS_AT)
values ('20140409', 'S23', '604047', '52010001', '302520153110003', '11000', '00', '0', 0.01);
insert into DQD_BSA_FILES_ALL_TEST (SETTLE_DT, TRANS_ID, SYS_TRA_NO, TERM_ID, MCHNT_CD, TRANS_ST, RESP_CD4, CROSS_DIST_IN, TRANS_AT)
values ('20140409', 'S23', '606012', '52010001', '302520153110003', '11000', '00', '0', 0.01);
insert into DQD_BSA_FILES_ALL_TEST (SETTLE_DT, TRANS_ID, SYS_TRA_NO, TERM_ID, MCHNT_CD, TRANS_ST, RESP_CD4, CROSS_DIST_IN, TRANS_AT)
values ('20140409', 'R22', '628964', '80000003', '443701094980169', '10000', '00', '0', 358);
insert into DQD_BSA_FILES_ALL_TEST (SETTLE_DT, TRANS_ID, SYS_TRA_NO, TERM_ID, MCHNT_CD, TRANS_ST, RESP_CD4, CROSS_DIST_IN, TRANS_AT)
values ('20140409', 'S22', '628964', '80000003', '443701094980169', '10010', '00', '0', 162);
insert into DQD_BSA_FILES_ALL_TEST (SETTLE_DT, TRANS_ID, SYS_TRA_NO, TERM_ID, MCHNT_CD, TRANS_ST, RESP_CD4, CROSS_DIST_IN, TRANS_AT)
values ('20140409', 'R22', '628963', '80000000', '443701094980169', '10000', '00', '0', 358);
insert into DQD_BSA_FILES_ALL_TEST (SETTLE_DT, TRANS_ID, SYS_TRA_NO, TERM_ID, MCHNT_CD, TRANS_ST, RESP_CD4, CROSS_DIST_IN, TRANS_AT)
values ('20140409', 'S56', '628967', '80000007', '443701094980167', '10010', '00', '0', 162);
insert into DQD_BSA_FILES_ALL_TEST (SETTLE_DT, TRANS_ID, SYS_TRA_NO, TERM_ID, MCHNT_CD, TRANS_ST, RESP_CD4, CROSS_DIST_IN, TRANS_AT)
values ('20140409', 'R56', '628967', '80000007', '443701094980167', '10000', '00', '0', 358);
/**
现在需要在上面的表中找出满足条件的数据并删除,条件如下:
存在S22和R22两条数据,并且 sys_tra_no、term_id 、mchnt_cd 相同,其中两条的resp_cd4 =‘00’、S22的trans_st =‘10010’,R22的trans_st =‘10000’;
S56和R56同上需求。
*/
目前我使用存储过程实现的,希望大神能够分析实现此需求的sql,不吝指教,多谢!!!
------解决思路----------------------
单引号写成中文的了,并且少些了个条件,修正下
select * from DQD_BSA_FILES_ALL_TEST T
WHER EXISTS(SELECT 1 FROM DQD_BSA_FILES_ALL_TEST
WHERE sys_tra_no=T.sys_tra_no AND term_id=T.term_id AND mchnt_cd=T.mchnt_cd
AND TRANS_ID='S22' AND trans_st ='10010' and resp_cd4 ='00')
AND EXISTS(SELECT 1 FROM DQD_BSA_FILES_ALL_TEST
WHERE sys_tra_no=T.sys_tra_no AND term_id=T.term_id AND mchnt_cd=T.mchnt_cd
AND TRANS_ID='R22' AND trans_st ='10000' and resp_cd4 ='00')