faint 我一窍不通,帮你顶
解决方案 »
- 逻辑方案求优化。请拍砖、求抛玉。
- 请教一个ORACLE删除时判断外键是否有数据的问题
- 怎样用toad创建一个用户并分配权限
- Oracle数据库同步
- oracle中的多属性索引是如何实现的?
- 想问一下这个SQL语句的意思?
- AIX5.2安装ORACLE817问题
- windows下备份ORACLE时,在批处理文件中,自动根据当前时间生成文件名称 ,参数是什么
- 请问,在pb中如何进行日期查询??
- 装了oracle816 for redhat7.2 在SQL*Plus中insert进的都是中文的,为什么一存入服务器后,再select出的就是???了
- oracle 8i个人版的问题,捡分啊
- 熟ORACLE和SQLSERVER的请帮忙(分析函数)?
create table t1(paren number(10),
ID number(5),
modulus number(3,2))
insert into t1 values(1,1,0.3)
insert into t1 values(1,2,0.4)
insert into t1 values(1,3,0.5)create table t2(
ID number(5),
Val number(5),
Organ number(5),
ptime Date
)insert into t2 values(1,53,'1001',to_date('2003-05-09','YYYY-MM-DD'));
insert into t2 values(3,100,'1001',to_date('2003-05-09','YYYY-MM-DD'));
insert into t2 values(1,70,'1002',to_date('2003-05-09','YYYY-MM-DD'));
insert into t2 values(2,71,'1002',to_date('2003-05-09','YYYY-MM-DD'));
insert into t2 values(3,838,'1002',to_date('2003-05-09','YYYY-MM-DD'));select * from t1;
select * from t2;
你这个需求基本上就是行列转换,用decode函数可以完成。
参考下面两个例子:Q: 如何实现行列转换
A: 1. 固定列数的行列转换
student subject grade
-------- ------- -----
student1 语文 80
student1 数学 70
student1 英语 60
student2 语文 90
student2 数学 80
student2 英语 70 转换为:
student 语文 数学 英语
student1 80 70 60
student2 90 80 70 SQl>create table student(id number,name varchar2(20), subject varchar2(20),
grade number(3)) nologging;
insert into student values(1,'张三','语文',80);
insert into student values(1,'张三','数学',70);
insert into student values(1,'张三','英语',60);
insert into student values(2,'李四','语文',90);
insert into student values(2,'李四','数学',80);
insert into student values(2,'李四','英语',70);
SQL>select id,name,sum(decode(subject,'语文', grade,null)) As "语文",
sum(decode(subject,'数学', grade,null)) As "数学",
sum(decode(subject,'英语', grade,null)) As "英语"
from student
group by id,name
2. 不定列行列转换
c1 c2
--- ---
1 我
1 是
1 谁
2 知
2 道
3 不
转换为:
1 我是谁
2 知道
3 不 这一类型的转换必须借助于PL/SQL来完成,这里给一个例子 CREATE OR REPLACE FUNCTION get_c2(tmp_c1 NUMBER)
RETURN VARCHAR2
IS
Col_c2 VARCHAR2(4000);
BEGIN
FOR cur IN (SELECT c2 FROM t WHERE c1=tmp_c1) LOOP
Col_c2 := Col_c2||cur.c2;
END LOOP;
Col_c2 := rtrim(Col_c2,1);
RETURN Col_c2;
END;
/ SQL> select distinct c1 ,get_c2(c1) cc2 from table;
from t1 tb1,t1 tb2,t1 tb3,t2 tb4,t2 tb5,t2 tb6
where
tb1.paren=1 and tb1.id=1 and tb1.id=tb4.id and tb4.organ = '1002'
and
tb3.paren=1 and tb3.id=3 and tb3.id=tb6.id and tb6.organ = '1002'
and
tb2.paren=1 and tb2.id=2 and tb2.id=tb5.id and tb5.organ = '1002'如果取organ = '1001'如果少值,就什么都取不出来。
select paren,sum(decode(id,1,vlaue,0)) value1,
sum(decode(id,2,vlaue,0)) value2,
sum(decode(id,3,vlaue,0)) value3,
organ,time
(select * from table1 a,table2 b where a.id=b.id)
group by paren,organ,time