我有如下表
id | product_id | product_type_id | closing_stock | created_dttm
-------------------------------------------------------------------------
1 2 1 0 21-Nov-21
2 2 2 9 21-Nov-21
3 2 3 11 21-Nov-21
4 2 1 7 20-Nov-21
我需要通过 created_dttm desc 获取具有唯一 product_id 和 product_type_id 顺序的最后或最近记录。
所以我有以下查询,但由于 close_stock 参数> 0,它没有获取最后或最近输入的数据。
select distinct on(product_id, product_type_id) *
from daily_stock
where product_id = 2
and product_type_id in (1, 2, 3)
and closing_stock > 0
order by product_id, product_type_id , created_dttm desc
id | product_id | product_type_id | closing_stock | created_dttm
-------------------------------------------------------------------------
1 2 2 9 21-Nov-21
2 2 3 11 21-Nov-21
3 2 1 7 20-Nov-21
但我期待以下结果
id | product_id | product_type_id | closing_stock | created_dttm
------------------------------------------------------------------------------------
2 2 2 9 21-Nov-21
3 2 3 11 21-Nov-21