一个oracle中的SQL表达式

来源:百度知道 编辑:UC知道 时间:2024/07/15 02:53:24
表的格式如下:
列名 y1 y2 y3
a a1 1
a a2 2
b b1 1
b b2 2
c c1 1
c c2 2

我想直接selece 出这种排列

a1 a2
b1 b2
c1 c2

目前还没想到怎么做。

select y21,y22
from
(select y1,y2 as y21 from 表 where y3 = '1') a ,
(select y1,y2 as y22 from 表 where y3 = '2') b
where a.y1 = b.y1

select (select y2 from test as a where a.y1 = c.y1 order by a.y3 limit 1),(select y2 from test as b where b.y1 = c.y1 order by b.y3 desc limit 1) from test as c order by substring(c.y1,1,2)

自连接

这个不是用一条SQL就能实现的问题,首先字段的列数就不固定。
oracle中的SQL语句,不用指定具体列数的选择列表只有用*通配符。
显然是不能实现的。
说到底这是个行列转至的问题,
可以用PL/SQL实现。
步骤1.取得所需数据。
2.转至处理后放入临时表中。
3.从临时表中取得数据。(当然要用*通配符)