【问题标题】:SQL : Geeting Order's status timestamp in a nested querySQL : Geeting Order 在嵌套查询中的状态时间戳
【发布时间】:2019-10-21 06:28:37
【问题描述】:

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

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 的查询 基本上所有时间戳都在一行中针对订单 ID 进行更新,我知道可以通过嵌套查询来完成,但是我不知道如何去做。

任何帮助将不胜感激。

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

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

【问题讨论】:

  • 提示:在select 子句中使用case
  • 我可以使用它,但不能用作,如果我想提取昨天发货的订单数据,我的条件将是“WHERE order_status = 'SHP' and from_unixtime( action_date / 100000) = Current_date -1" 这只会给出发货的时间戳细节。我想捕获针对已发货订单发生的所有更新。
  • 那么,如果您添加您需要的样本结果集,那将是您的好帮手。
  • 搜索 mysql pivot 和 mysql 条件聚合以及 mysql 行到列。
  • MySQL - Rows to Columns的可能重复

标签: mysql sql


【解决方案1】:

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

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')) 

【讨论】:

  • 请注意,这对 OP 没有帮助。
  • 谢谢。我认为这种逻辑和枢轴功能可以帮助我获得所需的结果。
  • @Arif Sher Khan 嗨,我编写了如下查询:select * from (select "order.order_number", "shipment.status", from_unixtime("action_date"/1000000) from order_table) pivot (max("action_date") for "shipment_status" in ('ALC' AS 'ALC_AT', 'PIK' AS 'PIK_AT', 'PAK' AS 'PAK_AT', 'SHP' AS 'SHP_AT')) AND "order. order_number" = '2019-10-19-N2-6411';但它显示错误:执行操作:第 6:12 行:输入不匹配 '(' 期待 {',',')'} 它在枢轴线上显示错误。知道可能是什么问题吗?
  • 嘿安舒尔。谢谢。我已经在答案中为您添加了代码(编辑 mysql 标题)。看看有没有帮助。
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 2015-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多