Oracle SQL精妙SQL语句讲解(一)

DROP TABLEt_change_lc;

CREATE TABLE t_change_lc (card_code VARCHAR2(3), q NUMBER, bal NUMBER);

INSERT INTOt_change_lc

SELECT ‘001‘ card_code, ROWNUM q, trunc(dbms_random.VALUE * 100) bal FROM
dualCONNECT BY ROWNUM <= 4

UNION

SELECT ‘002‘ card_code, ROWNUM q, trunc(dbms_random.VALUE * 100) bal FROM
dualCONNECT BY ROWNUM <= 4;

SELECT * FROMt_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;

--行列转换列转行

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 * FROMt_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;

--行列转换行转列合并

DROP TABLE t_change_lc_comma;

CREATE TABLE t_change_lc_comma AS SELECT card_code,‘quarter_‘||q AS q
FROMt_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;

--行列转换列转行分割

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 - 1 = PRIOR t1.rn

GROUP BY t1.card_code;

SELECT * FROMt_change_cl_comma;

SELECT t.card_code,

substr(t.q,

instr(‘;‘ || t.q, ‘;‘, 1, rn),

instr(t.q || ‘;‘, ‘;‘, 1, rn) - instr(‘;‘ || t.q, ‘;‘, 1, rn)) q

FROM (SELECT a.card_code, a.q, b.rn

FROM t_change_cl_comma a,

(SELECT ROWNUM rn FROM dual CONNECT BY ROWNUM <= 100) b

WHERE instr(‘;‘ || a.q, ‘;‘, 1, rn) > 0) t

ORDER BY 1, 2;

-- 实现一条记录根据条件多表插入

DROP TABLE t_ia_src;

CREATE TABLE t_ia_src AS SELECT ‘a‘||ROWNUMc1, ‘b‘||ROWNUM c2 FROM dual CONNECT
BY ROWNUM<=5;

DROP TABLE t_ia_dest_1;

CREATE TABLE t_ia_dest_1(flag VARCHAR2(10) , cVARCHAR2(10));

DROP TABLE t_ia_dest_2;

CREATE TABLE t_ia_dest_2(flag VARCHAR2(10) , cVARCHAR2(10));

DROP TABLE t_ia_dest_3;

CREATE TABLE t_ia_dest_3(flag VARCHAR2(10) , cVARCHAR2(10));

SELECT * FROM t_ia_src;

SELECT * FROM t_ia_dest_1;

SELECT * FROM t_ia_dest_2;

SELECT * FROM t_ia_dest_3;

INSERT ALL

WHEN (c1 IN (‘a1‘,‘a3‘)) THEN

INTO t_ia_dest_1(flag,c) VALUES(flag1,c2)

WHEN (c1 IN (‘a2‘,‘a4‘)) THEN

INTO t_ia_dest_2(flag,c) VALUES(flag2,c2)

ELSE

INTO t_ia_dest_3(flag,c) VALUES(flag1||flag2,c1||c2)

SELECT c1,c2, ‘f1‘ flag1, ‘f2‘ flag2 FROM t_ia_src;

时间: 2024-10-12 22:35:50

Oracle SQL精妙SQL语句讲解(一)的相关文章

Oracle SQL精妙SQL语句讲解(二)

- 如果存在就更新,不存在就插入用一个语句实现 DROP TABLE t_mg; CREATE TABLE t_mg(code VARCHAR2(10), NAME VARCHAR2(10)); SELECT * FROM t_mg; MERGE INTO t_mg a USING (SELECT 'the code' code, 'the name' NAME FROM dual) b ON (a.code = b.code) WHEN MATCHED THEN UPDATE SET a.NA

精妙sql语句_学习

说明:复制表(只复制结构,源表名:a 新表名:b) SQL: select * into b from a where 1<>1 说明:拷贝表(拷贝数据,源表名:a 目标表名:b) SQL: insert into b(a, b, c) select d,e,f from b; 说明:显示文章.提交人和最后回复时间 SQL: select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from ta

Oracle SQL语言DML语句与事务_超越OCP精通Oracle视频教程培训28

Oracle SQL语言DML语句与事务_超越OCP精通Oracle视频教程培训28 本课程介绍: Oracle视频教程,风哥本套oracle教程培训是<<Oracle数据库SQL语言实战培训教程>>的第3/5套:Oracle SQL语言DML语句与事务.主要学习Oracle数据库SQL语句INSERT命令,学习使用UPDATE命令,学习使用DELETE命令,学习创建PL/SQL对象,事务概念与控制,锁的检测和锁争用,了解撤销数据. Oracle SQL语言DML语句与事务管理,课

精妙SQL语句 基础

精妙SQL语句SQL语句先前写的时候,很容易把一些特殊的用法忘记,我特此整理了一下SQL语句操作,方便自己写SQL时方便一点,想贴上来,一起看看,同时希望大家能共同多多提意见,也给我留一些更好的佳句,整理一份<精妙SQL速查手册>,不吝赐教! 一.基础1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname 3.说明:备份sql server--- 创建 备份数据的 deviceUSE masterEXEC s

oracle 常用相关sql 语句

一.oracle 常用相关sql 语句 1. 连接数据库       su - oracle -c " sqlsplus 用户/密码     注:首次登陆用  sqlplus / as sysdba            注: 关闭数据库:注:shutdown可加关闭选项,从最温和到最粗暴的行为选项为(shutdown.shutdown transactional.shutdown immediate.shutdown abort)                 shutdown:关闭,等待每

sql server、db2、oracle 存储过程动态sql语句示例

Oracle CREATE OR REPLACE PROCEDURE a_test AS t_sql VARCHAR2(2000); t_a VARCHAR2(20); t_b VARCHAR2(20); t_c VARCHAR2(20); t_d VARCHAR2(20); BEGIN t_c := 'f'; t_d := 'g'; --这里可为insert 等任何sql语句. t_sql := 'SELECT MAX(a), MAX(b) FROM t1 WHERE c = :tempC O

Oracle数据库常用Sql语句大全

一,数据控制语句 (DML) 部分 1.INSERT  (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, --) VALUES ( 值1, 值2, --); INSERT INTO 表名(字段名1, 字段名2, --)  SELECT (字段名1, 字段名2, --) FROM 另外的表名; 字符串类型的字段值必须用单引号括起来, 例如: 'GOOD DAY' 如果字段值里包含单引号' 需要进行字符串转换, 我们把它替换成两个单引号''. 字符串类型的字段值超过

oracle性能检测sql语句

1. 监控事例的等待 select event,sum(decode(wait_Time,0,0,1)) "Prev",sum(decode(wait_Time,0,1,0)) "Curr",count(*) "Tot"  from v$session_Wait group by event order by 4; 注解:order by 4 指按第4列进行排序 2. 回滚段的争用情况 select name, waits, gets, wait

Oracle大数据SQL语句优化

1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描, 如: select id from t where num is null    可以在num上设置默认值0,确保表中num列没有null值,然后这样查询: select id from t where num=0 3.应尽量避免在 where 子句中使用!=或<>操作符,否则