有没有像listunagg功能甲骨文这样的事情? 举例来说,如果我有一个数据,如:
------------------------------------------------------------
| user_id | degree_fi | degree_en | degree_sv |
--------------------------------------------------------------
| 3601464 | 3700 | 1600 | 2200 |
| 1020 | 100 | 0 | 0 |
| 3600520 | 100,3200,400 | 1300, 800, 3000 | 1400, 600, 1500 |
| 3600882 | 0 | 100 | 200 |
--------------------------------------------------------------
我想显示这样的数据:
-----------------------------------------------
| user_id | degree_fi | degree_en | degree_sv |
-----------------------------------------------
| 3601464 | 3700 | 1600 | 2200 |
| 1020 | 100 | 0 | 0 |
| 3600520 | 100 | 1300 | 1400 |
| 3600882 | 0 | 100 | 200 |
| 3600520 | 3200 | 800 | 600 |
| 3600520 | 400 | 3000 | 1500 |
-----------------------------------------------
我试图找到像LISTAGG相反一些功能,但找不到任何。 提前致谢 :-)
作为@be现在这里的评论已经指出的Oracle不提供这样的功能。 因此,作为一个快速的解决方法,你可以写类似的查询:
with t1(user_id, degree_fi, degree_en, degree_sv) as
(
select 3601464, '3700', '1600', '2200' from dual union all
select 1020 , '100' , '0' , '0' from dual union all
select 3600520, '100,3200,400', '1300, 800, 3000', '1400, 600, 1500' from dual union all
select 3600882, '0', '100', '200' from dual
),
Occurence(ocr) as(
select Level as ocr
from (select max(greatest(regexp_count(degree_fi, '[^,]+')
, regexp_count(degree_en, '[^,]+')
, regexp_count(degree_sv, '[^,]+')
)
) mx
from t1
)
connect by level <= mx
)
select *
from (
select User_id
, regexp_substr(degree_fi, '[^,]+', 1, o.ocr) as degree_fi
, regexp_substr(degree_en, '[^,]+', 1, o.ocr) as degree_en
, regexp_substr(degree_sv, '[^,]+', 1, o.ocr) as degree_sv
from t1 t
cross join Occurence o
)
where degree_fi is not null
or degree_en is not null
or degree_sv is not null
结果:
User_Id Degree_Fi Degree_En Degree_Sv
------------------------------------------------------------
3601464 3700 1600 2200
1020 100 0 0
3600520 100 1300 1400
3600882 0 100 200
3600520 3200 800 600
3600520 400 3000 1500
有listunagg
功能提供OraOpenSource utils的包集合。 这也是行之有效的。
要unagg列表考虑一下汤姆Oracle的说“问汤姆”看到http://www.oracle.com/technetwork/issue-archive/2007/07-mar/o27asktom-084983.html代码3或4。
汤姆不讨论我的优先选择有利于短字符串(<34个字符)。 我使用Oracle DBMS_UTILITY.comma_to_table功能。 例:
SET SERVEROUTPUT ON
DECLARE
/** test data **/
L_LIST1 VARCHAR2(500) := '"A","B","C","Pierre - Andre","D","E","OFVampFVapos;CBryan","F","G","H","I","J"';
l_list2 VARCHAR2(500);
l_tablen BINARY_INTEGER;
l_tab DBMS_UTILITY.uncl_array;
BEGIN
DBMS_OUTPUT.put_line('l_list1 : ' || l_list1);
DBMS_UTILITY.comma_to_table (
list => l_list1,
tablen => l_tablen,
tab => l_tab);
FOR i IN 1 .. l_tablen LOOP
DBMS_OUTPUT.put_line(i || ' : ' || l_tab(i));
END LOOP;
DBMS_UTILITY.table_to_comma (
tab => l_tab,
tablen => l_tablen,
list => l_list2);
DBMS_OUTPUT.put_line('l_list2 : ' || l_list2);
end;