postgresql行轉(zhuǎn)列與列轉(zhuǎn)行圖文教程
列轉(zhuǎn)行
postgresql列轉(zhuǎn)行的思路主要是利用string_to_array
進行數(shù)組轉(zhuǎn)換,然后用unnest
進行行拆分
select t.bid_unit,unit_id from unit t where t.unit_id=1947; result=> 中國信息通信研究院;北京市海淀區(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=> 中國信息通信研究院 北京市海淀區(qū)學(xué)院 -- by zhengkai.blog.csdn.net
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ù)組展開到一組行) | 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)
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)分布的隨機值) |
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(生成一個包含行名和N個值列的“數(shù)據(jù)透視表”,其中N個由調(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(生成一個包含行名和N個值列的“數(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(生成具有由第二個查詢指定的值列的“數(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(過時版本的交叉表(文本)。參數(shù)N現(xià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(生成層次樹結(jié)構(gòu)的表示) |
總結(jié)
到此這篇關(guān)于postgresql行轉(zhuǎn)列與列轉(zhuǎn)行的文章就介紹到這了,更多相關(guān)postgresql行轉(zhuǎn)列 列轉(zhuǎn)行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PostgreSQL pg_ctl start啟動超時實例分析
這篇文章主要給大家介紹了關(guān)于PostgreSQL pg_ctl start啟動超時的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01PostgreSQL數(shù)據(jù)庫性能調(diào)優(yōu)的注意點以及pg數(shù)據(jù)庫性能優(yōu)化方式
這篇文章主要介紹了PostgreSQL數(shù)據(jù)庫性能調(diào)優(yōu)的注意點以及pg數(shù)據(jù)庫性能優(yōu)化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03PostgreSQL處理數(shù)據(jù)并發(fā)更新沖突的解決方法
在數(shù)據(jù)庫并發(fā)操作環(huán)境中,多個事務(wù)同時嘗試更新相同的數(shù)據(jù)可能導(dǎo)致沖突,PostgreSQL?提供了一系列機制來處理這些并發(fā)更新沖突,以確保數(shù)據(jù)的一致性和完整性,所以本文給大家介紹了PostgreSQL處理數(shù)據(jù)并發(fā)更新沖突的解決方法,需要的朋友可以參考下2024-07-07詳解如何優(yōu)化在PostgreSQL中對于日期范圍的查詢
在 PostgreSQL 中,處理日期范圍的查詢是常見的操作,然而,如果不進行適當(dāng)?shù)膬?yōu)化,這些查詢可能會導(dǎo)致性能問題,特別是在處理大型數(shù)據(jù)集時,本文章將詳細討論如何優(yōu)化在 PostgreSQL 中對于日期范圍的查詢,需要的朋友可以參考下2024-07-07PostgreSQL中調(diào)用存儲過程并返回數(shù)據(jù)集實例
這篇文章主要介紹了PostgreSQL中調(diào)用存儲過程并返回數(shù)據(jù)集實例,本文給出一創(chuàng)建數(shù)據(jù)表、插入測試數(shù)據(jù)、創(chuàng)建存儲過程、調(diào)用創(chuàng)建存儲過程和運行效果完整例子,需要的朋友可以參考下2015-01-01