我有一个表格,其中包含以逗号分隔的名称列表。我的目标是将它们分开。
Room | Name
room1 | Anne,Amy
room2 | Ben,Bryan
我的目标:
Room | Name
room1 | Anne
room1 | Amy
room2 | Ben
room2 | Bryan
我已经阅读了一些关于如何将字符串拆分为行的解决方案,但是有替代方法可以在Oracle 8i上运行。我已经按照一些文章将它们拆分成这样的行:
create or replace function str2tbl( p_str IN varchar2 , p_delimiter in varchar2) return mytabletype
as
l_str long default p_str || p_delimiter;
l_n number;
l_data mytabletype := mytabletype();
begin
loop
l_n := instr( l_str, p_delimiter );
exit when (nvl(l_n,0) = 0);
l_data.extend;
l_data( l_data.count ) := ltrim(rtrim(substr(l_str,1,l_n-1)));
l_str := substr( l_str, l_n+1 );
end loop;
return l_data;
end str2tbl;
然后我从我的桌子做一个SELECT,如下所示:
select * from the ( select cast(str2tbl( Name, ',' ) as mytableType )
from SPLITSTRING);
并获得了以下结果,但无法显示Room列的值:
Name
Anne
Amy
Ben
Bryan
有没有办法在Oracle 8i中拆分行?
答案 0 :(得分:1)
您可以通过hierarchical queries和字符串操作函数尝试使用稍微不同的方法,无需函数。 有点棘手,但这应该有效:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="16dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<!-- This is the image you want to add -->
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<!-- Toolbar to just hold the action icons -->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<!-- Nested scroll to make the content under the bar scrolls and move up and down the collapsing toolbar -->
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!-- YOUR CONTENT -->
<include layout="@layout/content_detail" />
</android.support.v4.widget.NestedScrollView>
答案 1 :(得分:0)
我不确定8i是否支持这种功能,我已经在12c中对此进行了测试,并且工作正常:
create table test (room varchar2(20), names varchar2(40));
表&#39;测试&#39;创建
insert into test values ('room1', 'anne');
INSERT INTO测试成功 1行受影响
insert into test values ('room2', 'amy,sheldon');
INSERT INTO测试成功 1行受影响
insert into test values ('room3', 'penny,leonard');
INSERT INTO测试成功 1行受影响
使用XMLTABLE:
SELECT room,
trim(COLUMN_VALUE) names
FROM test,
xmltable(('"'
|| REPLACE(names, ',', '","')
|| '"'))
/
房间| NAMES
room1 |安
room2 |艾米
room2 |谢尔顿
room3 |便士
room3 |伦纳德
答案 2 :(得分:0)
这是一种不使用connect by
的替代方法drop table pivot_t;
drop table rooms;
create table rooms (Room varchar2(30), Persons varchar2(30));
insert into rooms values ('room1', 'Anne,Amy');
insert into rooms values ('room2', 'Ben,Bryan,Paul');
insert into rooms values ('room3', 'John,Michael,Nik,Patrick');
create table pivot_t(num integer);
begin
for i in 1..10000 loop
insert into pivot_t values(i);
end loop;
end;
/
commit;
select
room
,substr(Persons, start_pos, case
when
next_comma - start_pos < 0 then 999
else
next_comma - start_pos
end)
from
(
select r.room
,r.persons
,nvl(instr(r.Persons,',',1,decode(pt.num-1,0,null,pt.num-1) ),0) +1 START_POS
,instr(r.Persons,',',1,pt.num) NEXT_COMMA
from rooms r
,pivot_t pt
where length(r.Persons) - length(replace(r.Persons,',')) +1 >= pt.num
order by r.room, pt.num
)
;