-
Erland Sommarskog 78,746 Reputation points MVP
2021-09-24T21:34:54.28+00:00 This query may work for you, although the result does not match our expected exactly. On the other hand, I am not sure your output is consistent.
SELECT 'P&B Match' AS text, a.portfolio, a.bin FROM test_all_accounts a WHERE EXISTS (SELECT * FROM test_client_subset c WHERE a.portfolio = c.portfolio AND a.bin = c.bin) UNION ALL SELECT 'Accounts port match' AS text, a.portfolio, a.bin FROM test_all_accounts a WHERE EXISTS (SELECT * FROM test_client_subset c WHERE a.portfolio = c.portfolio) AND NOT EXISTS (SELECT * FROM test_client_subset c WHERE a.portfolio = c.portfolio AND a.bin = c.bin) UNION ALL SELECT 'Client port match' AS text, c.portfolio, c.bin FROM test_client_subset c WHERE EXISTS (SELECT * FROM test_all_accounts a WHERE a.portfolio = c.portfolio) AND NOT EXISTS (SELECT * FROM test_all_accounts a WHERE a.portfolio = c.portfolio AND a.bin = c.bin) UNION ALL SELECT 'Accounts P&B NOT match' AS text, a.portfolio, a.bin FROM test_all_accounts a WHERE NOT EXISTS (SELECT * FROM test_client_subset c WHERE a.portfolio = c.portfolio) UNION ALL SELECT 'Client P&B not match' AS text, c.portfolio, c.bin FROM test_client_subset c WHERE NOT EXISTS (SELECT * FROM test_all_accounts a WHERE a.portfolio = c.portfolio) ORDER BY portfolio, bin
Hi @brenda grossnickle ,
Please also refer below:
select 'P&B Match' AS text,a.*
from test_all_accounts a
inner join test_client_subset b
on a.portfolio=b.portfolio and a.bin=b.bin
Union all
select 'Accounts P&B Not Match' AS text,a.*
from test_all_accounts a
left join test_client_subset b
on a.portfolio=b.portfolio
where b.portfolio is null
Union all
select 'Client P&B Not Match' AS text,b.*
from test_all_accounts a
right join test_client_subset b
on a.portfolio=b.portfolio
where a.portfolio is null
Union all
select 'Accounts Port Match' AS text,* from
(select a.*
from test_all_accounts a
inner join test_client_subset b
on a.portfolio=b.portfolio
except
select a.*
from test_all_accounts a
inner join test_client_subset b
on a.portfolio=b.portfolio and a.bin=b.bin)c
Union all
select 'Client Port Match' AS text,* from
(select b.*
from test_all_accounts a
inner join test_client_subset b
on a.portfolio=b.portfolio
except
select b.*
from test_all_accounts a
inner join test_client_subset b
on a.portfolio=b.portfolio and a.bin=b.bin)c
ORDER BY portfolio, bin
Output:
text portfolio bin
P&B Match p111 b111a
Accounts Port Match p111 b111b
Client Port Match p111 b111x
P&B Match p222 b222a
P&B Match p222 b222a
Client Port Match p222 b222x
Accounts P&B Not Match p333 b333a
Client P&B Not Match p444 b444a
Best regards,
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.