I have a table with below structure:
create table TEST_REFUND_CASHOUT
(
f_gdate DATE,
trackingcode VARCHAR2(4000),
cashout_trackingcode VARCHAR2(4000),
cashout_date DATE
)
Given the sample data:
f_gdate trackingcode cashout_trackingcode cashout_date
--------------------------------------------------------------
4/29/2025 6088 195 5/15/2025
4/29/2025 6088 201 5/15/2025
4/29/2025 6088 186 5/18/2025
4/29/2025 6088 129 5/19/2025
5/18/2025 1754 186 5/18/2025
5/18/2025 1754 129 5/19/2025
5/18/2025 1754 814 5/24/2025
5/18/2025 1754 191 5/24/2025
5/27/2025 1213 333 5/28/2025
From above table, I need to see only those trackingcode for which all the cashout_trackingcodes do not belong to another trackingcode.
So, I should only see trackingcode = 1213 because its cashout_trackingcode = 333 belongs only to this trackingcode. Other two remaining trackingcode have intersections. For instance, cashout_trackingcode = 129 belongs to another trackingcode as well which is 1754.
The solution I have found is below:
select *
from TEST_REFUND_CASHOUT t
where not exists (select 1
from TEST_REFUND_CASHOUT a
where t.trackingcode <> a.trackingcode
and a.cashout_trackingcode = t.cashout_trackingcode)
however it does not work and gives me wrong result ! thanks in advance