I will have a few SQL series blogs and as a WCS developer, I feel it is very important to have good SQL skills.
--Exampple . Find all part numbers that are not found in orders table from the list (DSC_1,DSC_3, DSC_10, DSC_11) in the XORDERITEMS table below.
Result: DSC_10, DSC_11
--1. Table creation:
CREATE TABLE XORDERITEMS (orders_id NUMBER,partnumber varchar2(255), status varchar2(1)) NOLOGGING;
--2. Data insertion into table
insert into /*+ APPEND */ XORDERITEMS values (1,'DSC_1','P');
insert into /*+ APPEND */ XORDERITEMS values (2,'DSC_2','Y');
insert into /*+ APPEND */ XORDERITEMS values (3,'DSC_3','c');
insert into /*+ APPEND */ XORDERITEMS values (4,'DSC_4','c');
insert into /*+ APPEND */ XORDERITEMS values (5,'DSC_5','c');
insert into /*+ APPEND */ XORDERITEMS values (6,'DSC_4','P');
insert into /*+ APPEND */ XORDERITEMS values (7,'DSC_3','y');
insert into /*+ APPEND */ XORDERITEMS values (8,'DSC_7','c');
insert into /*+ APPEND */ XORDERITEMS values (9,'DSC_8','P');
insert into /*+ APPEND */ XORDERITEMS values (10,'DSC_9','P');
--two alternates to fetch the above result.
--Query-1
select pno partnumber from
(SELECT TRIM(SUBSTR ( partnumber , INSTR (partnumber, ',', 1, level ) + 1 , INSTR (partnumber, ',', 1, level+1 ) - INSTR (partnumber, ',', 1, level) -1)) pno
FROM ( SELECT ','||'DSC_1,DSC_3,DSC_10,DSC_11'||',' AS partnumber FROM dual )
CONNECT BY level <= LENGTH(partnumber)-LENGTH(REPLACE(partnumber,',',''))-1 )
where pno not in (select partnumber from XORDERITEMS);
--Alternate Query
select pno partnumber from
(select 'DSC_1' pno from dual
union
select 'DSC_3' pno from dual
union
select 'DSC_10' pno from dual
union
select 'DSC_11' pno from dual)
where pno not in (select partnumber from XORDERITEMS);
--Exampple . Find all part numbers that are not found in orders table from the list (DSC_1,DSC_3, DSC_10, DSC_11) in the XORDERITEMS table below.
Result: DSC_10, DSC_11
--1. Table creation:
CREATE TABLE XORDERITEMS (orders_id NUMBER,partnumber varchar2(255), status varchar2(1)) NOLOGGING;
--2. Data insertion into table
insert into /*+ APPEND */ XORDERITEMS values (1,'DSC_1','P');
insert into /*+ APPEND */ XORDERITEMS values (2,'DSC_2','Y');
insert into /*+ APPEND */ XORDERITEMS values (3,'DSC_3','c');
insert into /*+ APPEND */ XORDERITEMS values (4,'DSC_4','c');
insert into /*+ APPEND */ XORDERITEMS values (5,'DSC_5','c');
insert into /*+ APPEND */ XORDERITEMS values (6,'DSC_4','P');
insert into /*+ APPEND */ XORDERITEMS values (7,'DSC_3','y');
insert into /*+ APPEND */ XORDERITEMS values (8,'DSC_7','c');
insert into /*+ APPEND */ XORDERITEMS values (9,'DSC_8','P');
insert into /*+ APPEND */ XORDERITEMS values (10,'DSC_9','P');
--two alternates to fetch the above result.
--Query-1
select pno partnumber from
(SELECT TRIM(SUBSTR ( partnumber , INSTR (partnumber, ',', 1, level ) + 1 , INSTR (partnumber, ',', 1, level+1 ) - INSTR (partnumber, ',', 1, level) -1)) pno
FROM ( SELECT ','||'DSC_1,DSC_3,DSC_10,DSC_11'||',' AS partnumber FROM dual )
CONNECT BY level <= LENGTH(partnumber)-LENGTH(REPLACE(partnumber,',',''))-1 )
where pno not in (select partnumber from XORDERITEMS);
--Alternate Query
select pno partnumber from
(select 'DSC_1' pno from dual
union
select 'DSC_3' pno from dual
union
select 'DSC_10' pno from dual
union
select 'DSC_11' pno from dual)
where pno not in (select partnumber from XORDERITEMS);