DBMS 2

DA, SQL, DB보안 등 실무자를 위한 위한 DB기술 바이블!

SQL 및 Function Tip

DBMS 2
DB2 가이드
DB2 UDB SQL 가이드
SQL 및 Function Tip
작성자
admin
작성일
2021-02-19 15:11
조회
1439

SQL 및 Function Tip

SQL 및 Function Tip

DB2 UDB의 SQL을 다음과 같은 요건에 쉽게 사용 할 수 있습니다.


Delete 수행 후 delete 전 data 보기

Table의 data 삭제 후 삭제된 data를 보고자 할 경우의 예입니다.


Select empno from old table ( delete from ( select * from employee
order by empno
fetch first row only ) );

Update 후 update 된 row 보기

Table의 data를 update한 후 update 된 row를 보고자 할 경우의 예입니다.


Select comm. From new table ( update employee set comm. = - comm. )

중복된 row중에서 한 row를 제외한 모든 row 삭제하기

Table의 data를 update한 후 update 된 row를 보고자 할 경우의 예입니다.


Delete from ( select rownumber() over (partition by ar_id order by sa_id ) as rn fromar ) where rn > 1;

insert into ar_dup
( select rownumber() over (partition by ar_id order by sa_id ) as rn
from ar ) where rn > 1;

중복되었는지 아닌지 기준이 되는 column은 partition by 절 다음에 기술하고 이 중복된 data에 대해 특정 column으로 순번을 정하고 할 경우 order by절 다음에 column을 기술합니다. 이렇게 return된 값 중에서 첫 번째 data만 남기고 삭제하고자 할 경우 상위 SQL절에서 rn > 1과 같이 순위에 대한 제어를 합니다.


subselect 절에서의 row 건수 제안하기

subselect 절에서 return되는 값을 제한하고자 할 경우 subselect 절 내에서 fetch first n rows only 절을 기술합니다.


Select temp.projno, temp.projname, emp.name
From ( select projno, projname, repemp from project
Where deptno = 'D11'
Fetch first 5 rows only ) temp, employee emp
Where temp.repemp = emp.emp_id

correlated delete 하기

다른 table과 join하여 조건을 만족시키는 값만 table에서 delete를 하고자 할 경우의 예입니다.


delete from inv i
where exists ( select 1 from sales s where i.itemno = s.itemno );

correlated update 하기

다른 table과 join하여 조건을 만족시키는 값만 update 하거나 만족시키지 않는 값만 update하는 경우의 예입니다.


update pltstag.ip_cust_mapp_info a
set a.icust_id = ( select b.icust_id from pltstag.tb_icstcustmap b
where a.src_cust_id = b.cust_id
and b.op_sys_cd = '1' )

update pltstag.ip_cust_mapp_info a
set a.icust_id = ' '
where not exists ( select 1 from pltstag.tb_icstcustmap b
where a.src_cust_id = b.cust_id
and b.op_sys_cd = '1' )

Bill of Material (BOM)

Recursive SQL을 사용하고자 할 경우의 예입니다.


WITH RPL (LEVEL, PART, SUBPART, QUANTITY) AS
(
SELECT 1, ROOT.PART, ROOT.SUBPART, ROOT.QUANTITY
FROM PARTLIST ROOT
WHERE ROOT.PART = '01'
UNION ALL
SELECT PARENT.LEVEL+1, CHILD.PART, CHILD.SUBPART, CHILD.QUANTITY
FROM RPL PARENT, PARTLIST CHILD
WHERE PARENT.SUBPART = CHILD.PART
AND PARENT.LEVEL < 2
)
SELECT PART, LEVEL, SUBPART, QUANTITY
FROM RPL

Outer Join

Left outer join을 사용하고자 할 경우의 예입니다. Join predicate은 on절에 기술합니다. where절은 두 table간의 outer join을 수행한 결과에 대한 조건을 기술합니다. Left outer join에서 left table에 해당하는 조건은 on 절에 기술하고 right table에 해당하는 조건은 inline view를 통해 먼저 읽어오도록 한 후 left outer join을 하도록 한다.


SELECT A.last_name,A.id,B.name
FROM emp A LEFT OUTER JOIN customer B
ON A.id = B.sales_rep_id
WHERE B.cust_id = 'AAA'

current timestamp, time, date 보기

values 문을 통해 시스템 시간을 볼 수 있으며 sysibm.sysdummy1 table에 대한 query를 통해서도 볼 수 있습니다. Values 문을 사용하는 것을 권고합니다.


Values ( current timestamp )
Values ( current time )
Values ( current date )
Select current timestamp, current time, current date from sysibm.sysdummy1

1) 현재 날짜에서 '-' 을 없애고자 할 경우, 2) 현재날짜와 시간 초만 알고 싶은 경우의 예입니다.


Values ( current date ) -> 2001-01-211)
values ( hex(current date) ) -> 200401212)
values (substr(hex(current timestamp),1,14)) -> 20040121135656

date를 character로 conversion 하고자 할 경우의 예입니다.


Values (char( current date, iso ) -> 2004-01-21
Values (char( current date, usa) -> 01/21/2004
Values (char( current date, local ) -> 01/21/2004

character를 date를 conversion 하고자 할 경우의 예입니다.


Values ( insert(insert('20040120',5,0,'-'),8,0'-'))

10 null 값 처리

select문으로부터 null값이 return될 때 원한 값으로 대체하고자 할 경우의 예입니다. Salary가 null이면 0을 return합니다.


Select empno, value(salary, 0) from employee

data truncate

많은 row가 들어있는 table의 data를 전부 삭제하고자 할 경우 아래 3가지 중 하나를 사용할 수 있습니다. Import를 사용할 것을 권고합니다.


Import from /dev/null of del replace into itg.ar
Load from /dev/null of del replace into irg.ar
Alter table itg.ar activate not logged initially with empty table