sql - 如何有条件地从选择查询中转置数据
问题描述
我有一个选择查询,它返回我的数据如下:
---------------------------------------
PartCode | ParentCode | Flag |
---------------------------------------
ABC | XYZ | null |
---------------------------------------
PQR | XYZ | Y |
---------------------------------------
现在我想根据标志将其转置如下:
- 标记为 Y 的零件代码应标记为关键零件,其他零件标记为辅助零件。
- 总会有两行我想应用这个条件。
---------------------------------------------
ParentCode | CriticalPart | SecondaryPart |
---------------------------------------------
XYZ | PQR | ABC |
---------------------------------------------
解决方案
使用下面的代码来实现结果。
select ParentCode
, Max(case when flag = 'Y' then PartCode end) as CriticalPart
, Max(case when flag is null then PartCode end) as SecondaryPart
from table
group by ParentCode
推荐阅读
- python - 是否可以设置 tkinter Text 小部件的样式?
- powershell - 如何更新活动目录中的自定义属性?
- debugging - lldb:跳出循环?
- r - UpSetR sets.bar 交互不同颜色及其集合交点
- python - 如何显示 ForeignKey 值?
- php - Symfony - 用于数据库密码的 Azure Vault
- apache-spark - 如何根据字段的值推测 DF 的模式推断?
- c++ - 替换值时谁会删除以前的 nlohmann json 对象资源?
- javascript - 清除处于状态的对象内的键
- javascript - JS获取浏览器之前下载的文件