SQL:多表关联采取这一纪录迄今为止最大

笔者: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,就是2014年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




版权声明:本文博客原创文章,博客,未经同意,不得转载。

posted @ 2015-07-01 08:55  hrhguanli  阅读(439)  评论(0编辑  收藏  举报