SQL:多表关联取最大日期的那条记录
时间:2022-03-10 18:09
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