简体   繁体   English

如何在 postgresql 中使用另一个条件过滤选定的查询

[英]how to filter selected query with another condition in postgresql

i have table as below我有如下表

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

i need to get last or recent records with unique product_id, and product_type_id order by created_dttm desc.我需要通过 created_dttm desc 获取具有唯一 product_id 和 product_type_id 顺序的最后或最近记录。

so i have below query, but it is not fetching last or recent entered data due to closing_stock param > 0.所以我有以下查询,但由于 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

but i am expecting below results但我期待以下结果

id  |  product_id  |  product_type_id |  closing_stock  |   created_dttm
------------------------------------------------------------------------------------
2          2               2                  9               21-Nov-21
3          2               3                  11              21-Nov-21

The WHERE clause is applied before DISTINCT ON , filtering out all the rows with closing_stock = 0 . WHERE子句在DISTINCT ON之前应用,过滤掉所有带有closing_stock = 0的行。
So, if any row with closing_stock = 0 is the latest for a combination of product_id and product_type_id this combination will not be excluded from the results.因此,如果对于product_idproduct_type_id的组合而言, closing_stock = 0的任何行是最新的,则此组合不会从结果中排除。

Remove the condition closing_stock > 0 from your query and use it after you get the results:从查询中删除条件closing_stock > 0并在获得结果后使用它:

select *
from (
  select distinct on(product_id, product_type_id) * 
  from daily_stock
  where product_id = 2 and product_type_id in (1, 2, 3)
  order by product_id, product_type_id, created_dttm desc
) t
where closing_stock > 0;

Or, with row_number() window function:或者,使用row_number() window function:

select *
from (
  select *, row_number() over (partition by product_id, product_type_id order by created_dttm desc) rn
  from daily_stock
  where product_id = 2
) t
where rn = 1 and closing_stock > 0;

See the demo .请参阅演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM