SQL 多表關聯取最大日期的那條記錄

2021-06-22 21:35:07 字數 4324 閱讀 4838

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,就是2023年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

MYSQL 如何查詢 修改最大日期的那條記錄

參考資料 更新資料 update product info as t inner join select product id,max update date update date from product info where product id 830group byproduct id t...

mysql查詢表中日期最大的那條資料

資料庫中有這樣的一張表,現在要查詢日期最大的那條資料。直接寫sql語句如下 select name,max gmt create from user 得到結果 但是這樣輸出結果並不正確,name的值不對。修改sql語句如下 select a.name,max a.gmt create from us...

SQL 多表關聯採取這一紀錄迄今為止最大

筆者 iamlasong 1 需求 兩個表,投遞記錄表和封發開拆記錄表,如今想知道投遞日期距最後一次封發日期天數分布情況。對這個需求,須要先查詢出投遞明細,同一時候要知道相應的郵件最後一次封發情況。如機構 日期等。2 明細查詢 考慮到一天可能封發多次,所以取日期和時間都是最大的那條,語句例如以下 s...