如果您不限制使用ListAgg,我可能會建議您使用沒有分析功能的其他解決方案。我使用集合來讓外部應用程序像字符串數組那樣傳遞狀態('AB','AM')。該函數將結果集作爲嵌套表返回。
初步類型聲明:
create or replace type t_test_states is varray(10) of varchar2(2);
create or replace type t_test_appnumbers is table of number;
創建一個函數(在現實生活中,你不會有一個子查詢「ample_data」塊):
create or replace function TEST_GET_ANSWER (p_states IN t_test_states)
return t_test_appnumbers
is
v_resultset t_test_appnumbers;
begin
with sample_data as (select 123 appnumber, 'AB' decision from dual union all
select 345 appnumber, 'BC' decision from dual union all
select 123 appnumber, 'PA' decision from dual union all
select 345 appnumber, 'PA' decision from dual union all
select 123 appnumber, 'AM' decision from dual)
select appnumber bulk collect into v_resultset
from (
select appnumber
from sample_data
where decision in (select column_value from table(p_states))
group
by appnumber
, decision
)
group
by appnumber
--the next condition ensures that appnumber has exactly ALL states
having count(*) = (select count(column_value) from table(p_states));
return v_resultset;
end TEST_GET_ANSWER;
使用該函數是這樣的:
嗨, - 這並沒有多大意義。你是說,如果appNumber 123有決定字符串BA和MC,並且級聯是BAMC,那麼這個字符串是否有子字符串AM? (如果這不是你所說的,並且AM必須是Decision中的值或Decision列中值的子字符串,則可以獨立於LISTAGG進行測試)。如果實際上你想讓BAMC導致AM的匹配,那麼...... LISTAGG取決於聚合的順序,但是你沒有提到任何關於這一點的內容。 MCBA,這是另一種可能性,不包含子字符串AM。 – mathguy
@mathguy你有一點,我給了我的例子。然而在現實生活中這不會發生。抱歉造成混淆。同樣使用LISTAGG,我會用逗號分隔它們。而且它不是該列的子字符串。這是它的一切:) – SeeWe