postgresql行轉(zhuǎn)列與列轉(zhuǎn)行圖文教程
列轉(zhuǎn)行
postgresql列轉(zhuǎn)行的思路主要是利用string_to_array進(jìn)行數(shù)組轉(zhuǎn)換,然后用unnest進(jìn)行行拆分

select t.bid_unit,unit_id from unit t where t.unit_id=1947; result=> 中國(guó)信息通信研究院;北京市海淀區(qū)學(xué)院 -- by zhengkai.blog.csdn.net

select unnest(string_to_array(t.bid_unit,';')),unit_id from unit t where t.unit_id=1947; result=> 中國(guó)信息通信研究院 北京市海淀區(qū)學(xué)院 -- by zhengkai.blog.csdn.net
pgsql官方對(duì)functions-array的解釋
| Function | Return Type | Description | Example | Result |
|---|---|---|---|---|
| string_to_array(text, text [, text]) | text[] | splits string into array elements using supplied delimiter and optional null string (使用提供的分隔符和可選的空字符串將字符串分割為數(shù)組元素) | string_to_array(‘xx^yy^zz’, ‘^’, ‘yy’) | {xx,NULL,zz} |
| unnest(anyarray) | setof anyelement | expand an array to a set of rows(將數(shù)組展開(kāi)到一組行) | unnest(ARRAY[1,2]) | 1 2 (2 rows) |
行轉(zhuǎn)列
用postgresql的crosstab交叉函數(shù)
-- by zhengkai.blog.csdn.net create table sales(year int, month int, qty int); insert into sales values(2022, 1, 1000); insert into sales values(2022, 2, 1500); insert into sales values(2022, 7, 500); insert into sales values(2022, 11, 1500); insert into sales values(2022, 12, 2000); insert into sales values(2023, 1, 1200); select * from crosstab( 'select year, month, qty from sales order by 1', 'select m from generate_series(1,12) m' ) as ( year int, "Jan" int, "Feb" int, "Mar" int, "Apr" int, "May" int, "Jun" int, "Jul" int, "Aug" int, "Sep" int, "Oct" int, "Nov" int, "Dec" int ); year | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec ------+------+------+-----+-----+-----+-----+-----+-----+-----+-----+------+------ 2022 | 1000 | 1500 | | | | | 500 | | | | 1500 | 2000 2023 | 1200 | | | | | | | | | | | (2 rows)
可以參考pgsql官方的tablefunc實(shí)用說(shuō)明
| Function | Returns | Description |
|---|---|---|
| normal_rand(int numvals, float8 mean, float8 stddev) | setof float8 | Produces a set of normally distributed random values(產(chǎn)生一組正態(tài)分布的隨機(jī)值) |
| crosstab(text sql) | setof record | Produces a “pivot table” containing row names plus N value columns, where N is determined by the row type specified in the calling query(生成一個(gè)包含行名和N個(gè)值列的“數(shù)據(jù)透視表”,其中N個(gè)由調(diào)用查詢中指定的行類型決定) |
| crosstabN(text sql) | setof table_crosstab_N | Produces a “pivot table” containing row names plus N value columns. crosstab2, crosstab3, and crosstab4 are predefined, but you can create additional crosstabN functions as described below(生成一個(gè)包含行名和N個(gè)值列的“數(shù)據(jù)透視表”。交叉表2、交叉表3和交叉表4都是預(yù)定義的,但是您可以創(chuàng)建額外的跨表n函數(shù),如下面所述) |
| crosstab(text source_sql, text category_sql) | setof record | Produces a “pivot table” with the value columns specified by a second query(生成具有由第二個(gè)查詢指定的值列的“數(shù)據(jù)透視表”) |
| crosstab(text sql, int N) | setof record | Obsolete version of crosstab(text). The parameter N is now ignored, since the number of value columns is always determined by the calling query(過(guò)時(shí)版本的交叉表(文本)。參數(shù)N現(xiàn)在被忽略,因?yàn)橹盗械臄?shù)量總是由調(diào)用查詢決定) |
| connectby(text relname, text keyid_fld, text parent_keyid_fld [, text orderby_fld ], text start_with, int max_depth [, text branch_delim ]) | setof record | Produces a representation of a hierarchical tree structure(生成層次樹(shù)結(jié)構(gòu)的表示) |
總結(jié)
到此這篇關(guān)于postgresql行轉(zhuǎn)列與列轉(zhuǎn)行的文章就介紹到這了,更多相關(guān)postgresql行轉(zhuǎn)列 列轉(zhuǎn)行內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
postgreSQL中的內(nèi)連接和外連接實(shí)現(xiàn)操作
這篇文章主要介紹了postgreSQL中的內(nèi)連接和外連接實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
PostgreSQL拼接字符串的幾種方法簡(jiǎn)單示例
在PostgreSQL中有多種方式可以拼接字符串,這篇文章主要給大家介紹了關(guān)于PostgreSQL拼接字符串的幾種方法,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
PostgreSQL 基于 inherits 實(shí)現(xiàn)分表的示例代碼
本文主要介紹了PostgreSQL 基于 inherits 實(shí)現(xiàn)分表的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2026-05-05
postgresql的jsonb數(shù)據(jù)查詢和修改的方法
這篇文章主要介紹了postgresql的jsonb數(shù)據(jù)查詢和修改的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
PostgreSQL實(shí)現(xiàn)數(shù)據(jù)表跨庫(kù)同步的四種方案
文章詳細(xì)介紹了四種用于實(shí)現(xiàn)PostgreSQL數(shù)據(jù)庫(kù)表數(shù)據(jù)變化實(shí)時(shí)或定時(shí)同步到另一個(gè)獨(dú)立PG庫(kù)的方法,包括觸發(fā)器+外部表、邏輯復(fù)制、Debezium+Kafka和pg_dump定時(shí)任務(wù),針對(duì)不同場(chǎng)景和需求,推薦了最適合的方案,并詳細(xì)闡述了各自的優(yōu)點(diǎn)、適用場(chǎng)景、配置方法和使用注意事項(xiàng)2026-05-05
PostgreSQL教程(四):數(shù)據(jù)類型詳解
這篇文章主要介紹了PostgreSQL教程(四):數(shù)據(jù)類型詳解,本文講解了數(shù)值類型、字符類型、布爾類型、位串類型、數(shù)組、復(fù)合類型等數(shù)據(jù)類型,需要的朋友可以參考下2015-05-05
PostgreSQL 使用raise函數(shù)打印字符串
這篇文章主要介紹了PostgreSQL 使用raise函數(shù)打印字符串,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
Windows下Postgresql數(shù)據(jù)庫(kù)的下載與配置方法
這篇文章主要介紹了Windows下Postgresql數(shù)據(jù)庫(kù)的下載與配置方法 ,需要的朋友可以參考下2014-06-06

