PostgreSQL的行轉列函數使用一例

來源:互聯網
上載者:User

開發同事說使用postgres的擴充行轉列應用時有一個問題,樣本如下

該擴充包初步使用參考:

一、環境
OS:CentOS 6.3
DB:PostgreSQL 9.3.0

二、情境

create table t(day date,equipment varchar(20),output integer);
insert into t values('2010-04-01','DAT501',100);
insert into t values('2010-04-01','DAT502',120);
insert into t values('2010-04-01','DAT503',130);
insert into t values('2010-04-02','DAT501',110);
insert into t values('2010-04-02','DAT502',105);
insert into t values('2010-04-03','DAT503',125);
insert into t values('2010-04-04','DAT501',100);
insert into t values('2010-04-04','DAT503',200);
--想得到如下結果
    day    | dat501 | dat502 | dat503
------------+--------+--------+--------
 2010-04-01 |    100 |    120 |    130
 2010-04-02 |    110 |    105 |     
 2010-04-03 |        |        |    125
 2010-04-04 |    100 |        |    200   
(4 rows)

--但是直接使用crosstab會導致第3,4行不準確,也就是說中間項為Null就會不準
test=# SELECT * FROM crosstab('select day, equipment, output from t order by 1,2')  AS t(day date, DAT501 integer, DAT502 integer,DAT503 integer);
    day    | dat501 | dat502 | dat503
------------+--------+--------+--------
 2010-04-01 |    100 |    120 |    130
 2010-04-02 |    110 |    105 |     
 2010-04-03 |    125 |        |     
 2010-04-04 |    100 |    200 |     
(4 rows)

三、解決
crosstab還有一個包含兩個輸入參數的用法,用這個可以解決上述問題
test=# SELECT * FROM crosstab('select day, equipment, output from t order by 1,2',$$values('DAT501'::text),('DAT502'::text),('DAT503'::text)$$)  AS t(day date, DAT501 integer, DAT502 integer,DAT503 integer);
    day    | dat501 | dat502 | dat503
------------+--------+--------+--------
 2010-04-01 |    100 |    120 |    130
 2010-04-02 |    110 |    105 |     
 2010-04-03 |        |        |    125
 2010-04-04 |    100 |        |    200
(4 rows)--其他的寫法,本質都一樣
test=# SELECT * FROM crosstab('select day, equipment, output from t order by 1','select distinct equipment  from t order by 1')  AS t(day date, DAT501 integer, DAT502 integer,DAT503 integer);
    day    | dat501 | dat502 | dat503
------------+--------+--------+--------
 2010-04-01 |    100 |    120 |    130
 2010-04-02 |    110 |    105 |     
 2010-04-03 |        |        |    125
 2010-04-04 |    100 |        |    200
(4 rows)

四、說明
使用兩個參數的crosstab其實更安全,推薦使用,其基礎用法是

crosstab(text source_sql, text category_sql)
 
參考:
http://www.postgresql.org/docs/9.2/static/tablefunc.html

PostgreSQL 9.3物化視圖使用

使用 PostgreSQL 資料庫日期類型的 4 個提示

PostgreSQL刪除表中重複資料行

PostgreSQL緩衝詳述

Windows平台編譯 PostgreSQL

Ubuntu下LAPP(Linux+Apache+PostgreSQL+PHP)環境的配置與安裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.