I have a parent and child table where a parent reference number has multiple entries in child table with status Pending, Success and Rejected. I need a query which will give me an output of only reference numbers which has status Success and Rejected. Even if there is one pending status for the reference number in child table the reference number should not be listed.
CREATE TABLE SI_DETAIL
( "S_NO" VARCHAR2(20 BYTE),
"CREATED_DATE" DATE);
CREATE TABLE SI_TRANSDETAIL
( "S_NO" VARCHAR2(20 BYTE),
"SL_NO" VARCHAR2(20 BYTE),
"EXE_DATE" DATE,
"STATUS" VARCHAR2(20 BYTE));
Insert into SI_DETAIL (S_NO,CREATED_DATE) values ('1000',to_date('29-11-22','DD-MM-RR'));
Insert into SI_DETAIL (S_NO,CREATED_DATE) values ('1001',to_date('01-12-22','DD-MM-RR'));
Insert into SI_DETAIL (S_NO,CREATED_DATE) values ('1002',to_date('30-11-22','DD-MM-RR'));
Insert into SI_TRANSDETAIL (S_NO,SL_NO,EXE_DATE,STATUS) values ('1000','1',to_date('30-11-22','DD-MM-RR'),'REJECTED');
Insert into SI_TRANSDETAIL (S_NO,SL_NO,EXE_DATE,STATUS) values ('1000','2',to_date('01-12-22','DD-MM-RR'),'SUCCESS');
Insert into SI_TRANSDETAIL (S_NO,SL_NO,EXE_DATE,STATUS) values ('1000','3',to_date('02-12-22','DD-MM-RR'),'SUCCESS');
Insert into SI_TRANSDETAIL (S_NO,SL_NO,EXE_DATE,STATUS) values ('1001','1',to_date('02-12-22','DD-MM-RR'),'SUCCESS');
Insert into SI_TRANSDETAIL (S_NO,SL_NO,EXE_DATE,STATUS) values ('1001','2',to_date('03-12-22','DD-MM-RR'),'PENDING');
Insert into SI_TRANSDETAIL (S_NO,SL_NO,EXE_DATE,STATUS) values ('1001','3',to_date('04-12-22','DD-MM-RR'),'PENDING');
Insert into SI_TRANSDETAIL (S_NO,SL_NO,EXE_DATE,STATUS) values ('1001','4',to_date('05-12-22','DD-MM-RR'),'PENDING');
Insert into SI_TRANSDETAIL (S_NO,SL_NO,EXE_DATE,STATUS) values ('1002','1',to_date('04-12-22','DD-MM-RR'),'PENDING');
I have tried using below query now and achieving the desired output. The output should only return 1000 as it has no pending child records. Is there a better solution
SELECT S_NO FROM SI_TRANSDETAIL DTL
WHERE EXISTS (SELECT S_NO FROM SI_DETAIL HD
WHERE DTL.S_NO = HD.S_NO
AND CREATED_DATE < SYSDATE - 1
AND HD.S_NO NOT IN (SELECT S_NO
FROM SI_TRANSDETAIL SUB
WHERE STATUS = 'PENDING'
AND SUB.S_NO = HD.S_NO));
If you are only wanting the S_NO and nothing else, I would use minus
. It works like union
in that the two queries must have the same number of columns and matching column types.
select s_no
from si_detail
where created_date < sysdate - 1
minus
select s_no
from si_transdetail
where status = 'PENDING'
If you are going to include dates or other data in your results, you will need to make the above a subquery and then join it back to another table.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.