不知道爲什麼wm_concat
不適合你,但我懷疑你是在錯誤的級別或分組奇怪。
如果我設置一些假數據:
create table issues (id number);
create table owners (id number, first varchar2(10), middle varchar2(10),
last varchar2(10), org varchar2(3));
create table issue_owners (issue_id number, owner_id number);
insert into issues (id) values (1);
insert into issues (id) values (2);
insert into issues (id) values (3);
insert into owners (id, first, middle, last, org)
values (11, 'Sam', null, 'Smith', 'AAA');
insert into owners (id, first, middle, last, org)
values (12, 'Stan', null, 'Bird', 'BBB');
insert into owners (id, first, middle, last, org)
values (13, 'Nancy', null, 'Bird', 'BBB');
insert into owners (id, first, middle, last, org)
values (14, 'Mike', null, 'Owen', 'CCC');
insert into issue_owners (issue_id, owner_id) values (1, 11);
insert into issue_owners (issue_id, owner_id) values (2, 12);
insert into issue_owners (issue_id, owner_id) values (2, 13);
insert into issue_owners (issue_id, owner_id) values (3, 14);
...賦予相同的初始輸出爲您配對降取樣:
column issue_id format 9 heading "#"
column owner format a50 heading "Owner"
select i.id as issue_id,
o.first
|| case when o.middle is null then null else ' ' || o.middle end
|| ' ' || last || ', ' ||o.org as owner
from issues i
left join issue_owners io on io.issue_id = i.id
left join owners o on o.id = io.owner_id
order by issue_id, owner;
# Owner
-- --------------------------------------------------
1 Sam Smith, AAA
2 Nancy Bird, BBB
2 Stan Bird, BBB
3 Mike Owen, CCC
4 rows selected.
我可以用wm_concat
聚集的名字:
select issue_id,
replace(cast(wm_concat(owner_name) as varchar2(4000)), ',', ', ')
|| ', ' || owner_org as owner
from (
select i.id as issue_id,
o.first
|| case when o.middle is null then null else ' ' || o.middle end
|| ' ' || last as owner_name,
o.org as owner_org
from issues i
left join issue_owners io on io.issue_id = i.id
left join owners o on o.id = io.owner_id
)
group by issue_id, owner_org
order by issue_id, owner;
# Owner
-- --------------------------------------------------
1 Sam Smith, AAA
2 Stan Bird, Nancy Bird, BBB
3 Mike Owen, CCC
3 rows selected.
該replace
只是放在名稱,這是不完全相關的,我cast
ing到varchar2
,因爲wm_concat
返回clob
這會導致問題級聯org
。至少,這是一個clob
在11gR2 - 我沒有一個10g實例與wm_concat
可用,但我認爲它在早期版本返回varchar2
;如果是的話不需要cast
,這將是更喜歡:
select issue_id,
replace(wm_concat(owner_name), ',', ', ') || ', ' || owner_org as owner
from (
...
我不知道您的org
值從所以這可能是簡化來了,我不知道你想什麼如果org
與某人(而非問題或您的等價物)相關聯,並且問題有兩個擁有不同org
值的所有者,則會發生這種情況。
如果這不是讓你更近,那麼也許你可以發表你的查詢的簡化版本,與一些固定的數據替換長多臺部分,展示你是如何嘗試使用wm_concat
反對;或者你自己版本的示例數據 - 構建,顯示相同的行爲。
替代sys_connect_by_path
方法,建議Appleman1234,對於同樣的數據:
select issue_id,
ltrim(max(sys_connect_by_path(owner_name, ', '))
keep (dense_rank last order by curr), ', ')
|| ', ' || owner_org as owner
from (
select issue_id,
owner_name,
owner_org,
row_number() over (partition by issue_id order by owner_name) as curr,
row_number() over (partition by issue_id order by owner_name) - 1 as prev
from (
select i.id as issue_id,
o.first
|| case when o.middle is null then null else ' ' || o.middle end
|| ' ' || last as owner_name,
o.org as owner_org
from issues i
left join issue_owners io on io.issue_id = i.id
left join owners o on o.id = io.owner_id
)
)
group by issue_id, owner_org
connect by prev = prior curr and issue_id = PRIOR issue_id
start with curr = 1;
# Owner
-- --------------------------------------------------
1 Sam Smith, AAA
2 Nancy Bird, Stan Bird, BBB
3 Mike Owen, CCC
3 rows selected.
如果你最終使用,Appleman1234應該添加一個答案,我會刪除此部分,因爲他應該得到建議它的功勞!無論如何,我想嘗試它,我以前看過它,但沒有記住它...
沒有人回答,因爲我錯誤地問了嗎?我對唐的編輯有點困惑,可能他是這麼想的,因爲我的研究不夠深入? – user1983682
不,你問得很好,據我所知。有些人認爲每個關鍵詞都應該標註爲代碼,但我們到達這裏的時間很長,但與您無關。人們可能只是不知道答案,就是這樣。那麼,盡我所能提高可讀性並添加相關標籤,因爲我們正在談論用SQL來做它,對吧? –
是的,謝謝。 – user1983682