如何在一行输出中显示多行值
How to displaying multiple rows values in a one line of output
我有这个查询:
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的记录。
- 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;
你可以使用 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;
我有这个查询:
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的记录。
- 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;
你可以使用 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;