oracle資料庫中按機構抽取2條郵件的SQL寫法

2021-07-08 17:39:46 字數 1739 閱讀 2829

iamlaosong文

客服需要抽取妥投郵件進行回訪,為了保證抽取的覆蓋率,要求每個機構抽取幾條,然後進行回訪,語句如下:

select d.city, d.ssxs, d.zj_code, d.zj_mc, c.dlv_date,c.mail_num, c.dlv_sts_code

from tb_evt_dlv c, sncn_zd_jg d

where c.dlv_date = to_date('2015-12-7', 'yyyy-mm-dd')

and c.dlv_sts_code = 'i'

and c.dlv_bureau_org_code = d.zj_code

and c.mail_num in (select t.mail_num

from tb_evt_dlv t

where t.dlv_date = to_date('2015-12-7', 'yyyy-mm-dd')

and t.dlv_sts_code = 'i'

and t.dlv_bureau_org_code = c.dlv_bureau_org_code

and rownum <= 2)

order by d.zj_code;

上述語句是隨機抽取的,由於我們是對虛假資訊進行抽查,為了提高準確度,就需要對資訊進行篩選,比如抽取下段和妥投時間間隔過小的郵件,語句如下(時間間隔600秒):

select d.city, d.ssxs, d.zj_code, d.zj_mc, c.dlv_date,c.mail_num, c.dlv_sts_code

from tb_evt_dlv c, sncn_zd_jg d

where c.dlv_date = to_date('2015-12-7','yyyy-mm-dd')

and c.dlv_sts_code = 'i'

and c.dlv_bureau_org_code = d.zj_code

and c.mail_num in (select t.mail_num

from tb_evt_dlv t,tb_evt_pseg_mail_rela v

where t.dlv_date = to_date('2015-12-7','yyyy-mm-dd')

and t.mail_num=v.mail_num

and t.dlv_date=v.seal_date

and t.dlv_sts_code = 'i'

and t.dlv_bureau_org_code = c.dlv_bureau_org_code

and (t.dlv_time - v.seal_time)*3600*24 <= 600

and rownum <= 2 )

order by d.zj_code;

需要注意的是,oracle中日期時間和excel類似,雖然顯示出來是日期+時間,但其值可以認為由整數和小數兩部分組成,整數部分是距某日期的天數(excel定義1900 年 1 月 1 日的值是1,oracle似乎從公元元年1 月 1 日開始,西元前是負數,沒有0值。oracle9i sql reference文件中有:from january 1, 4712 bc to december 31, 9999 ad. 即西元前2023年1月1日到公元2023年12月31日),小數則是時間值,是以當前時間距0時的秒數除以24*3600得到的資料。所以,兩個時間之差乘以24*3600就是以秒為單位的時間。同理

,乘以24就是以小時為單位的時間,乘以

24*60就是以分鐘為單位的時間。

隨機抽取資料庫記錄

mysql select from tablename order by rand limit 10 sql server select top 5 from tablename order by newid access select top 5 from tablename order by r...

Oracle資料庫中字元型字段按數字排序

今天在轉換資料時,遇到了乙個主鍵排序的問題。字元型的主鍵,儲存的都是數字,資料導過來以後發現資料排序都是亂的,就想著按數字規則排序。但發現to number總是報錯,就想著裡面應該是有字元存在。後來使用了正則關係式,問題解決。以下是正則關係式的兩種用法,記錄下來。方法一 select from xt...

資料庫 Oracle中建立資料庫

create database lihua 網上的說法是 oracle中的例項只對應乙個資料庫,出現此種情況說明oracle資料庫處於mount 裝載 或open狀態,需要使用startup nomount語句進行狀態更改,或者是直接使用dbca的圖形介面來建立 注 經測試,startup nomo...