欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

postgresql行轉(zhuǎn)列與列轉(zhuǎn)行圖文教程

 更新時(shí)間:2023年06月10日 11:35:09   作者:Moshow鄭鍇  
PostgreSQL是一種開(kāi)源的關(guān)系型數(shù)據(jù)庫(kù),它提供了多種管理工具來(lái)操作數(shù)據(jù)庫(kù),下面這篇文章主要給大家介紹了關(guān)于postgresql行轉(zhuǎn)列與列轉(zhuǎn)行的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

列轉(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的解釋

FunctionReturn TypeDescriptionExampleResult
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 anyelementexpand 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ō)明

FunctionReturnsDescription
normal_rand(int numvals, float8 mean, float8 stddev)setof float8Produces a set of normally distributed random values(產(chǎn)生一組正態(tài)分布的隨機(jī)值)
crosstab(text sql)setof recordProduces 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_NProduces 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 recordProduces a “pivot table” with the value columns specified by a second query(生成具有由第二個(gè)查詢指定的值列的“數(shù)據(jù)透視表”)
crosstab(text sql, int N)setof recordObsolete 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 recordProduces 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 pg_ctl start啟動(dòng)超時(shí)實(shí)例分析

    PostgreSQL pg_ctl start啟動(dòng)超時(shí)實(shí)例分析

    這篇文章主要給大家介紹了關(guān)于PostgreSQL pg_ctl start啟動(dòng)超時(shí)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • PostgreSQL中常用的時(shí)間日期腳本使用教程

    PostgreSQL中常用的時(shí)間日期腳本使用教程

    PostgreSQL是一款簡(jiǎn)介而又性能強(qiáng)大的數(shù)據(jù)庫(kù)應(yīng)用程序,其在日期時(shí)間數(shù)據(jù)方面所支持的功能也都非常給力,下面就來(lái)看一下PostgreSQL中常用的日期時(shí)間腳本使用教程.
    2016-05-05
  • PostgreSQL 正則表達(dá)式替換-使用變量方式

    PostgreSQL 正則表達(dá)式替換-使用變量方式

    這篇文章主要介紹了PostgreSQL 正則表達(dá)式替換-使用變量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • PostgreSQL數(shù)據(jù)庫(kù)性能調(diào)優(yōu)的注意點(diǎn)以及pg數(shù)據(jù)庫(kù)性能優(yōu)化方式

    PostgreSQL數(shù)據(jù)庫(kù)性能調(diào)優(yōu)的注意點(diǎn)以及pg數(shù)據(jù)庫(kù)性能優(yōu)化方式

    這篇文章主要介紹了PostgreSQL數(shù)據(jù)庫(kù)性能調(diào)優(yōu)的注意點(diǎn)以及pg數(shù)據(jù)庫(kù)性能優(yōu)化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • PostgreSQL處理數(shù)據(jù)并發(fā)更新沖突的解決方法

    PostgreSQL處理數(shù)據(jù)并發(fā)更新沖突的解決方法

    在數(shù)據(jù)庫(kù)并發(fā)操作環(huán)境中,多個(gè)事務(wù)同時(shí)嘗試更新相同的數(shù)據(jù)可能導(dǎo)致沖突,PostgreSQL?提供了一系列機(jī)制來(lái)處理這些并發(fā)更新沖突,以確保數(shù)據(jù)的一致性和完整性,所以本文給大家介紹了PostgreSQL處理數(shù)據(jù)并發(fā)更新沖突的解決方法,需要的朋友可以參考下
    2024-07-07
  • 詳解如何優(yōu)化在PostgreSQL中對(duì)于日期范圍的查詢

    詳解如何優(yōu)化在PostgreSQL中對(duì)于日期范圍的查詢

    在 PostgreSQL 中,處理日期范圍的查詢是常見(jiàn)的操作,然而,如果不進(jìn)行適當(dāng)?shù)膬?yōu)化,這些查詢可能會(huì)導(dǎo)致性能問(wèn)題,特別是在處理大型數(shù)據(jù)集時(shí),本文章將詳細(xì)討論如何優(yōu)化在 PostgreSQL 中對(duì)于日期范圍的查詢,需要的朋友可以參考下
    2024-07-07
  • 修改一行代碼提升 Postgres 性能 100 倍

    修改一行代碼提升 Postgres 性能 100 倍

    在一個(gè)(差)的PostgreSQL 查詢中只要一個(gè)小小到改動(dòng)(ANY(ARRAY[...])to ANY(VALUES(...)))就能把查詢時(shí)間從20s縮減到0.2s
    2013-09-09
  • PostgreSQL中調(diào)用存儲(chǔ)過(guò)程并返回?cái)?shù)據(jù)集實(shí)例

    PostgreSQL中調(diào)用存儲(chǔ)過(guò)程并返回?cái)?shù)據(jù)集實(shí)例

    這篇文章主要介紹了PostgreSQL中調(diào)用存儲(chǔ)過(guò)程并返回?cái)?shù)據(jù)集實(shí)例,本文給出一創(chuàng)建數(shù)據(jù)表、插入測(cè)試數(shù)據(jù)、創(chuàng)建存儲(chǔ)過(guò)程、調(diào)用創(chuàng)建存儲(chǔ)過(guò)程和運(yùn)行效果完整例子,需要的朋友可以參考下
    2015-01-01
  • PostgreSQL 刪除check約束的實(shí)現(xiàn)

    PostgreSQL 刪除check約束的實(shí)現(xiàn)

    這篇文章主要介紹了PostgreSQL 刪除check約束的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • PostgreSQL 如何修改文本類型字段的存儲(chǔ)方式

    PostgreSQL 如何修改文本類型字段的存儲(chǔ)方式

    這篇文章主要介紹了PostgreSQL 如何修改文本類型字段的存儲(chǔ)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12

最新評(píng)論