亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2023年06月10日 11:35:09   作者:Moshow鄭鍇  
PostgreSQL是一種開源的關(guān)系型數(shù)據(jù)庫,它提供了多種管理工具來操作數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關(guān)于postgresql行轉(zhuǎn)列與列轉(zhuǎn)行的相關(guā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

pgsql官方對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ù)組展開到一組行)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實用說明

FunctionReturnsDescription
normal_rand(int numvals, float8 mean, float8 stddev)setof float8Produces a set of normally distributed random values(產(chǎn)生一組正態(tài)分布的隨機值)
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(生成一個包含行名和N個值列的“數(shù)據(jù)透視表”,其中N個由調(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(生成一個包含行名和N個值列的“數(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(生成具有由第二個查詢指定的值列的“數(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(過時版本的交叉表(文本)。參數(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 recordProduces 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啟動超時實例分析

    PostgreSQL pg_ctl start啟動超時實例分析

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

    PostgreSQL中常用的時間日期腳本使用教程

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

    PostgreSQL 正則表達式替換-使用變量方式

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

    PostgreSQL數(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-03
  • PostgreSQL處理數(shù)據(jù)并發(fā)更新沖突的解決方法

    PostgreSQL處理數(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中對于日期范圍的查詢

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

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

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

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

    PostgreSQL中調(diào)用存儲過程并返回數(shù)據(jù)集實例

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

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

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

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

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

最新評論