SQL:多表关联取最大日期的那条记录

时间:2023-01-14 15:12:15

作者: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