안녕하세요 정말 오랜만에 질문을 하네요.
table
seq | id | type | goods
1 AK A 갤럭시
2 PS B 아이폰
3 BE B 냉장고
type이 A 일때에는 원래 값
->
1, AK, A, 갤럭시
그대로 표시되고
타입이 B 일때에는 값 하나에 5개씩 나오도록 하고 싶습니다.(4개의 임의의 값을 넣기위해)
->
2, PS, B, 아이폰 (원래 값)
2, PS, B, 텀블러 (임의의 값)
2, PS, B, 상품권 (임의의 값)
2, PS, B, ㅁㅁㅁ (임의의 값)
2, PS, B, ㄷㄷㄷ (임의의 값)
3, BE, B, 냉장고(원래 값)
3, BE, B, 텀블러 (임의의 값)
3, BE, B, 상품권 (임의의 값)
3, BE, B, ㅁㅁㅁ (임의의 값)
3, BE, B, ㄷㄷㄷ (임의의 값)
# 임의의 값의 상품은 항상 고정 되어있습니다.
젤위의 테이블을 기반으로 원하는 값은 이렇습니다. ↓
no | seq | id | type | goods
1, 1, AK, A, 갤럭시
2, 2, PS, B, 아이폰
3, 2, PS, B, 텀블러
4, 2, PS, B, 상품권
5, 2, PS, B, ㅁㅁㅁ
6, 2, PS, B, ㄷㄷㄷ
7, 3, BE, B, 냉장고
8, 3, BE, B, 텀블러
9, 3, BE, B, 상품권
10, 3, BE, B, ㅁㅁㅁ
11, 3, BE, B, ㄷㄷㄷ
WITH A AS ( select 1 seq,'AK' id, 'A' type, '갤럭시' goods from dual union all select 2 seq,'PS' id, 'B' type, '아이폰' goods from dual union all select 3 seq,'BE' id, 'B' type, '냉장고' goods from dual ),B AS ( SELECT LEVEL LV, LEVEL||'상품' RANDOM FROM DUAL CONNECT BY LEVEL<=5 ) SELECT A.SEQ,A.ID,A.TYPE ,CASE WHEN LV=1 THEN A.GOODS ELSE B.RANDOM END GOODS FROM A,B WHERE 1=1 AND B.LV<=DECODE(TYPE,'A',1,'B',5) ORDER BY SEQ,TYPE,LV
WITH data_t AS ( SELECT 1 seq, 'AK' id, 'A' type, '갤럭시' goods FROM dual UNION ALL SELECT 2, 'PS', 'B', '아이폰' FROM dual UNION ALL SELECT 3, 'BE', 'B', '냉장고' FROM dual ) , code_t AS ( SELECT 1 lv, '' goods FROM dual UNION ALL SELECT 2, '텀블러' FROM dual UNION ALL SELECT 3, '상품권' FROM dual UNION ALL SELECT 4, 'ㅁㅁㅁ' FROM dual UNION ALL SELECT 5, 'ㄷㄷㄷ' FROM dual ) SELECT ROW_NUMBER() OVER(ORDER BY a.seq, b.lv) rn , a.seq , a.id , a.type , b.lv , NVL(b.goods, a.goods) goods FROM data_t a , code_t b WHERE b.lv <= DECODE(a.type, 'B', 5, 1) ;
WITH data_t AS ( SELECT 1 seq, 'AK' id, 'A' good_type, '갤럭시' goods FROM dual UNION ALL SELECT 2, 'PS', 'B', '아이폰' FROM dual UNION ALL SELECT 3, 'BE', 'B', '냉장고' FROM dual ) , code_t AS ( SELECT 1 lv,'A' AS good_type, '' goods FROM dual UNION ALL SELECT 1,'B', '' FROM dual UNION ALL SELECT 2,'B', '텀블러' FROM dual UNION ALL SELECT 3,'B', '상품권' FROM dual UNION ALL SELECT 4,'B', 'ㅁㅁㅁ' FROM dual UNION ALL SELECT 5,'B', 'ㄷㄷㄷ' FROM dual ) SELECT seq, ID, b.good_type, nvl(b.goods,a.goods) AS goods FROM data_t a LEFT OUTER JOIN code_t b ON a.good_type = b.good_type ORDER BY seq, lv