求解行列转换的问题

[复制链接]
查看11 | 回复4 | 2014-11-5 02:21:02 | 显示全部楼层 |阅读模式
有一张表 a,内容如下:ID NAME
-- ----
2 b
2 b1
3 c
1 a
1 a1
3 c1

要求得到如下结果
123
abc
a1b1 c1

回复

使用道具 举报

千问 | 2014-11-5 02:21:02 | 显示全部楼层
http://www.itpub.net/forum.php?mod=viewthread&tid=1017026
回复

使用道具 举报

千问 | 2014-11-5 02:21:02 | 显示全部楼层
创建表T,结构与数据如下:
SQL> desc t
Name
Null?Type
----------------------------------------- -------- ----------------------------
ID
NUMBER
NAME
VARCHAR2(2)
SQL> select *
2from t
3/
ID NAME
---------- ----
2 b
2 b1
3 c
1 a
1 a1
3 c1
6 rows selected.
SQL>
可以使用以下SQL,这个SQL勉强可以用,呵呵。。
SQL> column x format a5
SQL> column y format a5
SQL> column z format a5
SQL> select decode(rownum,2,translate(x,1,' '),x) x,
2 decode(rownum,2,translate(y,1,' '),y) y,
3 decode(rownum,2,translate(z,1,' '),z) z
4from(
5 select r,max(
6
case when id = 1 and r = 1
7
then to_char(id)
8
when id = 1 and r = 2
9
then name
10
when id = 1 and r = 3
11
then name
12
end) x,
13
max(
14
case when id = 2 and r = 1
15
then to_char(id)
16
when id = 2 and r = 2
17
then name
18
when id = 3 and r = 3
19
then name
20
end) y,
21
max(
22
case when id = 3 and r = 1
23
then to_char(id)
24
when id = 3 and r = 2
25
then name
26
when id = 3 and r = 3
27
then name
28
end) z
29 from t,(select level r from dual connect by rownum
回复

使用道具 举报

千问 | 2014-11-5 02:21:02 | 显示全部楼层
没有任何逻辑表明转换后的哪些列应该放在同一行。比如下面的搭配完全也是有可能的:
abc1
a1b1 c

回复

使用道具 举报

千问 | 2014-11-5 02:21:02 | 显示全部楼层
需求要写得简洁、清晰、无二义性,别人才好帮你
我的回答是:自行搜索,论坛里这类问题相当多了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行