易截截图软件、单文件、免安装、纯绿色、仅160KB

Oracle 行转列问题


数据库查询中难免会遇到行列转换的情况,摘列一些较精典的解决方案
--####################################################################
一、采用SQL decode和PL/SQL函数实现
--####################################################################
1、固定列数的行列转换

student subject grade
---------------------------
student1 语文 80
student1 数学 70
student1 英语 60
student2 语文 90
student2 数学 80
student2 英语 100
……
转换为 
语文 数学 英语
student1 80 70 60
student2 90 80 100
……
语句如下:
select student,sum(decode(subject,'语文', grade,null)) "语文",
sum(decode(subject,'数学', grade,null)) "数学",
sum(decode(subject,'英语', grade,null)) "英语"
from table
group by student
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;即可
摘录自:http://www.360doc.com/showWeb/0/0/18772.aspx 
--####################################################################
二、使用分析函数进行行列转换
--####################################################################
其实使用分析函数进行处理是很好的方式,翻一下Tom的书,将其中的一个例子收录在这里. 比如查询scott.emp表的用户SAL排序信息,可以使用如下查询:
SQL> SELECT deptno, ename,
  2         ROW_NUMBER () OVER (PARTITION BY deptno ORDER BY sal DESC) seq
  3    from emp;
    DEPTNO ENAME             SEQ
---------- ---------- ----------
  &


相关文档:

oracle 表空间操作

oracle表空间操作详解
  1
  2
  3作者:   来源:    更新日期:2006-01-04 
  5
  6 
  7建立表空间
  8
  9CREATE TABLESPACE data01
 10DATAFILE '/ora ......

将数据导入到Oracle中

1. create table people (age int, id int);
2. 创建数据文件PeopleDate.txt.
    数据为:   
              20,1
              30,2
3. 创建 ......

Connecting Oracle with MS


This article shows how Oracle's Heterogeneous Services can be configured to allow a database to connect to a Microsoft Access database using standard databases links. The method described can be used to connect to MS-Access from about any platform - Unix/ Linux or Windows.
MS-Access 2003 and Orac ......

oracle 锁问题的解决

可以用Spotlight软件对数据库的运行状态进行监控。
当出现session锁时,我们要及时进行处理.
1. 查看哪些session锁:
SQL语句:select 'alter system kill session '''||sid||','||serial#||''';' from v$session where sid in (select sid from v$lock where block = 1);
SQL> select 'alter system kill sessio ......

Oracle 知识

ORACLE相关语法 收藏
一、Oracle入门
  理论知识:
    Oracle的物理组件有三个:
      (1)数据文件     数据文件是用于存储数据库数据的文件,如表、索引数据。每个Oracle数据库有一个或多个物理数据文件,
     &nbs ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号