sql - 如何在 postgresql 中转置表值?
问题描述
如何转置这些值以获取此表:
让它看起来像这样:
在 postgresql 中没有可以使用的 Pivot 函数,每次尝试交叉表函数时,我的代码中都会出现错误。假设我的表名为 select * from TAttributes
解决方案
使用条件聚合,在 Postgres 中意味着filter
:
select id,
max(attributenumber) filter (where attributeid = 2000),
max(attributenumber) filter (where attributeid = 2001),
max(attributenumber) filter (where attributeid = 2002),
max(attributenumber) filter (where attributeid = 2003)
from t
group by id;
这是一个 db<>fiddle。
推荐阅读
- ios - 类型参数'CMSampleBufferRef'(又名'struct opaqueCMSampleBuffer *')既不是Objective-C对象也不是块类型
- ios - 需要有关 ios 开发人员中店面的信息
- sql-server - 有没有办法按日期/时间拆分班次记录
- tensorflow - 训练期间验证准确性和验证损失的差异
- react-native - 使用 Reactnative 在 App 中显示联系人
- ios - IOS Swift CollectionView 如何在索引上设置正确的背景颜色
- apache-spark - 使用 kotlin 编程 Apache spark 2.0
- php - 将图像保存到 Pythonista 中的特定相册
- c++ - MFC 上下文菜单的项目高度不正确
- java - For 循环 - Java