简体   繁体   English

如何创建基于另一列中的多个值的新列?

[英]How do I create a new column that is based on multiple values in another column?

I've got a table that has three columns.我有一个包含三列的表。 Name, Status & ID姓名、状态和 ID 这是示例表

Now I've got to make a row called final status that has a status based on all the orders of a person.现在我必须创建一个名为 final status 的行,它的状态基于一个人的所有命令。

So for John since both his orders are delivered the final status would be complete.因此,对于约翰来说,由于他的两个订单都已交付,因此最终状态将是完整的。

For David since the status on his two orders are submitted and created his final status would be IN Progress.对于 David,因为他的两个订单的状态已提交并创建,他的最终状态将是 IN Progress。 Hope that makes sense.希望这是有道理的。

I've already thought about trying to split the status into different columns and using a case statement afterwords, but I've got no idea how to do all that in a single query.我已经考虑过尝试将状态拆分为不同的列并在后面使用 case 语句,但我不知道如何在单个查询中完成所有这些操作。

Would really appreciate some help here!真的很感激这里的一些帮助!

Another clarification that I'd like to add is that different combinations of the status would provide different final statuses:我想补充的另一个澄清是,不同的状态组合将提供不同的最终状态:

  1. When all orders for a customer are delivered then the status is COMPLETE当客户的所有订单都已交付时,状态为 COMPLETE

  2. So if the customer has orders that are not DELIVERED and some that are DELIVERED, the status would be IN PROGRESS因此,如果客户有未交付的订单和一些已交付的订单,则状态将为 IN PROGRESS

  3. If all of the customers orders are submitted then its status would be awaiting progress如果所有客户订单都已提交,则其状态将为等待进度

  4. Otherwise the final status would be awaiting submission否则最终状态将是等待提交

If you're looking to retrieve data in the way you've described, you may try below -如果您希望以您描述的方式检索数据,您可以尝试以下方法 -

Select distinct customer_name, 
CASE 
       WHEN EXISTS (SELECT status FROM cust_order_status os1 
             WHERE os1.order_id = os.order_id 
             and upper(os1.status) <> 'DELIVERED')
       THEN 'In Progress' 
       ELSE 'Complete'
  END AS FinalOrderStatus  
from cust_order_status os;

Updated query after your edits -修改后更新查询 -

Select distinct customer_name, 
CASE 
       WHEN NOT EXISTS (SELECT status FROM cust_order_status os1 
             WHERE os1.order_id = os.order_id 
             and upper(os1.status) <> 'DELIVERED')
       THEN 'Complete' 
       ELSE 
            CASE 
            WHEN EXISTS (SELECT status FROM cust_order_status os1 
             WHERE os1.order_id = os.order_id 
             and upper(os1.status) = 'DELIVERED')
             THEN 'In Progress'
            ELSE
            CASE 
            WHEN NOT EXISTS (SELECT status FROM cust_order_status os1 
             WHERE os1.order_id = os.order_id 
             and upper(os1.status) <> 'SUBMITTED')
             THEN 'Awaiting Progress'
            ELSE 'Awaiting Submission'
            END
       END
  END AS FinalOrderStatus  
from cust_order_status os;

Based on your clarifications this is slightly more complex.根据您的澄清,这稍微复杂一些。 You are implying that the statuses themselves are part of a sequence, so we'll need to map them to their sequential point in the sequence, aggregate across the customer each distinct status, and then remap the combined result to your combined status.您是在暗示状态本身是序列的一部分,因此我们需要将它们 map 到序列中的顺序点,在客户之间聚合每个不同的状态,然后将组合结果重新映射到您的组合状态。

Select Customer_name,
       Case When all_statuses='3' Then 'Complete'
            When all_statuses like '3%' Then 'In Progress'
            When all_statuses like '2%' Then 'Awaiting Progress'
            Else 'Awaiting Submission' End as final_status
From (
      Select Customer_name, 
             Group_Concat(Distinct Case when status='CREATED' Then '1'
                                       When status='SUBMITTED' Then '2'
                                       When status='DELIVERED' Then '3' End
                          Order by Case when status='CREATED' Then '1'
                                      When status='SUBMITTED' Then '2'
                                       When status='DELIVERED' Then '3' End desc
                          Separator '') as all_statuses
      From Mytable
      Group by Customer_name
     )

If Group_Concat won't let you embed the Case When mapping then you'll have to do the case mapping in one Select, the group_concat in a second, and the remapping in a third.如果 Group_Concat 不允许您在映射时嵌入案例,那么您必须在一个 Select 中进行案例映射,第二个是 group_concat,第三个是重新映射。

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

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