sql - 如何将多条 SQL 记录日志转换为一条新的单条记录?
问题描述
我需要处理一些日志,我意识到它们保存在多个 SQL 记录中。例如 :
如您所见,对于相同的“TRO_IdOrdre”,我有不同的“TRO_TypeTrc”,因此日期也不同。我知道每个“TRO_TypeTrc”都有特定的含义。例如 :
- 1:创建任务
- 2:任务开始
- 4:任务结束等
问题不是每个任务都有相同的行数。它可以被创建并且永远不会启动,或者它可以启动然后被取消。
所以我想知道是否有一种方法可以将我的所有表格转换为一个新表格,如下所示:
- 订单编号
- 创建日期
- 开始日期
- 结束日期
- 取消 ?(是/否)
- 取消日期
谢谢
解决方案
您可以使用条件聚合:
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;
您可以填写列的名称。
推荐阅读
- typescript - @typescript-eslint/no-unsafe-assignment - `any` 值的不安全分配
- java - 如何订购扩展另一个类的类的字段?
- c# - 带有复选框和文本框的微调器在适配器中识别项目检查状态更改时
- sql - 基于多列条件的新列
- javascript - 如何使用 node-fetch 发出三个 POST 请求
- python - 当满足时间序列的条件时,Python Pandas 会聚合过去的行
- javascript - 更改 CSS Grid 中项目的顺序
- flutter - 如何修复此错误未处理的异常:类型“字符串”不是“索引”类型“int”的子类型
- android - 如何仅从 Android Studio 中的依赖项中实现单个组件?
- python - Python selenium - 从 p 和 span 获取值