sql 取最新一條記錄

2022-06-26 10:12:13 字數 1753 閱讀 1745

1. 選出某個條件最新的一條記錄

// 選出最新狀態下的每乙個單號對應的子單資料

select

distinct

a.receiving_code,a.product_barcode,a.rd_putaway_qty

from

odoo_ykd_oversea_shipping_information a

inner

join odoo_ykd_receiving_order_manage b on a.receiving_code=b.receiving_code and a.data_version=

b.data_version

inner

join odoo_sync_control_scrapy c on b.data_version=

c.data_version

inner

join

(select

max(a.odoo_create_time) as odoo_create_time, a.receiving_code

from odoo_ykd_receiving_order_manage a

inner

join odoo_sync_control_scrapy b on b.data_version=a.data_version

where a.odoo_create_time <

'2020-11-19

'group

bya.receiving_code)

as d

on d.receiving_code=b.receiving_code and d.odoo_create_time=

b.odoo_create_time

where c.task_type=

1and c.task_status=

2and c.result_type=

1and c.record_status=

1and c.data_date <=

'2020-11-18

'and c.data_date >

'2020-08-10

'// 或者用這種方式,沒寫完整,自己補充

with res as(

select id,consignment_no, row_number()over(partition by b.consignment_no order by b.odoo_create_time desc) as row_n from odoo_dsf_inbound_order_info)

2. 對上面sql的簡化解釋

1. 

select a.receiving_code,a.product_code

from

table_a

inner join

--找出receiving_code維度的最大的create_time(如果可以確定id大的就是最大的話,用id更好),

( select max(c.create_time),c.receiving_code

from table_c where 條件1 group by receiving_code) b on a.receiving_code=b.receiving_code and a.create_time=b.create_time

where 條件

2. 分組排序,去除排序之後的序號為1的(mysql不支援分組排序函式)

mysql 分組取最新一條

mysql 分組取最新一條 select from select from usr warn info handle order by handle time desc limit 10 t group by warn info idusr warn info handle 表名 handle ti...

Mysql 分組取最新一條

我有如下這張表的資料,需要根據mobile 號碼分組,每條 取最新的資料 方案1 select from select from model online forecastscore phone0001 order by id desc as cn group by cn.mobile 方案2 最佳...

sql 分組後重複資料取時間最新的一條記錄

max id 注意id必須使用聚合函式max pid,max time as desc 降序是為了where keyid 1 1是固定值第一條 如果公升序由於不知道每組多少條where中keyid就無法過濾了 as keyid,drop table if exists tmptable 存在表則刪除...