SQL常用的特殊寫法
連線到:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
1.生成1-10的數字:
SQL> select rownum from dual connect by rownum <11;
ROWNUM
----------
1
2
3
4
5
6
7
8
9
10
已選擇10行。
2.生成5-10的數字:
SQL> select rownum+5 from dual connect by rownum <6;
ROWNUM+5
----------
6
7
8
9
10
3.生成4個隨機數:
SQL> select dbms_random.random from dual connect by rownum <5;
RANDOM
----------
-1.272E+09
1091597786
144717231
1903280455
4.隨機選出2行資料:
SQL> create table test ( id number);
表已建立。
SQL> insert into test values (1);
已建立 1 行。
SQL> insert into test values (11);
已建立 1 行。
SQL> insert into test values (111);
已建立 1 行。
SQL> insert into test values (1111);
已建立 1 行。
SQL> insert into test values (11111);
已建立 1 行。
SQL> insert into test values (111111);
已建立 1 行。
SQL> commit;
提交完成。
SQL> select * from test;
ID
----------
1
11
111
1111
11111
111111
已選擇6行。
SQL> select tt.id from (
2 select t.*,dbms_random.random rd,rownum rn from test t order by rd) tt wher
e rownum<3;
ID
----------
11
111111
5.記錄分組,每組取前3行:
SQL> select * from test;
ID NAME
---------- --------------------
1 aaa
1 bbb
1 ccc
1 ddd
2 dfdjfk
3 dddddjfk
4 dddddjfk
4 j45djfk
2 39uijdf
2 333uijdf
2
4
4 j93nmf
3 j9887eef
3 j^&*7eef
3 j^djkieeef
3 j((ieeef
4 j899f
3 ###9f
已選擇19行。
SQL> select *
2 from (select id, name, row_number() over(partition by id order by id) rn
3 from test)
4 where rn <= 3;
ID NAME RN
---------- -------------------- ----------
1 aaa 1
1 bbb 2
1 ccc 3
2 dfdjfk 1
2 2
2 333uijdf 3
3 dddddjfk 1
3 ###9f 2
3 j((ieeef 3
4 dddddjfk 1
4 j45djfk 2
4 3
已選擇12行。
行轉列:
select a,b,c from
(with test as (select 'aaa' a,'bbb' b,'1,2,3' c from dual)
select a,b,substr(t.ca,instr(t.ca, ',', 1, c.lv) + 1,instr(t.ca, ',', 1, c.lv + 1) - (instr(t.ca, ',', 1, c.lv) + 1)) AS c
from (select a,b,',' || c || ',' AS ca,length(c || ',') - nvl(length(REPLACE(c, ',')), 0) AS cnt FROM test) t,
(select LEVEL lv from dual CONNECT BY LEVEL <= 100) c where c.lv <= t.cnt )
子查詢定義:
http://www.itpub.net/viewthread.php?tid=1235717
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23135684/viewspace-630257/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 常用的 SQL 語法SQL
- js常用寫法JS
- java常用寫法Java
- Elasticsearch常用寫法Elasticsearch
- initWithFrame常用寫法
- HTML特殊字元的html、js、css寫法彙總HTML字元JSCSS
- Mybatis xml特殊符號轉義寫法MyBatisXML符號
- Oracle SQL寫法OracleSQL
- sql寫法小記SQL
- 常用CSS縮寫語法CSS
- Oracle常用sql語法集合OracleSQL
- MySQL中常用SQL語句的編寫MySql
- CSS3常用功能的寫法CSSS3
- SQL語句規範的寫法SQL
- Mysql 分組排序的sql寫法MySql排序
- OCI插入SQL語句的寫法SQL
- 單例模式常用的的兩種寫法單例模式
- html中的常用特殊符號HTML符號
- hive sql 常用語法詳解HiveSQL
- css的三種常用寫法是什麼CSS
- Perl 常用特殊變數變數
- perl常用特殊變數變數
- 當頁面特殊無法執行js時寫的程式碼方法JS
- 許可權控制下的SQL寫法SQL
- 15 個常用的 SQL Server 高階語法SQLServer
- ORACLE常用的SQL語法和資料物件OracleSQL物件
- ORACLE 常用的SQL語法和資料物件OracleSQL物件
- oracle sql tunning 15 --常用改寫OracleSQL
- 常用CSS縮寫語法總結CSS
- 3分鐘看完SQL常用語法SQL
- 【丁原】分頁sql中普通寫法和rowid寫法的效能比較SQL
- .md,markdown檔案的基本常用編寫語法
- 關於號段選取的sql寫法SQL
- Update 多個關聯表SQL的寫法SQL
- ORACLE常用的SQL語法和資料物件(轉)OracleSQL物件
- MySQL與SQL的觸發器的不同寫法MySql觸發器
- sql中使用特殊字元SQL字元
- 常用資料庫JDBC連線寫法資料庫JDBC