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

Oracle SQL精妙SQL语句讲解

 
1、行列转换 行转列
DROP TABLE t_change_lc;
CREATE TABLE t_change_lc (card_code VARCHAR2(3), q NUMBER, bal NUMBER);
INSERT INTO t_change_lc
SELECT '001' card_code, ROWNUM q, trunc(dbms_random.VALUE * 100) bal from dual CONNECT BY ROWNUM <= 4
UNION
SELECT '002' card_code, ROWNUM q, trunc(dbms_random.VALUE * 100) bal from dual CONNECT BY ROWNUM <= 4;
SELECT * from t_change_lc;
SELECT a.card_code,
SUM(decode(a.q, 1, a.bal, 0)) q1,
SUM(decode(a.q, 2, a.bal, 0)) q2,
SUM(decode(a.q, 3, a.bal, 0)) q3,
SUM(decode(a.q, 4, a.bal, 0)) q4
from t_change_lc a
GROUP BY a.card_code
ORDER BY 1;
2、行列转换 列转行
DROP TABLE t_change_cl;
CREATE TABLE t_change_cl AS
SELECT a.card_code,
SUM(decode(a.q, 1, a.bal, 0)) q1,
SUM(decode(a.q, 2, a.bal, 0)) q2,
SUM(decode(a.q, 3, a.bal, 0)) q3,
SUM(decode(a.q, 4, a.bal, 0)) q4
from t_change_lc a
GROUP BY a.card_code
ORDER BY 1;
SELECT * from t_change_cl;
SELECT t.card_code,
t.rn q,
decode(t.rn, 1, t.q1, 2, t.q2, 3, t.q3, 4, t.q4) bal
from (SELECT a.*, b.rn
from t_change_cl a,
(SELECT ROWNUM rn from dual CONNECT BY ROWNUM <= 4) b) t
ORDER BY 1, 2;
3、行列转换 行转列 合并
DROP TABLE t_change_lc_comma;
CREATE TABLE t_change_lc_comma AS SELECT card_code,'quarter_'||q AS q from t_change_lc;
SELECT * from t_change_lc_comma;
SELECT t1.card_code, substr(MAX(sys_connect_by_path(t1.q, ';')), 2) q
from (SELECT a.card_code,
a.q,
row_number() over(PARTITION BY a.card_code ORDER BY a.q) rn
from t_change_lc_comma a) t1
START WITH t1.rn = 1
CONNECT BY t1.card_code = PRIOR t1.card_code
AND t1.rn - 1 = PRIOR t1.rn
GROUP BY t1.card_code;
4、行列转换 列转行 分割
DROP TABLE t_change_cl_comma;
CREATE TABLE t_change_cl_comma AS
SELECT t1.card_code, substr(MAX(sys_connect_by_path(t1.q, ';')), 2) q
from (SELECT a.card_code,
a.q,
row_number() over(PARTITION BY a.card_code ORDER BY a.q) rn
from t_change_lc_comma a) t1
START WITH t1.rn = 1
CONNECT BY t1.card_code = PRIOR t1.card_code
AND t1.rn


相关文档:

航空公司管理系统(VC++ 与SQL 2005)

系统环境:Windows 7
软件环境:Visual C++ 2008 SP1 +SQL Server 2005
本次目的:编写一个航空管理系统
      这是数据库课程设计的成果,虽然成绩不佳,但是作为我用VC++ 以来编写的最大程序还是传到网上,以供参考。用VC++ 做数据库设计并不容易,但也不是不可能。以下是我的程序界面,后面 ......

Oracle 转义字符

一、准备特殊数据
create table t_escape(s varchar2(50));
--show define -- define "&" (hex 26)
--show escape -- escape off
set define off
set escape on
insert into t_escape values('string&text');
insert into t_escape values('string\&text');
insert into t_escape values('st ......

Oracle中统计契合某列条件的列总和

 最近在项目报表中需要一个查询语句,用来统计符合某一列条件的其它几列的个数
    比如有下面一个表结构:
   
    需要在列D后面增加一列,统计在列A不为空,并且列B、C、D不为空的个数
    经过自己试验,查找帮助,总算实现了上面的查询。
&nb ......

Oracle随机函数的用法

 容易得说,经过dbms_random包调用随机数的步骤大体有4种:
一、dbms_random.normal
这个函数不带参数,会回到normal distribution的一个number门类,之所以大抵随机数会在-一到一其间。
简略测试了一下子,发作100000次最大能到五上下:
SQL> declare
二 i number:=零;
三 j number:=零;
四 begin
五 for ......

Oracle 查询、更新基本操作

oracle 默认隔离等级是:读已提交。
查询锁定,防止另外用户更新:
select * from books for update;
当前用户更新之后,另外用户可以更改。
01、表连接
假定from子句中从左到右两个表分别为A,B表。
内连接:选取A、B表的完全匹配的集合,两表交集:
select empno,ename,emp.deptno A,dept.deptno B,dname from emp ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号