前几天解答的题目
把下面例子中fq字段按每个字符来分组拆分
原始数据
SEASON | MONTH | CODE | NO | FQ | DAYS |
17S_A | 4 | AB | 8001 | 1234567 | 7 |
18S_A | 4 | CD | 8002 | 246 | 3 |
19S_A | 5 | EF | 8003 | 15 | 2 |
要实现的结果
SEASON MONTH CODE NO FQ
17S_A 4 AB 8001 1
17S_A 4 AB 8001 2
17S_A 4 AB 8001 3
17S_A 4 AB 8001 4
17S_A 4 AB 8001 5
17S_A 4 AB 8001 6
17S_A 4 AB 8001 7
18S_A 4 CD 8002 2
18S_A 4 CD 8002 4
18S_A 4 CD 8002 6
19S_A 5 EF 8003 1
19S_A 5 EF 8003 5
SQL:
- with d as
- (select '17S_A' as season,
- 4 as month,
- 'AB' as code,
- 8001 as no,
- 1234567 as fq,
- 7 as days
- from dual
- union all
- select '18S_A' as season,
- 4 as month,
- 'CD' as code,
- 8002 as no,
- 246 as fq,
- 3 as days
- from dual
- union all
- select '19S_A', 5, 'EF', 8003, 15, 2 from dual)
- select season, month, code, no, substr(fq, level, 1) fq
- from d
- connect by level <= length(fq)
- and season = prior season
- and month = prior month
- and code = prior code
- and no = prior no
- and prior dbms_random.value is not null;
下面是按照两个字节拆分的例子
- with d as
- (select '17S_A' as season,
- 4 as month,
- 'AB' as code,
- 8001 as no,
- 1234567 as fq,
- 7 as days
- from dual
- union all
- select '18S_A' as season,
- 4 as month,
- 'CD' as code,
- 8002 as no,
- 246 as fq,
- 3 as days
- from dual
- union all
- select '19S_A', 5, 'EF', 8003, 15, 2 from dual)
- select season, month, code, no, substr(fq, 2 * (level - 1) + 1, 2) fq
- from d
- connect by level < length(fq) / 2 + 1
- and season = prior season
- and month = prior month
- and code = prior code
- and no = prior no
- and prior dbms_random.value is not null;
结果:
SEASON | MONTH | CODE | NO | FQ |
17S_A | 4 | AB | 8001 | 12 |
17S_A | 4 | AB | 8001 | 34 |
17S_A | 4 | AB | 8001 | 56 |
17S_A | 4 | AB | 8001 | 7 |
18S_A | 4 | CD | 8002 | 24 |
18S_A | 4 | CD | 8002 | 6 |
19S_A | 5 | EF | 8003 | 15 |