码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2017-05-29 12:00:06      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:char   trunc   ble   varchar2   nio   drop   car   else   sys   

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; 

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

标签:char   trunc   ble   varchar2   nio   drop   car   else   sys   

原文地址:http://www.cnblogs.com/yxj2006/p/6917532.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!