我正在使用 PostgreSQL 9.1,我有以下表格:
element(element_id int, name varchar, category_id int)
category(category_id int, name varchar)
characteristic(characteristic_id int, name varchar, unit varchar)
category_characteristic(characteristic_id, category_id)
element_characteristic(element_id, characteristic_id, value)
想象以下表格:
elements
---------------------
|id|name |category |
-----------------------
|1 |'item 1'|1 |
|2 |'item 2'|2 |
|3 |'item 3'|1 |
---------------------
category
----------------
|id|name |
|----------------|
|1 | 'category 1'|
|2 | 'category 2'|
----------------
characteristic
--------------------------------
|id|name |unit |
--------------------------------
|1 |'characteristic 1' | 'unit 1'|
|2 |'characteristic 2' | 'unit 2'|
|3 |'characteristic 3' | 'unit 3'|
|4 |'characteristic 4' | 'unit 4'|
|5 |'characteristic 5' | 'unit 5'|
|6 |'characteristic 6' | 'unit 6'|
--------------------------------
category_characteristic
------------------------------
|category_id|characteristic_id |
|------------------------------
|1 |1 |
|1 |2 |
|1 |4 |
|1 |5 |
|2 |1 |
|2 |3 |
|2 |5 |
------------------------------
element_characteristic
---------------------------------------
|id_element|id_characteristic|value |
|---------------------------------------|
|1 |1 |'value a' |
|1 |2 |'value b' |
|1 |4 |'value c' |
|1 |5 |'value d' |
|2 |1 |'value e' |
|2 |3 |'value f' |
|2 |5 |'value g' |
|3 |1 |'value h' |
|3 |2 |'value i' |
|3 |4 |'value j' |
|3 |5 |'value k' |
---------------------------------------
现在我想获得以下表格:
category 1
---------------------------------------------------------------------------
|name |characteristic 1|characteristic 2|characteristic 4|characteristic 5|
| --------------------------------------------------------------------------|
|item 1 |value a |value b |value c |value d |
|item 3 |value h |value i |value j |value k |
---------------------------------------------------------------------------
category 2
-----------------------------------------------------------
|name |characteristic 1|characteristic 3|characteristic 5|
| ----------------------------------------------------------
|item 2 |value e |value f |value g |
----------------------------------------------------------
我试图了解什么是最好的程序。我已经阅读了 tablefunc 文档,但我只是无法弄清楚如何动态地制作该过程,因为类别表上会有 N 个条目。某些方向将不胜感激。