需要知道Oracle sql中的输出
Country Code
USA 23, 24, 25, 30
Canada 40, 50, 70
输出要像这样显示
Country Code
USA 23
USA 24
USA 25
USA 30
Canada 40
Canada 50
Canada 70
您可以尝试使用connect by
与instr
获取,
组号到水平,然后使用regexp_substr
获取字符串。
Oracle 11g R2架构设置:
CREATE TABLE T(
Country varchar(50),
Code varchar(50)
);
INSERT INTO T VALUES ('USA','23, 24, 25, 30');
INSERT INTO T VALUES ('Canada','40, 50, 70');
查询1:
select distinct Country,trim(regexp_substr(Code, '[^,]+', 1, level)) Code
FROM T
connect by instr(Code, ',', 1, level - 1) > 0
ORDER BY Country desc
| COUNTRY | CODE |
|---------|------|
| USA | 23 |
| USA | 24 |
| USA | 25 |
| USA | 30 |
| Canada | 40 |
| Canada | 50 |
| Canada | 70 |