【发布时间】:2017-01-04 04:14:47
【问题描述】:
我知道我需要为此使用子查询,但我不知道该怎么做。每个列 ID 有多个条目,但我想将它们显示为单行。这是桌子设计:
UUID | position_id | spot
-----+-------------+-----
111 | 1 | left
112 | 1 | right
113 | 3 | center
114 | 4 | right
我想输出这个数据的方式是这样的:
postion_1_left | position_1_right | postion_3_center | position_4_right
---------------+------------------+------------------+-----------------
true | true | true | true
这样做的原因是我想将这些数据放入 BIRT 报告中,并且将每个 position_id 和 spot 的绝对值设置为 true 或 false 会使报告更好。报告将如下所示:
left | center | right
-----------+-------+--------+-----------
position 1 | yes | no | yes
position 2 | no | no | no
position 3 | no | yes | no
position 4 | no | no | yes
我想不出更好的方法来做到这一点,所以如果有人有建议,我愿意接受。否则,我将继续使用此布局,但我很难提出查询。我尝试从这样的查询开始:
SELECT (SELECT spot FROM positions_table WHERE position_id = 3 AND spot = 'left')
from positions_table
WHERE uuid = 'afb36733'
但显然那是行不通的。
【问题讨论】:
-
尝试从
select pos.id, bool_or(spot = 'left') as left from generate_series(1,4) as pos(id) left join your_table on (pos.id = position_id) group by pos.id开始
标签: postgresql birt