首页 > 解决方案 > SQL : Geeting Order 在嵌套查询中的状态时间戳

问题描述

我正在处理一个订单表,其中包含有关已分配订单的所有详细信息。示例数据库示例是

Order ID   Order Status     Action Date
23424      ALC              1571467792094280
23424      PIK              1571467792999990
23424      PAK              1571469792999990
23424      SHP              1579967792999990
33755      ALC              1581467792238640
33755      PIK              1581467792238640
33755      PAK              1581467792238640
33755      SHP              1581467792238640

在表中,我有订单 ID、状态、action_date(当订单状态根据更新时间戳更新时,操作日期会更新,action_date 是 unix 时间)

我正在尝试编写一个可以为我提供订单 ID、ALC_AT、PIK_AT、PAK_AT、SHP_AT 的查询无法弄清楚如何去做。

任何帮助将不胜感激。

编辑(根据要求提供示例结果):

Order ID   Order Status     ALC_AT             PIK_AT            PAK_AT              SHP_AT
23424      SHP              1571467792094280   1571467792999990  1571469792999990    1579967792999990

标签: mysqlsql

解决方案


我不确定它是如何在 mysql 中完成的。但下面描述了它将如何在 Oracle 中完成。您可以PIVOT在 mysql 中搜索更多内容以帮助您。

select * 
  from (select order_id,
               status, 
               action_date 
          from order)  
 pivot (max(status) 
        for app_id in ( 'ALC' as 'ALC_AT', 'PIK' as 'PIK_AT', 'PAK' as 'PAK_AT', 'SHP' as 'SHP_AT'))

希望这会帮助你。

编辑mysql:

select * 
  from (select "order.order_number",
               "shipment.status", 
               from_unixtime("action_date"/1000000) as "action_date"
          from order_table 
         where "order.order_number" = '2019-10-19-N2-6411')
  pivot (max("action_date")
         for "shipment_status" in ( 'ALC' AS 'ALC_AT', 'PIK' AS 'PIK_AT', 'PAK' 
                                     AS 'PAK_AT', 'SHP' AS 'SHP_AT')) 


推荐阅读