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

作者: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
时间: 2024-10-28 04:35:36

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

图解SQL多表关联查询

图解SQL多表关联查询 网上看了篇文章关于多表连接的,感觉很好,记录下来,以便日后自己学习  内连接 左连接 右连接 全外连接

SQL两表关联查询&批量修改字段值

SQL关联查询&修改字段,正确范例如下: --批量修改报告单位名称&更新时间 --tt和tp两表关联查询,将符合条件的tt表中的principal字段更新到tp表的ruperson字段 merge into nhis34.t_publicplaces tp using standard.t_organization tt on (tt.orgcode = tp.r_orgcode and tp.create_time > '2015-05-07 00:00:00') when mat

稍微复杂的sql逻辑(从数据库逆序查找有限条记录(limit))并按相反顺序输出

项目中有一个业务需求是:默认加载15条历史记录(按时间顺序从早到晚). 下面是我构造的sql逻辑,mark一下,亲测可行. SELECT * FROM (SELECT *FROM group_chatmsg_v WHERE ((group_Id=46 AND send_user_id=28 AND receive_user_id=70) OR (group_Id=46 AND receive_user_id=28 AND STATUS=1)) AND is_delete =0 ORDER BY

获取分组后取某字段最大一条记录

获取分组后取某字段最大一条记录 方法一:(效率最高) select * from test as a where typeindex = (select max(b.typeindex) from test as b where a.type = b.type );

获取分组后取某字段最大一条记录(求每个类别中最大的值的列表)

获取分组后取某字段最大一条记录方法一:(效率最高) select * from test as a where typeindex = (select max(b.typeindex) from test as b where a.type = b.type ); 方法二:(效率次之) select a.* from test a, (select type,max(typeindex) typeindex from test group by type) b where a.type = b.

PL/SQL 多表关联UPDATE

假设有两个表A和B,A表字段a,b,c,d,B表字段b,e,f,两表的关联条件是字段b,现在想做个data patch,欲将B表中的字段e的值patch给A表的字段c. 有如下两种方法: 1 update A set A.c=(select e from B where B.b=A.b) where exists(select 1 from B where B.b=A.b); 2 merge into A using B on (A.b=B.b) when matched then update

SQL Server表关联

表关联:Hash.Nested Loops.Merge.这是实际算法,不是T-SQL中的inner/left/right/full/cross join.优化器会把这些T-SQL写法转换成上面的3种算法. 通过这3种算法,可以推出其他操作符的行为. 1.Hash Match Join Hashing(散列法)和Hash Table. Hashing:是编码技术,把数据转换成符号格式 原文地址:https://www.cnblogs.com/sunliyuan/p/9615012.html

[mysql] 先按某字段分组再取每组中前N条记录

From: http://blog.chinaunix.net/uid-26729093-id-4294287.html 请参考:http://bbs.csdn.net/topics/330021260 create table t2 (    id int primary key,    gid    char,    col1    int,    col2    int) engine=myisam; insert into t2 values (1,'A',31,6),(2,'B',25

MYSQL-如何查询/修改最大日期的那条记录

参考资料:http://stackoverflow.com/questions/6898935/sql-update-query-with-group-by-clause -- 更新数据 UPDATE product_info AS t INNER JOIN (SELECT product_id,max(update_date) update_date FROM product_info WHERE product_id = 830 GROUP BY product_id) t1 ON t.pr