请教一个SQL

[复制链接]
查看11 | 回复7 | 2005-10-30 17:05:33 | 显示全部楼层 |阅读模式
a 表有3列,id,cate,name
见个视图
id,name1,name2,name3
其中name1是cate='1'的
name2是cate='2'的
name3是cate='3'的
也就是说把一个表的记录横过来构成个视图
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
C:\Documents and Settings\aa>sqlplus wws/wws
SQL*Plus: Release 9.2.0.1.0 - Production on 星期一 10月 13 16:31:21 2003
Copyright (c) 1982, 2002, Oracle Corporation.All rights reserved.

连接到:
Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.1.0 - Production
SQL> create table
2test2
3(id number,
4cate number,
5name varchar2(10)
6);
表已创建。
SQL> insert into test2 values(1,1,'aa1');
已创建 1 行。
SQL> insert into test2 values(2,1,'aa2');
已创建 1 行。
SQL> insert into test2 values(1,2,'bb1');
已创建 1 行。
SQL> insert into test2 values(2,2,'bb2');
已创建 1 行。
SQL> insert into test2 values(1,3,'cc1');
已创建 1 行。
SQL> insert into test2 values(1,3,'cc2');
已创建 1 行。
SQL> commit;
提交完成。
SQL> select * from test2;
ID CATE NAME
---------- ---------- ----------
1
1 aa1
2
1 aa2
1
2 bb1
2
2 bb2
1
3 cc1
1
3 cc2
已选择6行。
SQL> select a.id,a.name,b.name,c.name
2from (select id,name from test2 where cate=1) a,
3(select id,name from test2 where cate=2) b,
4(select id,name from test2 where cate=3) c
5where a.id=b.id
6and b.id=c.id;
ID NAME NAME NAME
---------- ---------- ---------- ----------
1 aa1bb1cc1
1 aa1bb1cc2
SQL>
是不是这个意思?
:)
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
最后一条数据笔误:)
SQL> update test2 set id=2 where cate=3 and name='cc2';
已更新 1 行。
SQL> commit;
提交完成。
SQL> select * from test2;
ID CATE NAME
---------- ---------- ----------
1
1 aa1
2
1 aa2
1
2 bb1
2
2 bb2
1
3 cc1
2
3 cc2
已选择6行。
SQL> select a.id,a.name,b.name,c.name
2from (select id,name from test2 where cate=1) a,
3(select id,name from test2 where cate=2) b,
4(select id,name from test2 where cate=3) c
5where a.id=b.id
6and b.id=c.id;
ID NAME NAME NAME
---------- ---------- ---------- ----------
1 aa1bb1cc1
2 aa2bb2cc2
SQL>
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
用 self-join 方式。
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
感谢阿,似的
不知道性能如何?
谢谢!
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
不对阿,你的各项平均分配了,如果cate条件的记录不是一样的多,就乱了阿,很奇怪的结果
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
where 条件中用 outer joins
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
最初由 yanchang 发布
[B]a 表有3列,id,cate,name
见个视图
id,name1,name2,name3
其中name1是cate='1'的
name2是cate='2'的
name3是cate='3'的
也就是说把一个表的记录横过来构成个视图 [/B]

try:
select id,decode(cate,'1',name,'') name1,decode(cate,'2',name,'') name2,decode(cate,'3',name,'') name3
from a
/
回复

使用道具 举报

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

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行