筆者:iamlasong
1、需求
兩個表,投遞記錄表和封發開拆記錄表,如今想知道投遞日期距最後一次封發日期天數分布情況。
對這個需求,須要先查詢出投遞明細,同一時候要知道相應的郵件最後一次封發情況。如機構、日期等。
2、明細查詢
考慮到一天可能封發多次,所以取日期和時間都是最大的那條,語句例如以下:
select d.city,d.ssxs,d.zj_code,d.zj_mc,c.mail_num,
c.dlv_date,to_char(c.dlv_time,'hh24miss'), c.actual_goods_fee,
c.dlv_pseg_code,c.dlv_pseg_name,c.dlv_bureau_name,
c.dlv_staff_code,c.dlv_staff_name,c.signer_name,
a.deal_org_code,a.dlv_org_code,a.label_strip,a.deal_date,a.deal_time
from tb_evt_bag_mail_rela a, tb_evt_dlv c, tb_jg d
where a.mail_num = c.mail_num
and a.bag_actn_code = '3'
and c.dlv_date between to_date('2014-6-1', 'yyyy-mm-dd') and
to_date('2014-6-1', 'yyyy-mm-dd')
and c.dlv_bureau_org_code = d.zj_code
and c.dlv_sts_code = 'i'
and d.jgfl = 'yz'
and (a.deal_date, a.deal_time) =
(select max(t.deal_date), max(t.deal_time)
from tb_evt_bag_mail_rela t
where t.mail_num = a.mail_num
and t.bag_actn_code = '3'
group by t.mail_num, t.bag_actn_code)
3、時限分布
有了明細語句。時間分布就比較簡單了。語句例如以下:
select d.city, d.ssxs, d.zj_code, d.zj_mc, count(*) ttzl,
sum(decode(c.dlv_date - a.deal_date, 0, 1, 0)) t0,
sum(decode(c.dlv_date - a.deal_date, 1, 1, 0)) t1,
sum(decode(c.dlv_date - a.deal_date, 2, 1, 0)) t2,
sum(decode(c.dlv_date - a.deal_date, 3, 1, 0)) t3,
sum(decode(c.dlv_date - a.deal_date, 4, 1, 0)) t4,
sum(decode(c.dlv_date - a.deal_date, 5, 1, 0)) t5
from tb_evt_bag_mail_rela a, tb_evt_dlv c, tb_jg d
where a.mail_num = c.mail_num
and a.bag_actn_code = '3'
and c.dlv_date between to_date('2014-6-1', 'yyyy-mm-dd') and
to_date('2014-6-1', 'yyyy-mm-dd')
and c.dlv_bureau_org_code = d.zj_code
and c.dlv_sts_code = 'i'
and d.jgfl = 'yz'
and (a.deal_date, a.deal_time) =
(select max(t.deal_date), max(t.deal_time)
from tb_evt_bag_mail_rela t
where t.mail_num = a.mail_num
and t.bag_actn_code = '3'
group by t.mail_num, t.bag_actn_code)
group by d.city, d.ssxs, d.zj_code, d.zj_mc
order by d.city, d.ssxs, d.zj_code
4、存在問題及解決
上面語句的查詢結果出來後。經核對,數字對不上,記錄變少了,差了非常多。檢查發現有一部分郵件沒有分發記錄。只是這個數字非常少,那麼原因出在哪兒呢?
原來原因出在最後乙個條件上。最後乙個條件是查出最大日期和最大時間。可是。最大日期的那條記錄時間不一定最大,結果導致,這些郵件都被涮下去了。為了得到正確結果。最後乙個條件改為:
and to_char(a.deal_date,'yyyymmdd')||to_char(a.deal_time,'000000') =
(select max(to_char(t.deal_date,'yyyymmdd')||to_char(t.deal_time,'000000'))
from tb_evt_bag_mail_rela t
where t.mail_num = a.mail_num
and t.bag_actn_code = '3'
group by t.mail_num, t.bag_actn_code)
時間按格式「000000」轉換是由於表中時間是時分秒組成的數值型字段,長度不定。按格式「000000」轉換後統一長度,便於比較大小
。比方日期時間合成結果:20140530 091239,就是2023年5月30日9時12分39秒。
select d.city, d.ssxs, d.zj_code, d.zj_mc, count(*) ttzl,
sum(decode(c.dlv_date - a.deal_date, 0, 1, 0)) t0,
sum(decode(c.dlv_date - a.deal_date, 1, 1, 0)) t1,
sum(decode(c.dlv_date - a.deal_date, 2, 1, 0)) t2,
sum(decode(c.dlv_date - a.deal_date, 3, 1, 0)) t3,
sum(decode(c.dlv_date - a.deal_date, 4, 1, 0)) t4,
sum(decode(c.dlv_date - a.deal_date, 5, 1, 0)) t5
from tb_evt_bag_mail_rela a, tb_evt_dlv c, tb_jg d
where a.mail_num = c.mail_num
and a.bag_actn_code = '3'
and c.dlv_date between to_date('2014-6-1', 'yyyy-mm-dd') and
to_date('2014-6-1', 'yyyy-mm-dd')
and c.dlv_bureau_org_code = d.zj_code
and c.dlv_sts_code = 'i'
and d.jgfl = 'yz'
and to_char(a.deal_date,'yyyymmdd')||to_char(a.deal_time,'000000') =
(select max(to_char(t.deal_date,'yyyymmdd')||to_char(t.deal_time,'000000'))
from tb_evt_bag_mail_rela t
where t.mail_num = a.mail_num
and t.bag_actn_code = '3'
group by t.mail_num, t.bag_actn_code)
group by d.city, d.ssxs, d.zj_code, d.zj_mc
order by d.city, d.ssxs, d.zj_code
最後須要說明一下。to_char按指定格式「000000」轉換後,會在前面加上乙個空格,只是這個不影響比較。to_char這個函式後面假設沒有格式指定,轉換後則沒有空格,只是長度就是數字的實際長度了,要想統一長度。能夠加上乙個大數。比如,
to_char(t.deal_time+9000000)
轉換結果:201405309091239 sql 多表關聯
專案中遇到多表關聯查詢,device info與device oper是一對多關係,project info,branch info與device info是一對多關係。多表的查詢 select o.d.devicename,p.projectname,b.branchname r.releasei...
sql多表關聯
student表,score表,student的id欄位和score的studentid欄位關聯。student表中有1,2,而score表中有2,3。student表中有score表中沒有的1,score表中有student表中沒有的3.有乙個交集是2。drop table student cre...
SQL多表關聯查詢
關於 有時候,我們查詢資料時,會採用多資料庫關聯查詢的方式。資料庫通過連線兩張表或多張表查詢時,會生成一張臨時的中間表,然後返回給使用者的就是這張臨時表的資料。那麼具體怎麼操作呢?我們可以採用left join,搭配on where來實現。具體備註 1.on條件是在生成臨時表時使用的條件,它不管on...