A테이블
num
1
2
3
4
5
B테이블
num seq category
1 1 a
1 2 b
3 1 b
3 2 c
3 3 a
3 4 a
이거를 outer join 해서 a인것만 뽑고 싶은데 한 num에 a가 두개이상이면 그 행이 다 나오더라구요
밑에처럼 나오게 할 수 있는 방법 없을까요
select * from a left outer join b on a.num=b.num and b.category='a'
이건 제가 짠 쿼리구여.. 밑에처럼 나오고싶어요
num category
1 a
2 null
3 a
4 null
5 null
Comment 1
-
Terry
2015.03.18 16:48
with A As
(
Select 1 As num Union All
Select 2 As num Union All
Select 3 As num Union All
Select 4 As num Union All
Select 5 As num
)
,B As
(
Select 1 As num ,1 As seq ,'a' As category Union All
Select 1 As num ,2 As seq ,'b' As category Union All
Select 3 As num ,1 As seq ,'b' As category Union All
Select 3 As num ,2 As seq ,'c' As category Union All
Select 3 As num ,3 As seq ,'a' As category Union All
Select 3 As num ,4 As seq ,'a' As category
)
Select
a.num
,ISNULL(b.category,null) As category
From A As a
Left Outer Join
(
Select
b.num As num
,b.category As category
From B As b
Where b.category = 'a'
Group By b.num
,b.category
) As b
On a.num = b.num