首页 > 解决方案 > 如何将多条 SQL 记录日志转换为一条新的单条记录?

问题描述

我需要处理一些日志,我意识到它们保存在多个 SQL 记录中。例如 :

日志示例

如您所见,对于相同的“TRO_IdOrdre”,我有不同的“TRO_TypeTrc”,因此日期也不同。我知道每个“TRO_TypeTrc”都有特定的含义。例如 :

问题不是每个任务都有相同的行数。它可以被创建并且永远不会启动,或者它可以启动然后被取消。

所以我想知道是否有一种方法可以将我的所有表格转换为一个新表格,如下所示:

谢谢

标签: sqllogging

解决方案


您可以使用条件聚合:

select TRO_IdOrdre,
       max(case when TRO_TypeTrc = 1 then tro_date end),
       max(case when TRO_TypeTrc = 2 then tro_date end),
       max(case when TRO_TypeTrc = 6 then tro_date end),
       max(case when TRO_TypeTrc = 8 then tro_date end),
       coalesce(max(case when TRO_TypeTrc = 4 then 'Yes' end), 'No') as is_cancelled
from t
group by TRO_IdOrdre;

您可以填写列的名称。


推荐阅读