作者:iamlasong
1、需求
两个表,投递记录表和封发开拆记录表,现在想知道投递日期距最后一次封发日期天数分布情况。
对这个需求,需要先查询出投递明细,同时要知道对应的邮件最后一次封发情况,如机构、日期等
2、明细查询
考虑到一天可能封发多次,所以取日期和时间都是最大的那条,语句如下:
select *
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 (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原文地址:http://blog.csdn.net/iamlaosong/article/details/38065547