簡體   English   中英

如何在相關表上為最后一條記錄應用條件?

[英]How do I apply a condition on a related table for the last records?

我有三個以這種方式相關的表:

  • 商店(一個商店有很多產品)
  • 產品(一個產品有很多產品庫存歷史)
  • 產品_庫存_歷史

Products有一個名為status的字段。 它具有當前的庫存狀態。 可能的值為 1(有庫存)或任何其他值(無庫存)。

Product_Stock_History也有一個狀態字段,具有相同的可能值。


我想在 SQL 中構建的查詢是:

對於所有商店,我想獲取所有沒有庫存的產品,它們歷史上的最新 2 條記錄也沒有庫存。

簡而言之,我想知道哪些產品已經缺貨 3 天。

我也想知道如何建立索引,所以這個查詢運行效率很高。

嘗試這個

Select p.* from products p where productid in
Select productid from (
(Select PSH.productid, 
row_number() over (partition by PSH.productid  order by versionid desc) rn   from Product_Stock_History psh  where status<>1
) 
where
rn<=2) where date_col= current_date-3 and status<>1

select p.product_id
from Products p inner join Product_Stock_History ph
    on ph.product_id = p.product_id
where p.status <> 1 and ph.status <> 1 and ph.date > current_date - interval '3' days
group by p.product_id
having count(*) = 2

不參考當前日期:

select p.product_id
from Products p inner join Product_Stock_History ph
    on ph.product_id = p.product_id
where p.status <> 1
qualify
    row_number() over (
        partition by p.product_id order by date desc
    ) = 1 and 
    count(*) over (
        partition by p.product_id order by date desc
        rows between current row and 1 following 
    ) filter (ph.status <> 1) = 2

我忘記了 Postgres 不允許qualify 看看這個,直到我能回來: https://dbfiddle.uk/?rdbms=postgres_14&fiddle=ad252c27153626eb6c3e33fae5ab1eb7

您可以使用window 查詢(例如 Himanshugroup by/have like shawnt00 )來做您想做的事情。 或者,您可以重新組織架構以使其保持簡單。

與其存儲標志,不如存儲兩個時間戳: stocked_atout_of_stock_at

stores

products

store_products
  store_id references stores
  product_id references products
  unique(store_id, product_id)

  stocked_at timestamp,
  out_of_stock_at timestamp,
  check (stocked_at != out_of_stock_at)

從它們計算它的狀態。

select
  stocked_at > out_of_stock_at as in_stock
from store_products

您可以使用 生成的列方便地完成此操作。

  in_stock boolean generated always as (stocked_at > out_of_stock_at) stored

簡而言之,我想知道哪些產品已經缺貨 3 天。

select product_id
from store_products
where not in_stock
  and out_of_stock_at < current_timestamp - '3 days'::interval

我也想知道如何建立索引,所以這個查詢運行效率很高。

(out_of_stock_at, stocked_at)上創建一個復合索引。


狀態標志通常可以被連接表替換。

我們可以做一個批判性的觀察。

  • 商店的目錄與其庫存不同。

所以我們有...

  • 有產品。
  • 有商店。
  • 商店有他們提供的產品目錄。
  • 商店有庫存的產品庫存。

表示為表和約束...

stores

products

store_product_catalog
  store_id references stores
  product_id references products
  unique(store_id, product_id)

-- This allows a store to have inventory not in their catalog.
-- If you don't want that, give store_product_catalog an id
-- and relate store_product_inventory to store_product_catalog
store_product_inventory
  store_id references stores
  product_id references products
  unique(store_id, product_id)

  quantity
  updated_at

當 store_product_inventory.quantity 發生變化時,編寫一個更新觸發器來更改 store_product_inventory.updated_at。

簡而言之,我想知道哪些產品已經缺貨 3 天。

select product_id
from store_product_inventory
where quantity = 0
  and updated_at < current_timestamp - '3 days'::interval

我也想知道如何建立索引,所以這個查詢運行效率很高。

(quantity, updated_at)上創建一個復合索引。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM