SQL> desc test;
Name Type Nullable Default Comments
------- ------------ -------- ------- --------
COUNTRY VARCHAR2(20) Y
CITY VARCHAR2(20) Y
SQL> select * from test;
COUNTRY CITY
-------------------- --------------------
中国 台北
中国 香港
中国 上海
日本 东京
日本 大阪
要求得到如下结果集:
------- --------------------
中国 台北,香港,上海
日本 东京,大阪
SQL> select t.country,
2 MAX(decode(t.city,'台北',t.city',',NULL))
3 MAX(decode(t.city,'香港',t.city',',NULL))
4 MAX(decode(t.city,'上海',t.city',',NULL))
5 MAX(decode(t.city,'东京',t.city',',NULL))
6 MAX(decode(t.city,'大阪',t.city',',NULL))
7 from test t GROUP BY t.country
8 /
COUNTRY MAX(DECODE(T.CITY,'台北',T.CIT
-------------------- ------------------------------
中国 台北,香港,上海,
日本 东京,大阪,
create or replace function str_list( str_in in varchar2 )
--分类字段
return varchar2 is
str_list varchar2(4000) default null;
--连接后字符串
str varchar2(20) default null;
--连接符号
begin
for x in ( select TEST.CITY from TEST
where TEST.COUNTRY = str_in ) loop
str_list := str_list str to_char(x.city);
str := ', ';
end loop;
return str_list;
end;
;
使用:
SQL> select DISTINCT(T.country),
list_func1(t.country) from test t;
COUNTRY LIST_FUNC1(T.COUNTRY)
-------------------- ----------------
中国 台北, 香港, 上海
日本 东京, 大阪
SQL> select t.country,str_list(t.country)
from test t GROUP BY t.country;
COUNTRY STR_LIST(T.COUNTRY)
-------------------- -----------------------
中国 台北, 香港, 上海
日本 东京, 大阪
create or replace function str_list2
( key_name in varchar2,
key in varchar2,
coname in varchar2,
tname in varchar2 )
return varchar2
as
type rc is ref cursor;
str varchar2(4000);
sep varchar2(2);
val varchar2(4000);
cur rc;
begin
open cur for 'select 'coname'
from ' tname '
where ' key_name ' = :x '
using key;
loop
fetch cur into val;
exit when cur%notfound;
str := str sep val;
sep := ', ';
end loop;
close cur;
return str;
end;
SQL> select test.country,
2 str_list2('COUNTRY',
test.country, 'CITY', 'TEST') emplist
3 from test
4 group by test.country
5 /
COUNTRY EMPLIST
-------------------- -----------------
中国 台北, 香港, 上海
日本 东京, 大阪
SELECT country,max(substr(city,2)) city
FROM
(SELECT country,sys_connect_by_path(city,',') city
FROM
(SELECT country,city,country
rn rchild,country(rn-1) rfather
FROM
(SELECT test.country ,test.city,row_number() over
(PARTITION BY test.country ORDER BY
test.city) rn
FROM test))
CONNECT BY PRIOR rchild=rfather
START WITH rfather LIKE '%0')
GROUP BY country;
下面分步解析,有4个FROM,就有4次结果集的操作。
step 1 给记录加上序号rn
SQL> SELECT test.country ,test.city,row_number() over
(PARTITION BY test.country ORDER
BY test.city) rn
2 FROM test
3 /
COUNTRY CITY RN
-------------------- -------------------- ----------
日本 大阪 1
日本 东京 2
中国 上海 1
中国 台北 2
中国 香港 3
;
step 2 创造子节点父节点:
SQL> SELECT country,city,country
rn rchild,country(rn-1) rfather
2 FROM
3 (SELECT test.country ,
test.city,row_number() over
(PARTITION BY test.country ORDER
BY test.city) rn
4 FROM test)
5 /
日本 大阪 日本1 日本0
日本 东京 日本2 日本1
中国 上海 中国1 中国0
中国 台北 中国2 中国1
中国 香港 中国3 中国2
step 3 利用sys_connect_by_path生成结果集:
SELECT country,sys_connect_by_path(city,',') city
FROM
(SELECT country,city,country
rn rchild,country(rn-1) rfather
FROM
(SELECT test.country ,test.city,row_number()
over (PARTITION BY test.country ORDER BY
test.city) rn
FROM test))
CONNECT BY PRIOR rchild=rfather START WITH rfather LIKE '%0'
日本 ,大阪
日本 ,大阪,东京
中国 ,上海
中国 ,上海,台北
中国 ,上海,台北,香港
step 4 最终步骤,筛选结果集合:
SQL> SELECT country,max(substr(city,2)) city
2 FROM
3 (SELECT country,sys_connect_by_path(city,',') city
4 FROM
5 (SELECT country,city,countryrn rchild,country
(rn-1) rfather
6 FROM
7 (SELECT test.country ,test.city,row_number()
over (PARTITION BY test.country ORDER
BY test.city) rn
8 FROM test))
9 CONNECT BY PRIOR rchild=rfather START WITH rfather LIKE '%0')
10 GROUP BY country;
COUNTRY CITY
-------------------- -------
中国 上海,台北,香港
日本 大阪,东京
SQL> SELECT t.country,strcat(t.city)
FROM test t GROUP BY t.country;
COUNTRY STRCAT(T.CITY)
-------------------- ------------------
日本 东京,大阪
中国 台北,香港,上海
简单吧,和官方的函数一样的便捷高效。
函数:
CREATE OR REPLACE FUNCTION strcat(input varchar2 )
RETURN varchar2
PARALLEL_ENABLE AGGREGATE USING strcat_type;
TYPE:
create or replace type strcat_type as object (
cat_string varchar2(4000),
static function ODCIAggregateInitialize
(cs_ctx In Out strcat_type) return number,
member function ODCIAggregateIterate
(self In Out strcat_type,value in varchar2) return
number,
member function ODCIAggregateMerge
(self In Out strcat_type,ctx2 In Out strcat_type)
return number,
member function ODCIAggregateTerminate
(self In Out strcat_type,returnValue Out
varchar2,flags in number) return number
)