mysql - 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
解决方案
我不确定它是如何在 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'))
推荐阅读
- java - 如何为多个 ImageView 创建更新函数?
- php - PHP统一重复数组
- css - 使用 CSS 使用嵌套输入字段更改字段集的背景颜色
- python - 如何设置 cookie 值,然后立即重定向到下一页并显示其中的内容
- c++ - 使用rapidjson c ++保存/加载对象的向量
- angular - Angular 4 错误消息应链接到无效的 formControl
- javascript - 仅当下拉元素存在时才显示 tr/td 元素
- java - 多模块Spring项目:覆盖测试中的资源文件
- javascript - angular-cli build 给出错误:leaflet.browser.print“不是模块”
- mysql - Mysql] 查找用户以不同角色加入的事件的第一个和最后一个日期