sql server 순서 쿼리 질문 0 1 579

by ahriow [SQL Query] sql server [2022.01.07 14:31:06]


with a as(
select 1 id , 'A' product, 0 depth union all
select 2 id , 'B' product, 1 depth union all
select 3 id , 'C' product, 2 depth union all
select 4 id , 'D' product, 2 depth union all
select 5 id , 'E' product, 2 depth union all
select 6 id , 'F' product, 1 depth union all
select 7 id , 'G' product, 2 depth union all
select 8 id , 'H' product, 2 depth union all
select 9 id , 'I' product, 0 depth union all
select 10 id , 'J' product, 1 depth union all
select 11 id , 'K' product, 2 depth union all
select 12 id , 'A' product, 2 depth union all
select 13 id , 'B' product, 2 depth union all
select 14 id , 'L' product, 1 depth union all
select 15 id , 'E' product, 2 depth union all
select 16 id , 'D' product, 2 depth 
), b as (
select 'A' bundle_product , 'B' product, 1 sort union all
select 'A' bundle_product , 'F' product, 2 sort union all
select 'B' bundle_product , 'C' product, 1 sort union all
select 'B' bundle_product , 'D' product, 2 sort union all
select 'B' bundle_product , 'E' product, 3 sort union all
select 'F' bundle_product , 'H' product, 1 sort union all
select 'F' bundle_product , 'G' product, 2 sort union all
select 'I' bundle_product , 'J' product, 1 sort union all
select 'I' bundle_product , 'L' product, 2 sort union all
select 'J' bundle_product , 'A' product, 1 sort union all
select 'J' bundle_product , 'B' product, 2 sort union all
select 'J' bundle_product , 'K' product, 3 sort union all
select 'L' bundle_product , 'D' product, 1 sort union all
select 'L' bundle_product , 'E' product, 2 sort 
)

테이블 A 

id       product depth
1 A 0
2 B 1
3 C 2
4 D 2
5 E 2
6 F 1
7 G 2
8 H 2
9 I 0
10 J 1
11 K 2
12 A 2
13 B 2
14 L 1
15 E 2
16 D 2

 

테이블 B

bundle_product product  sort
A B 1
A F 2
B C 1
B D 2
B E 3
F H 1
F G 2
I J 1
I L 2
J A 1
J B 2
J K 3
L D 1
L E 2

 

1. 0depth 끼리는 id 순서가 빠른것

2. bundle 테이블의 sort 순서대로

조회를 하고싶은데요. 

순서 product
1 A
2 B
3 C
4 D
5 E
6 F
7 H
8 G
9 I
10 J
11 A
12 B
13 K
14 L
15 D
16 E


최종 순서는 위와같이 나와야 하는데 방법을 모르겠습니다.

고수님들의 도움이 필요합니다

by 마농 [2022.01.10 09:35:39]

테이블 설계가 좀 이상합니다.
하나의 테이블로 설계했으면 좋을 듯 한데. 두개 테이블로 나뉘어져 있네요.
a 의 경우 코드가 중복이 되는데.
이게 b 와의 유니크한 연결고리를 정확하게 알 수가 없는 구조입니다.

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입