一列中的所有行值

时间:2012-07-18 09:58:36

标签: oracle plsql oracle11g plsqldeveloper

我想在一列中显示所有值。我怎么能这样做?

数据如下所示:

-----------------------------------------------
| user_id | degree_fi | degree_en | degree_sv |
-----------------------------------------------
| 3601464 | 3700      |  1600     |  2200     |
|  1020   | 100       |  0        |   0       |
| 3600520 |  100      | 1300      |  1400     |
| 3600882 |  0        |   100     |  200      |
| 3600520 |  3200     |   800     |  600      |
| 3600520 |  400      | 3000      |  1500     |
-----------------------------------------------

我想要的是:

-------------------------------------------------------------
| user_id | degree_fi    | degree_en       | degree_sv       |
--------------------------------------------------------------
| 3601464 | 3700         |  1600           |  2200           |
|  1020   | 100          |  0              |   0             |
| 3600520 | 100,3200,400 | 1300, 800, 3000 | 1400, 600, 1500 |
| 3600882 |  0           |   100           |  200            |
--------------------------------------------------------------

正如您所看到的,3600520的值不仅在一个组中,而且在一个列中也是如此。我怎么能这样做?

提前致谢

create table USER_MULTI_DEGREE
(
  USER_ID   INTEGER not null,
  DEGREE_FI VARCHAR2(128),
  DEGREE_EN VARCHAR2(128),
  DEGREE_SV VARCHAR2(128)
);
insert into USER_MULTI_DEGREE (USER_ID, DEGREE_FI, DEGREE_EN, DEGREE_SV) values (3601464, '3700', '1600', '2200');
insert into USER_MULTI_DEGREE (USER_ID, DEGREE_FI, DEGREE_EN, DEGREE_SV) values (1020, '100', '0', '0');
insert into USER_MULTI_DEGREE (USER_ID, DEGREE_FI, DEGREE_EN, DEGREE_SV) values (3600520, '100', '1300', '1400');
insert into USER_MULTI_DEGREE (USER_ID, DEGREE_FI, DEGREE_EN, DEGREE_SV) values (3600882, '0', '100', '200');
insert into USER_MULTI_DEGREE (USER_ID, DEGREE_FI, DEGREE_EN, DEGREE_SV) values (3600520, '3200', '800', '600');
insert into USER_MULTI_DEGREE (USER_ID, DEGREE_FI, DEGREE_EN, DEGREE_SV) values (3600520, '400', '3000', '1500');

3 个答案:

答案 0 :(得分:5)

如果您使用的是11g R2,则可以使用内置的listagg()函数:

select user_id, listagg(degree_fi, ',') within group (order by degree_fi)
from user_Multi_degree
group by user_id 

如果您使用11g R1,则必须为此定义自己的类型 - 有关示例,请参阅AskTom: stragg function

答案 1 :(得分:1)

由于你是11g,假设它是11gR2,你可以使用内置的listagg()功能;这是弗兰克施密特答案略有变化的分析版本:

select distinct user_id,
    listagg(degree_fi, ',') within group (order by degree_id)
        over (partition by user_id) as degree_fi,
    listagg(degree_en, ',') within group (order by degree_id)
        over (partition by user_id) as degree_en,
    listagg(degree_sv, ',') within group (order by degree_id)
        over (partition by user_id) as degree_sv
from user_multi_degree;

   USER_ID DEGREE_FI            DEGREE_EN            DEGREE_SV
---------- -------------------- -------------------- --------------------
      1020 100                  0                    0
   3600520 100,3200,400         1300,800,3000        1400,600,1500
   3600882 0                    100                  200
   3601464 3700                 1600                 2200

答案 2 :(得分:0)

为SQL创建并使用内联函数。 根据表的大小,可能会导致性能问题。如果是这种情况,您也可以考虑使用物化视图。

忘记 - LISTAGG功能。

listagg function in 11g release 2