tablefunc 行转列
安裝extension?tablefunc??:
digoal=# create extension tablefunc ; CREATE EXTENSION
幾個主要函數介紹:
normal_rand:
normal_rand(int numvals, float8 mean, float8 stddev) returns setof float8 生成numvals個值,其平均為mean,標準偏差為stddev。 例如,兩組數的集合 {0,5,9,14} 和 {5,6,8,9} 其平均值都是 7 ,但第二個集合具有較小的標準差。digoal=# SELECT * FROM normal_rand(10, 5, 3); normal_rand --------------------- 2.40647424167461 3.97358357565708 6.83705458528592 3.55287920003402 -0.813572020831272 8.04323869492369 10.4944509678492 6.0051904741271 3.70728813314852 -0.0320731730703967 (10 rows) digoal=# SELECT * FROM normal_rand(10, 5, 3000); normal_rand ------------------- 865.360923345198 -1977.78887247386 2826.97836891189 1053.5080721384 366.541989988861 3071.38745724025 1832.10141151151 -1217.16007210777 -7018.42213577992 -1816.45691496064 (10 rows)
crosstab:
crosstab(text sql):
參數sql的格式必須固定如下: select rowid,?attribute, value from tb; 解釋: rowid:分類row行的約束,即組成group的條件。 attribute:分類column列的約束,即crosstab后形成的setof結果集中有刨去rowid這一列外,會分成多少組group(category)。value:在分類萬row和column后,對應row, column交叉地帶應填充的值。
舉例說明:
postgres=# CREATE TABLE ct(id SERIAL, rowid TEXT, attribute TEXT, value TEXT); INSERT INTO ct(rowid, attribute, value) VALUES('test1','att1','val1'); INSERT INTO ct(rowid, attribute, value) VALUES('test1','att2','val2'); CREATE TABLE postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test1','att1','val1'); INSERT 0 1 postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test1','att2','val2'); INSERT 0 1 postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test1','att3','val3'); INSERT 0 1 postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test1','att4','val4'); INSERT 0 1 postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test2','att1','val5'); INSERT 0 1 postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test2','att2','val6'); INSERT 0 1 postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test2','att3','val7'); INSERT 0 1 postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test2','att4','val8'); INSERT 0 1 postgres=# SELECT * postgres-# FROM crosstab( postgres(# 'select rowid, attribute, value postgres'# from ct postgres'# where attribute = ''att2'' or attribute = ''att3'' postgres'# order by 1,2') postgres-# AS ct(row_name text, category_1 text, category_2 text, category_3 text);row_name | category_1 | category_2 | category_3 ----------+------------+------------+------------test1 | val2 | val3 | test2 | val6 | val7 | (2 rows)此外:結果集setof需要做"AS" 的說明羅列出氣列數和列的類型。
crosstabN(text sql):
跟crosstab一樣,只是不需要對結果集setof進行做“AS”聲明,而是使用“N” 對齊進行說明結果集的列。 The?tablefunc?module includes?crosstab2,?crosstab3, and?crosstab4
tablefunc模塊只支持N為2,3,4。 如果想之多更多則需要參考文檔create type進行創建:http://www.postgresql.org/docs/9.3/static/tablefunc.html
crosstab(text source_sql, text category_sql):
解釋: source_sql:格式需規定為:select?rowid, rowdt, attribute, value from tb;大體與crosstab一致。其中至少需要rowid, attribute, value著三個值。rowdt是額外可有可無的(但是需要注意"AS" 部分的定義要與其相符)。 category_sql:為分類列的約束group(category)。postgres=# CREATE TABLE cth(rowid text, rowdt timestamp, attribute text, val text); INSERT INTO cth VALUES('test1','01 March 2003','temperature','42'); CREATE TABLE postgres=# INSERT INTO cth VALUES('test1','01 March 2003','temperature','42'); INSERT INTO cth VALUES('test1','01 March 2003','volts','2.6987'); INSERT 0 1 postgres=# INSERT INTO cth VALUES('test1','01 March 2003','test_result','PASS'); INSERT 0 1 postgres=# INSERT INTO cth VALUES('test1','01 March 2003','volts','2.6987'); INSERT 0 1 postgres=# INSERT INTO cth VALUES('test2','02 March 2003','temperature','53'); INSERT INTO cth VALUES('test2','02 March 2003','test_result','FAIL'); INSERT 0 1 postgres=# INSERT INTO cth VALUES('test2','02 March 2003','test_result','FAIL'); INSERT 0 1 postgres=# INSERT INTO cth VALUES('test2','02 March 2003','test_startdate','01 March 2003'); INSERT INTO cth VALUES('test2','02 March 2003','volts','3.1234');INSERT 0 1 postgres=# INSERT INTO cth VALUES('test2','02 March 2003','volts','3.1234'); INSERT 0 1 postgres=# SELECT * FROM crosstab postgres-# ( postgres(# 'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1', postgres(# 'SELECT DISTINCT attribute FROM cth ORDER BY 1' postgres(# ) postgres-# AS postgres-# ( postgres(# rowid text, postgres(# rowdt timestamp, postgres(# temperature int4, postgres(# test_result text, postgres(# test_startdate timestamp, postgres(# volts float8 postgres(# );rowid | rowdt | temperature | test_result | test_startdate | volts -------+---------------------+-------------+-------------+---------------------+--------test1 | 2003-03-01 00:00:00 | 42 | PASS | | 2.6987test2 | 2003-03-02 00:00:00 | 53 | FAIL | 2003-03-01 00:00:00 | 3.1234 (2 rows)
注意:其中的value值是取最后一條: postgres=# INSERT INTO cth VALUES('test1','02 March 2003','temperature','42'); INSERT 0 1 postgres=# INSERT INTO cth VALUES('test1','03 March 2003','temperature','42'); INSERT 0 1 postgres=# INSERT INTO cth VALUES('test1','04 March 2003','temperature','422'); INSERT 0 1 postgres=# select * from cth order by 1,3;
?rowid | ? ? ? ?rowdt ? ? ? ?| ? attribute ? ?| ? ? ?val ? ? ?
-------+---------------------+----------------+---------------
?test1 | 2003-03-01 00:00:00 | temperature ? ?| 42
?test1 | 2003-03-02 00:00:00 | temperature ? ?| 42
?test1 | 2003-03-03 00:00:00 | temperature ? ?| 42
?test1 | 2003-03-04 00:00:00 | temperature ? ?| 422
?test1 | 2003-03-01 00:00:00 | test_result ? ?| PASS
?test1 | 2003-03-01 00:00:00 | volts ? ? ? ? ?| 2.6987
?test2 | 2003-03-02 00:00:00 | temperature ? ?| 53
?test2 | 2003-03-02 00:00:00 | test_result ? ?| FAIL
?test2 | 2003-03-02 00:00:00 | test_startdate | 01 March 2003
?test2 | 2003-03-02 00:00:00 | volts ? ? ? ? ?| 3.1234
(10 rows)
postgres=# SELECT * FROM crosstab postgres-# ( postgres(# 'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1', postgres(# 'SELECT DISTINCT attribute FROM cth ORDER BY 1' postgres(# ) postgres-# AS postgres-# ( postgres(# rowid text, postgres(# rowdt timestamp, postgres(# temperature int4, postgres(# test_result text, postgres(# test_startdate timestamp, postgres(# volts float8 postgres(# );rowid | rowdt | temperature | test_result | test_startdate | volts -------+---------------------+-------------+-------------+---------------------+--------test1 | 2003-03-01 00:00:00 | 422 | PASS | | 2.6987test2 | 2003-03-02 00:00:00 | 53 | FAIL | 2003-03-01 00:00:00 | 3.1234 (2 rows)
?
轉載于:https://www.cnblogs.com/xxvv/p/3766254.html
總結
以上是生活随笔為你收集整理的tablefunc 行转列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux最初配置( vimrc设置
- 下一篇: 算法学习三阶段