2016-08-01 61 views
0

的單行顯示多行的值我有這個疑問:如何輸出

SELECT table_1.user_code AS user, 
    table_1.charge_code, 
    table_1.unit_code, 
    table_3.roles_code, 
    table_2.apps_code 
FROM table_1 
INNER JOIN table_3 ON 
     table_1.user_code = table_3.Cod_Usr 
INNER JOIN table_2 ON 
     table_3.roles_code = table_2.roles_code 
WHERE 
     table_1.fec_baja IS NULL 
ORDER BY table_1.user_code ASC, table_3.roles_code, table_2.roles_code; 

我通常用它來了解,用戶根據自己的charge_code,unit_code和role_code

訪問的applications_code

而且,這是上面的查詢結果:

user_code charge_code unit_code role_code apps_code 
1 USER01  AAA001  111111111 BASICMENU APPS0001 
2 USER01  AAA001  111111111 BASICMENU APPS0005 
3 USER01  AAA001  111111111 BASICMENU APPS0008 
4 USER01  AAA001  111111111 BASICMENU APPS1245 
5 USER01  AAA001  111111111 LOGIN  APPS0013 
6 USER01  AAA001  111111111 LOGIN  APPS1291 
7 USER02  BBB0001  222222222 CASHIER001 APPS01SF 
8 USER02  BBB0001  222222222 CASHIER001 APPS12RE 
9 USER02  BBB0001  222222222 CASHIER001 APPS178E 
8 USER02  BBB0001  222222222 CASHIER001 APPSZS45 
9 USER02  BBB0001  222222222 CASHIER001 APPS12DF 
10 USER02  BBB0001  222222222 CASHIER001 APPS1RE5 
11 USER02  BBB0001  222222222 BASICMENU APPSTY45 
12 USER02  BBB0001  222222222 BASICMENU APPS1KJ5 

以前我是可以正常使用,但現在拿由於大量的記錄,超過10分鐘的運行時間。

我試圖得到以下結果:

user_code charge_code unit_code role_code apps_code 
1 USER01  AAA001  111111111 BASICMENU APPS0001,APPS0005,APPS0008,APPS1245 
2 USER01  AAA001  111111111 LOGIN  APPS0013,APPS1291 
3 USER02  BBB0001  222222222 CASHIER001 APPS01SF,APPS12RE,APPSZS45,APPS178E,APPS12DF,APPS1RE5 
4 USER02  BBB0001  222222222 BASICMENU APPSTY45,APPS1KJ5 

注:這些都是顯示每個表中的記錄。

  • 的TABLE_1:user_code/charge_code/unit_code
  • 的TABLE_2:role_code/apps_code
  • 的TABLE_3:role_code/user_code

我想實現這樣的查詢:

SELECT table_2.roles_code, wm_concat(table_2.apps_code) AS apps 
FROM table_2 
GROUP BY table_2.roles_code; 
+0

你爲什麼要在彙總(逗號分隔)格式輸出?也許有更有效的方法來做你最終需要做的事情。 – mathguy

+0

輸出格式不重要......我只需要一行中的所有值。 @mathguy – spikeTJ

+0

你能推薦我一個更有效的方法嗎? @mathguy – spikeTJ

回答

1

您可以使用LISTAGG

SELECT 
table_2.roles_code, 
LISTAGG(table_2.apps_code, ', ') WITHIN GROUP (ORDER BY table_2.apps_code) "apps code" 
FROM table_2 
GROUP BY able_2.roles_code 

,併爲您的查詢

SELECT table_1.user_code AS user, 
    table_1.charge_code, 
    table_1.unit_code, 
    table_3.roles_code, 
    table_2.roles_code, 
    LISTAGG(table_2.apps_code, ', ') WITHIN GROUP (ORDER BY table_2.apps_code) "apps code" 
FROM table_1 
INNER JOIN table_3 ON 
     table_1.user_code = table_3.Cod_Usr 
INNER JOIN table_2 ON 
     table_3.roles_code = table_2.roles_code 
WHERE 
     table_1.fec_baja IS NULL 
GROUP BY table_1.user_code ASC, table_3.roles_code, table_2.roles_code 
ORDER BY table_1.user_code ASC, table_3.roles_code, table_2.roles_code; 

與wm_concat()應該是

SELECT table_1.user_code AS user, 
    table_1.charge_code, 
    table_1.unit_code, 
    table_3.roles_code, 
    table_2.roles_code, 
    wm_concat(table_2.apps_code) "apps code" 
FROM table_1 
INNER JOIN table_3 ON 
     table_1.user_code = table_3.Cod_Usr 
INNER JOIN table_2 ON 
     table_3.roles_code = table_2.roles_code 
WHERE 
     table_1.fec_baja IS NULL 
GROUP BY table_1.user_code ASC, table_3.roles_code, table_2.roles_code 
ORDER BY table_1.user_code ASC, table_3.roles_code, table_2.roles_code; 
+0

但我如何加入到第一個查詢? @scaisEdge – spikeTJ

+0

我嘗試多次這種方式,但我得到了這個錯誤:ORA-00923:從關鍵字找不到預期的地方。我已經查看了語法並找不到任何錯誤。 @scaisEdge – spikeTJ

+0

我有這個版本的Oracle:Oracle數據庫10g企業版版本10.2.0.5.0 - 64bi。我認爲listagg適用於11.2或更高版本。 @scaisEdge – spikeTJ