将行连接到列

橘子

我有3个看起来像下面的表,我想将它们连接在一起,以便属性表的行成为结果表的列。

block
id  name
1   a
2   b
3   c
4   d

attr_value
id   value  block_id attr_type_id
1    0.1    1         1
2    0.2    1         2
3    0.3    2         3
4    0.4    3         4

attr_type
id   name
1    type_a
2    type_b
3    type_c
4    type_d


result table
block id  name   type_a  type_b  type_c  type_d 
1         a      0.1     0.2     null    null
2         b      null    null    0.3     null
3         c      null    null    null    0.4
4         d      null    null    null    null

我正在使用postgresql并尝试过,case when但是这重复了一些我不想要的列(即,id = 1的多个块行)。

沙尔玛(Sharma)
select  b.id,b.name
,(select a.value from attr_value a inner join attr_type at on at.id=a.attr_type_id where b.id=a.block_id and at.id=1) as 'type_a'
,(select a.value from attr_value a inner join attr_type at on at.id=a.attr_type_id where b.id=a.block_id and at.id=2) as 'type_b'
,(select a.value from attr_value a inner join attr_type at on at.id=a.attr_type_id where b.id=a.block_id and at.id=3) as 'type_c'
,(select a.value from attr_value a inner join attr_type at on at.id=a.attr_type_id where b.id=a.block_id and at.id=4) as 'type_d'
from block b

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将列连接到行

来自分类Dev

将行连接到一列 sybase

来自分类Dev

将列连接到文件

来自分类Dev

如何将单列的行连接到一行中最多4列?

来自分类Dev

如何将行连接到列取决于列的值?

来自分类Dev

将特殊字符连接到数据列?

来自分类Dev

将列连接到矩阵(numpy)

来自分类Dev

将列向量连接到矩阵的末尾

来自分类Dev

Matlab:将行和列重复并连接到新数组中

来自分类Dev

将行连接到具有指定列空间的矩阵的算法

来自分类Dev

在这种情况下,如何将行连接到动态列?

来自分类Dev

熊猫:将一行的多列连接到多行(1:n)

来自分类Dev

将基于类型列的结果连接到不同的列中

来自分类Dev

使用条件熊猫将列连接到一列

来自分类Dev

将一个表中的行集合连接到另一个表的列 - SQL Server

来自分类Dev

将多条记录连接到一行?

来自分类Dev

将数据行连接到Pandas中的新数据框

来自分类Dev

R:根据ID将行连接到单列

来自分类Dev

将文本连接到Pig中的列

来自分类Dev

将字符串连接到数据框列

来自分类Dev

将最新的列条目连接到MySQL查询结果

来自分类Dev

将Temp列的结果连接到表SQL Server

来自分类Dev

如何将约束类型连接到列信息

来自分类Dev

将Pandas列名称连接到列值

来自分类Dev

根据条件将备用标量列连接到熊猫

来自分类Dev

ms SQL将多行连接到多列

来自分类Dev

MySQL将多行查询连接到一列

来自分类Dev

将文本连接到Pig中的列

来自分类Dev

将QAction连接到插槽