2015-09-22 90 views
0

我有一種情況如下:與Postgres裏列的動態列表動態視圖

表用戶 ID User viewname Description 1 JamesId employeeview This is a employee 2 FredId employeeview This is a employee 3 NickId managerview This is a manager

表性能 ID key value user_ref 1 name james 1 2 phone 88888888888 1 3 email [email protected] 3 4 name fred 2 5 phone 555555555555 2

我需要創建的意見如下:

Emp loyeeView Id empId name phone Description 1 JamesId james 88888888888 This is a employee 2 FredId fred 555555555555 This is a employee

ManagerView Id empId email Description 1 NickId [email protected] This is a manager

屬性表是擴展用戶表的一切是存儲爲鍵值對。需要使用上述兩個表動態創建視圖。屬性表是動態的。

是否可以生成這樣的視圖。有人可以舉個例子來做。

+0

您需要動態數據透視表或交叉表[** SAMPLE **](http://www.cureffi.org/2013/03/19/automatically-creating-pivot-table-column-names-in-postgresql/ ) –

+0

經理總是有電子郵件和員工有電話? – sagi

+0

這是一個示例,屬性可以與其他類型的電子郵件,電話等不同的類型一起增長。 – UserAdi

回答

0

我設法在postgres中使用交叉表功能。然而,我不得不修改屬性表,以適應另一個colume作爲一個確定。

修改屬性表: ID key value user_ref identifier 1 name james 1 viewemployee 2 phone 88888888888 1 viewemployee 3 email [email protected] 3 viewmanager 4 name fred 2 viewemployee 5 phone 555555555555 2 viewemployee

SQL查詢使用交叉爲employeeview

create MATERIALIZED VIEW EmployeeView as select one.id, one.userid, two.name, two.phone, one.description from user_table one, (SELECT * FROM crosstab( 'SELECT user_ref,key,value FROM user_properties where identifier=''viewemployee'' order by 1,2') AS ct_row (user_ref int, name varchar, phone varchar)) two where one.id=two.user_ref

SQL查詢使用交叉爲managerview

create MATERIALIZED VIEW ManagerView as select one.id, one.userid, two.email, one.description from user_table one, (SELECT * FROM crosstab( 'SELECT user_ref,key,value FROM user_properties where identifier=''viewmanager'' order by 1,2') AS ct_row (user_ref int, email varchar)) two where one.id=two.user_ref