查找产品的最新发货(sql 子选择?)

发布于 2024-08-25 02:39:11 字数 473 浏览 4 评论 0原文

我有三个表

shipment (shipment_id, shipping_date)
company_order (company_order_id, shipment_id, company_id)
company_order_item (company_order_item_id, company_order_id, product_id)

几家公司聚集在一起并汇总来自单个制造商的订单。该聚合订单称为“发货”。公司在每次发货中都会订购精选的产品:因此并非所有产品都会出现在任何一次发货中或为任何一家公司提供。

如何编写 SQL 查询来查找每个 Product_id 的最新发货?

我看过 SQL 查询 - 获取最新修订(更简单的情况)。

I have three tables

shipment (shipment_id, shipping_date)
company_order (company_order_id, shipment_id, company_id)
company_order_item (company_order_item_id, company_order_id, product_id)

Several companies get together and aggregate orders from a single manufacturer. This aggregate order is called a "shipment". Companies order a selection of products in each shipment: so not all products will be present in any one shipment or for any one company.

How do I write an SQL query find the most recent shipment for each product_id ?

I've looked at
SQL Query - Get Most Recent Revision (much simpler case).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无边思念无边月 2024-09-01 02:39:11

您需要获取每个产品 ID 的最大发货日期,然后检索发货详细信息,

例如

SELECT  *
FROM    (
            SELECT  coi.product_id,
                    MAX(s.shipping_date) MaxDate
            FROM    company_order_item coi INNER JOIN
                    company_order co ON coi.company_order_id = co.company_order_id INNER JOIN
                    shipment s ON co.shipment_id =s.shipment_id
            GROUP BY coi.product_id
        ) sub INNER JOIN
        company_order_item coi ON sub.product_id = coi.product_id INNER JOIN
        company_order co ON coi.company_order_id = co.company_order_id INNER JOIN
        shipment s ON   co.shipment_id = s.shipment_id
                    AND s.shipping_date = sub.MaxDate

You need to get the max shipment date per product id and then retrieve the shipment detaisl

Something like

SELECT  *
FROM    (
            SELECT  coi.product_id,
                    MAX(s.shipping_date) MaxDate
            FROM    company_order_item coi INNER JOIN
                    company_order co ON coi.company_order_id = co.company_order_id INNER JOIN
                    shipment s ON co.shipment_id =s.shipment_id
            GROUP BY coi.product_id
        ) sub INNER JOIN
        company_order_item coi ON sub.product_id = coi.product_id INNER JOIN
        company_order co ON coi.company_order_id = co.company_order_id INNER JOIN
        shipment s ON   co.shipment_id = s.shipment_id
                    AND s.shipping_date = sub.MaxDate
套路撩心 2024-09-01 02:39:11

用于说明的 SQL 代码 - (这是 T-SQL,对 SQL Server 友好,但我手边没有任何 mysql。最后一个查询应该进行微小的修改(以适合您的表名称),在 MySQL 中也能很好地工作。

我的逻辑就是找到每个product_id的最新company_order。一旦我有了它,我就可以将company_order_id加入到company_order,并且我有每个product_id的每个最新company_order的shipment_id。

DROP TABLE #shipment
DROP TABLE #company_order
DROP TABLE #company_order_item
CREATE TABLE #shipment
    (
      shipment_id INT ,
      shipping_date INT
    ) ;
CREATE TABLE #company_order
    (
      company_order_id INT ,
      shipment_id INT ,
      company_id INT
    ) ;
CREATE TABLE #company_order_item
    (
      company_order_item_id INT ,
      company_order_id INT ,
      product_id INT
    ) ;

INSERT  INTO #shipment
        ( shipment_id , shipping_date )
VALUES
        ( 1 , 1 ),
        ( 2 , 2 ),
        ( 3 , 3 )

INSERT  INTO #company_order
        ( company_order_id , shipment_id , company_id )
VALUES
        ( 1 , 1 , 1 ),
        ( 2 , 2 , 1 ),
        ( 3 , 3 , 1 )

INSERT  INTO #company_order_item
        ( company_order_item_id , company_order_id , product_id )
VALUES
        ( 1 , 1 , 1 )        ,
        ( 2 , 1 , 2 ),
        ( 2 , 2 , 2 ),
        ( 1 , 1 , 3 ),
        ( 1 , 3 , 4 )

SELECT
    product_id ,
    shipment_id
FROM
    (
      SELECT
        product_id ,
        MAX(company_order_id) AS company_order_id
      FROM
        #company_order_item
      GROUP BY
        product_id
    ) AS MostRecentProductInOrder
INNER JOIN #company_order
ON  MostRecentProductInOrder.company_order_id = #company_order.company_order_id

SQL Code to illustrate - (This is T-SQL and is SQL Server friendly, but i didn't have any mysql handy. The last query should with tiny modifications (to suit your table names) work nicely in MySQL as well.

My logic is to find the most recent company_order for each product_id. Once i have that i can just join the company_order_id to company_order, and i have the shipment_id for each most-recent company_order per product_id

DROP TABLE #shipment
DROP TABLE #company_order
DROP TABLE #company_order_item
CREATE TABLE #shipment
    (
      shipment_id INT ,
      shipping_date INT
    ) ;
CREATE TABLE #company_order
    (
      company_order_id INT ,
      shipment_id INT ,
      company_id INT
    ) ;
CREATE TABLE #company_order_item
    (
      company_order_item_id INT ,
      company_order_id INT ,
      product_id INT
    ) ;

INSERT  INTO #shipment
        ( shipment_id , shipping_date )
VALUES
        ( 1 , 1 ),
        ( 2 , 2 ),
        ( 3 , 3 )

INSERT  INTO #company_order
        ( company_order_id , shipment_id , company_id )
VALUES
        ( 1 , 1 , 1 ),
        ( 2 , 2 , 1 ),
        ( 3 , 3 , 1 )

INSERT  INTO #company_order_item
        ( company_order_item_id , company_order_id , product_id )
VALUES
        ( 1 , 1 , 1 )        ,
        ( 2 , 1 , 2 ),
        ( 2 , 2 , 2 ),
        ( 1 , 1 , 3 ),
        ( 1 , 3 , 4 )

SELECT
    product_id ,
    shipment_id
FROM
    (
      SELECT
        product_id ,
        MAX(company_order_id) AS company_order_id
      FROM
        #company_order_item
      GROUP BY
        product_id
    ) AS MostRecentProductInOrder
INNER JOIN #company_order
ON  MostRecentProductInOrder.company_order_id = #company_order.company_order_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文