1. 程式人生 > 實用技巧 >sql - 記錄

sql - 記錄

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 by a.receiving_code) as d on d.receiving_code=b.receiving_code and
d.odoo_create_time=b.odoo_create_time where c.task_type=1 and c.task_status=2 and c.result_type=1 and c.record_status=1 and 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)