Obsidian/Recognition/Programing/MariaDB/정렬된 자료에서 빈칸채우기.md

44 lines
1.1 KiB
Markdown

- 정렬된 자료에서 특정기준의 Row에만 값이있고 정렬기준으로 같은 컬럼에 빈칸을 채워야하는경우
``` SQL
--데이터 형식
1 1 a a a
2 2
3 3
4 1 b b b
5 2
이런 형식의 데이터를 다음과 같이 변경
1 1 a a a
2 2 a a a
3 3 a a a
4 1 b b b
5 2 b b b
-- 정렬된 자료 기준컬럼을 이용하여 채워야할 내용 리스트업
-- create table tmp_titems as
select row_number() over(order by cast(seq as integer)) as rownum, seq -- 시작순번
, nvl(LEAD(seq ) OVER(order by cast(seq as integer)), 469) -1 as next_seq -- 마지막순번
, TableID, TableNm
from table_items ti
where no = 1
order by cast(seq as integer)
-- 필요컬럼에 Update
update table_items a,
(
select concat('TB_', lpad(rownum * 10, 4, '0')) as keyseq -- 임시키생성
, nvl(TableID,concat('TB_', lpad(rownum * 10, 4, '0'))) as tid -- 값이 없는경우 임시키로 대체
, TableNm
, seq
, next_seq
from tmp_titems ) b
set a.Table_seq = b.keyseq,
a.tableID = b.tid,
a.tableNm = b.tableNm
where a.seq >= b.seq
and a.seq <= b.next_seq
```