효율적인 쿼리는 다른분들이 알려주실거라 믿고
제가 생각해본 내용 공유드려요
with tmp_a as (
select 1 as buyid, 10 as sellid union all
select 2 , 11 union all
select 3 , 12 union all
select 10, 4 union all
select 11, 5 union all
select 12, 6 union all
select 10, 10
)
select sellid /* 판매/구매 모두 한 id */
from (
select sellid
from tmp_a
where buyid != sellid
group by sellid
) selllist /* 판매자 id list */
inner join (
select buyid
from tmp_a
where buyid != sellid
group by buyid
) buylist /* 구매자 id list */
on selllist.sellid = buylist.buyid /* 판매/구매 모두 한 list */
다른형태로 작성한것도 공유드려요
with tmp_a as (
select 1 as buyid, 10 as sellid union all
select 2 , 11 union all
select 3 , 12 union all
select 10, 4 union all
select 11, 5 union all
select 12, 6 union all
select 10, 10 union all
select 4 , 4 union all
select 3 , 3
)
select buyid from tmp_a aa
where aa.buyid != aa.sellid
and exists (select 1 from tmp_a bb
where aa.buyid = bb.sellid
and bb.buyid != bb.sellid)
group by buyid
WITH t AS ( SELECT 1 buy_user_id, 10 sell_user_id FROM dual UNION ALL SELECT 2, 11 FROM dual UNION ALL SELECT 3, 12 FROM dual UNION ALL SELECT 10, 4 FROM dual UNION ALL SELECT 11, 5 FROM dual UNION ALL SELECT 12, 6 FROM dual UNION ALL SELECT 10, 10 FROM dual ) SELECT COUNT(DISTINCT a.buy_user_id) cnt FROM t a , t b WHERE a.buy_user_id = b.sell_user_id AND b.buy_user_id != b.sell_user_id ;