with
temp as
(
select ...
(case when ....then..else... end) as dist_1,
... as goods_1,
from ...
where ...
)
select *
from temp gg
where (dist_1 = '회원 or dist_1 = '비회원' )and
gg.order_no in (select order_no from temp where goods_1 = '가방')
order by gg.order_no
위의 구문을 실행해서 아래와 같은 결과를 얻었습니다. (goods_1 = '가방'을 포함하고 있는 order_no 추출)
| order_no | id | dist_1 | goods_1 |
| 1000 | AA | 회원 | 가방 |
| 1000 | AA | 회원 | 휴대폰 |
| 1000 | AA | 회원 | 책상 |
| 2000 | BB | 비회원 | 가방 |
| 3000 | CC | 회원 | 가방 |
저는 이 상태에서 order_no의 unique한 count '3'을 도출하고 싶은데, 추가로 어떻게 구문을 작성하면 될까요?
with
temp as
(
select ...
(case when ....then..else... end) as dist_1,
... as goods_1,
from ...
where ...
)
, temp_2 as
(
select *
from temp gg
where (dist_1 = '회원 or dist_1 = '비회원' )and
gg.order_no in (select order_no from temp where goods_1 = '가방')
order by gg.order_no
)
select ...
from temp_2
where ...
가방이 포함된 주문의 주문내역을 뽑을 때는 위와 같은 단계를 거쳐야 할 수 있지만.
가방이 포함된 주문의 주문내역의 유니크한 주문번호 카운트를 구한다면?
주문 내역은 불필요해 보입니다.
그냥 가방의 주문번호만 카운트 하면 될 것 같네요.
SELECT COUNT(DISTINCT order_no) cnt
FROM temp
WHERE dist_1 IN ('회원', '비회원')
AND goods_1 = '가방'
;