表是如下SQL: 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;
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;
select tb1.paren paren ,tb4.val val4,tb5.val val5,tb6.val val6, tb4.organ organ 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'如果少值,就什么都取不出来。
一条sql不可预知末知数 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
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