MySQL多行,其值在2或行中具有特定值,并且具有另一个状态

发布于 2025-02-10 06:02:45 字数 782 浏览 0 评论 0原文

我有一个带有项目ID,状态和创建的表格和关联order_id的表'item_status_history'。该表拥有项目经历的状态历史。

我正在尝试查询所有具有多个状态=“已发货”的item_id。 “返回”后必须至少有一个运输状态。

在图片中,我们可以看到第一组,第三组是我需要的所需item_id。 第四组数据已发货2张,但它们是“返回”之前的。 请注意,状态不是由Create_at订购的。我需要检查返回状态后是否有发货状态。

我尝试了一些事情。我试图选择所有具有状态的ITEM_ID及其ID,并在子查询中使用它,以再次找到已发货的状态,但不等于相同的ID。这不起作用,

select item_id from item_status_history as ISH where ISH.status = "Shipped" and ISH.item_id in (Select
        item_id
    from item_status_history
    Where status = "Shipped" and item_status_history.id <> ISH.id
) 

我知道这只是其中的一部分,然后我需要从生成的ID中找到哪些ID以及在发货之后返回的。

请参考下图

I have a table 'Item_status_history' with Item id, Status and created_at and associated order_id. This table holds an history of status an item has gone through.

I am trying to query all item_id which have multiple status = "Shipped".
and there must be at least one shipped status after a "Returned".

In the picture we can see the first set and the third set are the desired item_id I need.
The fourth set of data has 2 shipped but they were before "returned".
Note that the statuses are not ordered by created_at. I need to check if there is a shipped status after the return status was sent.

I have tried a few things. I tried to select all the item_id and its id with status shipped and use this in a subquery to again find status with shipped but not equal to the same id. THis is not working

select item_id from item_status_history as ISH where ISH.status = "Shipped" and ISH.item_id in (Select
        item_id
    from item_status_history
    Where status = "Shipped" and item_status_history.id <> ISH.id
) 

This i know is only a part of it, then I need to find from the resulting ids, which ones have returned and if it is after the shipped.

Please refer the image below
enter image description here

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

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

发布评论

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

评论(1

烟─花易冷 2025-02-17 06:02:45

它一定是一排独特的行,也许是一些ID?
但是,我们可以假设,而不是发货retored 返回之后相比,是同一时间实现。

select item_id, 
   count(case when status='Shipped' then 1 end) number_of_shippments,
   max(case when status='Shipped' then created_at end) max_shiped,
   max(case when status='Returned' then created_at end) max_returned
   from item_status_history 
group by item_id
having 
   count(case when status='Shipped' then 1 end) > 1
   and 
   max(case when status='Shipped' then created_at end) >= 
   max(case when status='Returned' then created_at end)

It must be some unique order of rows , maybe some ID ?
But we can assume, than if Shipped and Retured is in the same time, than condition about Shipped after Returned is fulfilled .

select item_id, 
   count(case when status='Shipped' then 1 end) number_of_shippments,
   max(case when status='Shipped' then created_at end) max_shiped,
   max(case when status='Returned' then created_at end) max_returned
   from item_status_history 
group by item_id
having 
   count(case when status='Shipped' then 1 end) > 1
   and 
   max(case when status='Shipped' then created_at end) >= 
   max(case when status='Returned' then created_at end)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文