반응형



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* 
    계층형 쿼리 
        : 오라클에서만 제공됨
*/
 
create table bom_sphone(
    item_id number(3not null,
    parent_id number(3),
    item_name varchar2(20not null,
    primary key (item_id)
);
 
 
insert into bom_sphone values(100,null,'스마트폰');
insert into bom_sphone values(101,100,'메인pc');
insert into bom_sphone values(102,100,'배터리');
insert into bom_sphone values(103,101,'cpu');
insert into bom_sphone values(104,101,'메모리');
insert into bom_sphone values(105,101,'블루투스');
 
 
select * from bom_sphone;
 
 
 
select  s1.item_name, s1.item_id,s2.item_name parent_name
from bom_sphone s1, bom_sphone s2
where s1.parent_id = s2.item_id (+)
order by s1.item_id;
 
 
 
/* start with, connect by절을 이용한 계층형 쿼리를 할 수 있다 */
 
select lpad('*'2*(level-1)) || item_name itemnames
from bom_sphone
start with parent_id is null /* 최상위 nod지정 */
connect by prior item_id = parent_id; 
 
 
 
/* 
    prior는 부모열을 찾아주는 의미 
    = connect by parent_id = prior item_id
*/
 
select level, lpad(' '4*(level-1)) || first_name||' '||last_name "이름"
from employees
start with manager_id is null /* 부모가 없으므로 최상위가 됨 */
connect by manager_id = prior employee_id;
 
 
select job.job_title, lpad(' '4*(level-1)) || emp.first_name||' '||emp.last_name "이름"
from employees emp, jobs job
where emp.job_id = job.job_id
start with emp.manager_id is null 
connect by emp.manager_id = prior emp.employee_id;
cs








반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] PL/SQL 변수 선언 및 데이터 타입  (0) 2017.09.06
[Oracle SQL] PL/SQL  (0) 2017.08.21
[Oracle SQL] Sequence  (0) 2017.08.16
[Oracle SQL] VIEW  (0) 2017.08.16
[Oracle SQL] 무결성 제약조건  (0) 2017.08.12

+ Recent posts