Oracle 隨機(jī)數(shù)
更新時(shí)間:2009年05月27日 12:10:33 作者:
用于抽樣統(tǒng)計(jì),從數(shù)據(jù)庫(kù)中按類別隨機(jī) 抽取各類用戶
利用oracle的dbms_random包結(jié)合rownum來實(shí)現(xiàn),示例如下,隨機(jī)取499戶:
select * from
( select * from busi.t_ar_userinfo order by dbms_random.value)
where rownum < 500;
有關(guān)dbms_random的參考文獻(xiàn),鏈接為:http://www.psoug.org/reference/dbms_random.html
Deprecated. Use the methods in the DBMS_CRYPTO built-in package,這個(gè)包已經(jīng)不建議使用了
附,dbms_random幾個(gè)參數(shù)的介紹:
function value return number,返回一個(gè)[0,1)之間的隨機(jī)數(shù),精度為38位(Gets a random number, greater than or equal to 0 and less than 1, with decimal 38 digits)
function value(low IN NUMVBER,high IN NUMBER) return number,返回一個(gè)[low,high)之間的隨機(jī)數(shù)
function normal return number,return random numbers in a standard normal distribution,返回服從正態(tài)分布的一組數(shù),標(biāo)準(zhǔn)偏差為1,期望值為0,返回值中68%介于+1 和 -1 之間,95%介于 +2 和 -2 之間,99%介于+3 和 -3之間。
function random return BINARY_INTEGER, (Generate Random Numeric Values),
function string(opt char,length Number) return varchar2(the maximum is 60),返回一個(gè)指定長(zhǎng)度的字符串( Create Random Strings),opt seed values:
'a','A'&n
問:我工作中的問題:主管讓我為了某個(gè)活動(dòng)要隨機(jī)取出一些符合條件的EMAIL或者手機(jī)號(hào)碼用戶,來頒發(fā)獲獎(jiǎng)通知或其它消息,我們公司用的Oracle 9i 請(qǐng)問這個(gè)如何實(shí)現(xiàn)?
答:可以用oracle里生成隨機(jī)數(shù)的PL/SQL, 目錄文件名在:/ORACLE_HOME/rdbms/admin/dbmsrand.sql。
用之前先要在sys用戶下編譯:
SQL>@/ORACLE_HOME/rdbms/admin/dbmsrand.sql
它實(shí)際是在sys用戶下生成一個(gè)dbms_random程序包,同時(shí)生成公有同義詞,并授權(quán)給所有數(shù)據(jù)庫(kù)用戶有執(zhí)行的權(quán)限。
使用dbms_random程序包, 取出隨機(jī)數(shù)據(jù)的方法:
1. 先創(chuàng)建一個(gè)唯一增長(zhǎng)的序列號(hào)tmp_id
create sequence tmp_id increment by 1 start with 1 maxvalue 9999999 nocycle nocache;
2. 然后創(chuàng)建一個(gè)臨時(shí)表tmp_1,把符合本次活動(dòng)條件的記錄全部取出來。
create table tmp_1 as select tmp_id.nextval as id,email,mobileno from 表名 where 條件;
找到最大的id號(hào):
select max(id) from tmp_1;
假設(shè)為5000
3. 設(shè)定一個(gè)生成隨機(jī)數(shù)的種子
execute dbms_random.seed(12345678);
或者
execute dbms_random.seed(TO_CHAR(SYSDATE,'MM-DD-YYYY HH24:MI:SS'));
4. 調(diào)用隨機(jī)數(shù)生成函數(shù)dbms_random.value生成臨時(shí)表tmp_2
假設(shè)隨機(jī)取200個(gè)
create table tmp_2 as select trunc(dbms_random.value(1,5000)) as id from tmp_1 where rownum<201;
[ 說明:dbms_random.value(1,5000)是取1到5000間的隨機(jī)數(shù),會(huì)有小數(shù),
trunc函數(shù)對(duì)隨機(jī)數(shù)字取整,才能和臨時(shí)表的整數(shù)ID字段相對(duì)應(yīng)。
注意:如果tmp_1記錄比較多(10萬條以上),也可以找一個(gè)約大于兩百行的表(假如是tmp_3)來生成tmp_2
create table tmp_2 as select trunc(dbms_random.value(1,5000)) as id from tmp_3 where rownum<201; ]
5. tmp_1和tmp_2相關(guān)聯(lián)取得符合條件的200用戶
select t1.mobileno,t1.email from tmp_1 t1,tmp_2 t2 where t1.id=t2.id;
[ 注意:如果tmp_1記錄比較多(10萬條以上),需要在id字段上建索引。]
也可以輸出到文本文件:
set pagesize 300;
spool /tmp/200.txt;
select t1.mobileno,t1.email from tmp_1 t1,tmp_2 t2 where t1.id=t2.id order by t1.mobileno;
spool off;
6. 用完后,刪除臨時(shí)表tmp_1、tmp_2和序列號(hào)tmp_id。
select * from
( select * from busi.t_ar_userinfo order by dbms_random.value)
where rownum < 500;
有關(guān)dbms_random的參考文獻(xiàn),鏈接為:http://www.psoug.org/reference/dbms_random.html
Deprecated. Use the methods in the DBMS_CRYPTO built-in package,這個(gè)包已經(jīng)不建議使用了
附,dbms_random幾個(gè)參數(shù)的介紹:
function value return number,返回一個(gè)[0,1)之間的隨機(jī)數(shù),精度為38位(Gets a random number, greater than or equal to 0 and less than 1, with decimal 38 digits)
function value(low IN NUMVBER,high IN NUMBER) return number,返回一個(gè)[low,high)之間的隨機(jī)數(shù)
function normal return number,return random numbers in a standard normal distribution,返回服從正態(tài)分布的一組數(shù),標(biāo)準(zhǔn)偏差為1,期望值為0,返回值中68%介于+1 和 -1 之間,95%介于 +2 和 -2 之間,99%介于+3 和 -3之間。
function random return BINARY_INTEGER, (Generate Random Numeric Values),
function string(opt char,length Number) return varchar2(the maximum is 60),返回一個(gè)指定長(zhǎng)度的字符串( Create Random Strings),opt seed values:
'a','A'&n
問:我工作中的問題:主管讓我為了某個(gè)活動(dòng)要隨機(jī)取出一些符合條件的EMAIL或者手機(jī)號(hào)碼用戶,來頒發(fā)獲獎(jiǎng)通知或其它消息,我們公司用的Oracle 9i 請(qǐng)問這個(gè)如何實(shí)現(xiàn)?
答:可以用oracle里生成隨機(jī)數(shù)的PL/SQL, 目錄文件名在:/ORACLE_HOME/rdbms/admin/dbmsrand.sql。
用之前先要在sys用戶下編譯:
SQL>@/ORACLE_HOME/rdbms/admin/dbmsrand.sql
它實(shí)際是在sys用戶下生成一個(gè)dbms_random程序包,同時(shí)生成公有同義詞,并授權(quán)給所有數(shù)據(jù)庫(kù)用戶有執(zhí)行的權(quán)限。
使用dbms_random程序包, 取出隨機(jī)數(shù)據(jù)的方法:
1. 先創(chuàng)建一個(gè)唯一增長(zhǎng)的序列號(hào)tmp_id
create sequence tmp_id increment by 1 start with 1 maxvalue 9999999 nocycle nocache;
2. 然后創(chuàng)建一個(gè)臨時(shí)表tmp_1,把符合本次活動(dòng)條件的記錄全部取出來。
create table tmp_1 as select tmp_id.nextval as id,email,mobileno from 表名 where 條件;
找到最大的id號(hào):
select max(id) from tmp_1;
假設(shè)為5000
3. 設(shè)定一個(gè)生成隨機(jī)數(shù)的種子
execute dbms_random.seed(12345678);
或者
execute dbms_random.seed(TO_CHAR(SYSDATE,'MM-DD-YYYY HH24:MI:SS'));
4. 調(diào)用隨機(jī)數(shù)生成函數(shù)dbms_random.value生成臨時(shí)表tmp_2
假設(shè)隨機(jī)取200個(gè)
create table tmp_2 as select trunc(dbms_random.value(1,5000)) as id from tmp_1 where rownum<201;
[ 說明:dbms_random.value(1,5000)是取1到5000間的隨機(jī)數(shù),會(huì)有小數(shù),
trunc函數(shù)對(duì)隨機(jī)數(shù)字取整,才能和臨時(shí)表的整數(shù)ID字段相對(duì)應(yīng)。
注意:如果tmp_1記錄比較多(10萬條以上),也可以找一個(gè)約大于兩百行的表(假如是tmp_3)來生成tmp_2
create table tmp_2 as select trunc(dbms_random.value(1,5000)) as id from tmp_3 where rownum<201; ]
5. tmp_1和tmp_2相關(guān)聯(lián)取得符合條件的200用戶
select t1.mobileno,t1.email from tmp_1 t1,tmp_2 t2 where t1.id=t2.id;
[ 注意:如果tmp_1記錄比較多(10萬條以上),需要在id字段上建索引。]
也可以輸出到文本文件:
set pagesize 300;
spool /tmp/200.txt;
select t1.mobileno,t1.email from tmp_1 t1,tmp_2 t2 where t1.id=t2.id order by t1.mobileno;
spool off;
6. 用完后,刪除臨時(shí)表tmp_1、tmp_2和序列號(hào)tmp_id。
相關(guān)文章
win7安裝oracle10g 提示程序異常終止 發(fā)生未知錯(cuò)誤
本文將詳細(xì)介紹oracle 10g 在win7下安裝提示程序異常終止,發(fā)生未知錯(cuò)誤的解決方法,需要的朋友可以參考下2012-12-12檢查Oracle數(shù)據(jù)庫(kù)版本的7種方法匯總
在Oracle數(shù)據(jù)庫(kù)的發(fā)展中,數(shù)據(jù)庫(kù)一直處于不斷升級(jí)狀態(tài),下面這篇文章主要給大家介紹了關(guān)于檢查Oracle數(shù)據(jù)庫(kù)版本的7種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10CentOS系統(tǒng)上安裝配置Oracle數(shù)據(jù)庫(kù)的詳細(xì)教程
這篇文章主要介紹了CentOS系統(tǒng)上安裝配置Oracle數(shù)據(jù)庫(kù)的詳細(xì)教程,包括安裝過程中一些常見錯(cuò)誤問題的解決,需要的朋友可以參考下2016-03-03Windows10 x64安裝、配置Oracle 11g過程記錄(圖文教程)
這篇文章主要介紹了Windows10 x64安裝、配置Oracle 11g過程記錄(圖文教程),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03Oracle數(shù)據(jù)泵EXPDP/IMPDP導(dǎo)出導(dǎo)入功能詳細(xì)深入解析
這篇文章主要給大家介紹了關(guān)于Oracle數(shù)據(jù)泵EXPDP/IMPDP導(dǎo)出導(dǎo)入功能的相關(guān)資料,數(shù)據(jù)泵導(dǎo)出/導(dǎo)入屬于邏輯備份,熱備份與冷備份都屬于物理備份,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12Oracle報(bào)錯(cuò):ORA-28001:口令已失效解決辦法
最近在工作中遇到了一個(gè)問題,錯(cuò)誤是Oracle報(bào)錯(cuò)ORA-28001:口令已失效,下面這篇文章主要給大家介紹了關(guān)于Oracle報(bào)錯(cuò):ORA-28001:口令已失效的解決辦法,需要的朋友可以參考下2023-04-04