【问题标题】:fetch latest message from sql DB based on timestamp by ignore failed one通过忽略失败的时间戳从 sql DB 中获取最新消息
【发布时间】:2020-11-09 11:46:18
【问题描述】:

在我的发货数据库中,12456 有许多记录,我们的状态代码为 1000(成功)或 1001(失败),我想为给定的发货 ID 列表提取报告

如果 DB 中的最新记录包含状态代码 1000,则忽略,否则在选择查询中显示我的数据。应该能够根据消息添加额外的过滤器,如果记录包含特定文本,则在报告中忽略

如何修改查询。我是这个领域的新手

select createdDate, , status_code, message from SHIPMENT_DATA
where shipmentid in (
'12456'
)

样本数据

TimeStamp                               ShipmentId  StatusCode      Message
####################################################################################################
03-NOV-20 07.15.28.951000000 AM         12456       1000            error message
03-NOV-20 06.15.28.951000000 AM         222         1001            error message
03-NOV-20 05.15.28.951000000 AM         12456       1001            Success
03-NOV-20 04.15.28.951000000 AM         333         1000            Success

这里发货,12456最新消息状态码为1000,不要拉入端口,显示rest 2记录。

【问题讨论】:

  • 请提供样本数据和期望的结果。
  • 您的样本没有“最新记录”。它有 2 条记录与 12456 的时间戳完全相同。
  • @JamesZ - 数据已修复

标签: sql oracle


【解决方案1】:

如果您想了解未成功发货的详细信息,可以使用first_value()

select shipment_id, createdDate, status_code, message
from (select sd.*,
             first_value(status_code) over (partition by shipment_id order by createdDate desc) as last_status_code
      from SHIPMENT_DATA sd
      where shipmentid in ('12456')
     ) sd
where last_status_code <> 1000;

【讨论】:

  • 嗨@Gordon .. 不适合我。它没有拿起 SD。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
  • 2011-04-09
  • 1970-01-01
  • 2018-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多