我正在尝试在postgresql中创建一个小词汇表。 到目前为止,我有一个表格,其中有一些"垂直值"
General_id Sing_id Synonim Language
1 1 yes en-GB
2 1 ja de-DE
3 1 oui fr-FR
4 2 no en-GB
5 2 nein de-DE
6 2 niet ru-RU
SELECT
General_id,
Sing_id,
Synonim,
Language
FROM table1.base
但我想这样:
Sing_id en_GB de_DE fr_FR ru_RU ...
1 yes ja oui
2 no nein niet ...
你想告诉我如何实现这个目标吗?
答案 0 :(得分:2)
您正在谈论'转动'。
请参阅此帖子:Pivoting in Postgres
帖子示例:
SELECT *
FROM crosstab(
'SELECT
a date,
b.desc AS os,
(random() * 10000 + 1)::int AS value
FROM generate_series((now() - ''100 days''::interval)::date, now()::date, ''1 DAY''::interval) a,
(SELECT unnest(ARRAY[''OSX'', ''Windows'', ''Linux'']) AS DESC) b ORDER BY 1,2
','SELECT unnest(ARRAY[''OSX'', ''Windows'', ''Linux''])'
)
AS ct(date date, OSX int, Windows int, Linux int);