2016-04-01 67 views
0

我有3個表:table_A,table_B和table_C。 Table_A有一個主鍵,並由來自table_B的外鍵引用。 Table_C有一個由來自table_B的外鍵引用的主鍵。設計是這樣的:如何在Oracle查詢上使用listagg的條件

Table_A: ID_A TextData

Table_B: ID_B ID_A ID_C

Table C: ID_C TextData

查詢:

select a.id_a, a.textdata, listagg(c.textdata, ', ') within group (order by c.id_c) data_c from table_a a inner join table_b b on (a.id_a = b.id_a) inner join table_c c on (b.id_c = c.id_c) where c.textdata like :data group by a.id_a, a.textdata;

我剛剛得到c.textdata的一個表,而不是數據來自listagg。如果我嘗試使用此查詢:

select a.id_a, a.textdata, listagg(c.textdata, ', ') within group (order by c.id_c) data_c from table_a a inner join table_b b on (a.id_a = b.id_a) inner join table_c c on (b.id_c = c.id_c) where listagg(c.textdata, ', ') like :data group by a.id_a, a.textdata;

我得到一個錯誤,如:組功能在這裏不允許使用

如何給上LISTAGG參數的條件,因爲我需要從數據LISTAGG?

回答

1

您可以嘗試包裹查詢:

select * 
from (
     select a.id_a, 
      a.textdata, 
      listagg(c.textdata, ', ') within group (order by c.id_c) data_c 
     from table_a a 
      inner join table_b b on (a.id_a = b.id_a) 
      inner join table_c c on (b.id_c = c.id_c) 
     group by a.id_a, a.textdata 
    ) 
where data_c like :data 

或使用具有對由LISTAGG了領域適用條件:

select a.id_a, 
     a.textdata, 
     listagg(c.textdata, ', ') within group (order by c.id_c) data_c 
from table_a a 
     inner join table_b b on (a.id_a = b.id_a) 
     inner join table_c c on (b.id_c = c.id_c) 
group by a.id_a, a.textdata 
having listagg(c.textdata, ', ') within group (order by c.id_c) like :data 
+0

我試圖運行查詢和我得到的結果類似這:「data_c」:無效的標識 – user3505775

+0

對不起,我有錯誤的條件。感謝您的支持:D – user3505775