最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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中的內(nèi)連接和外連接實(shí)現(xià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拼接字符串的幾種方法簡(jiǎn)單示例

    在PostgreSQL中有多種方式可以拼接字符串,這篇文章主要給大家介紹了關(guān)于PostgreSQL拼接字符串的幾種方法,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • PostgreSQL 基于 inherits 實(shí)現(xiàn)分表的示例代碼

    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長(zhǎng)事務(wù)概念解析

    PostgreSQL長(zhǎng)事務(wù)概念解析

    pg中的長(zhǎng)事務(wù)會(huì)影響表中垃圾回收,導(dǎo)致表的年齡增長(zhǎng)無(wú)法freeze。能消耗事務(wù)的只有當(dāng)執(zhí)行了一些DML或者DDL操作后才能算是我們通常說(shuō)的長(zhǎng)事務(wù)。否則只能算是我們常說(shuō)的長(zhǎng)連接,當(dāng)然長(zhǎng)連接也有很多弊端,例如占用內(nèi)存、cpu等資源
    2022-09-09
  • PostgreSQL之連接失敗的問(wèn)題及解決

    PostgreSQL之連接失敗的問(wèn)題及解決

    這篇文章主要介紹了PostgreSQL之連接失敗的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • postgresql的jsonb數(shù)據(jù)查詢和修改的方法

    postgresql的jsonb數(shù)據(jù)查詢和修改的方法

    這篇文章主要介紹了postgresql的jsonb數(shù)據(jù)查詢和修改的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • PostgreSQL實(shí)現(xiàn)數(shù)據(jù)表跨庫(kù)同步的四種方案

    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ù)類型詳解

    這篇文章主要介紹了PostgreSQL教程(四):數(shù)據(jù)類型詳解,本文講解了數(shù)值類型、字符類型、布爾類型、位串類型、數(shù)組、復(fù)合類型等數(shù)據(jù)類型,需要的朋友可以參考下
    2015-05-05
  • PostgreSQL 使用raise函數(shù)打印字符串

    PostgreSQL 使用raise函數(shù)打印字符串

    這篇文章主要介紹了PostgreSQL 使用raise函數(shù)打印字符串,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • Windows下Postgresql數(shù)據(jù)庫(kù)的下載與配置方法

    Windows下Postgresql數(shù)據(jù)庫(kù)的下載與配置方法

    這篇文章主要介紹了Windows下Postgresql數(shù)據(jù)庫(kù)的下載與配置方法 ,需要的朋友可以參考下
    2014-06-06

最新評(píng)論

伊宁县| 靖宇县| 陆川县| 米林县| 黑河市| 汽车| 红河县| 固镇县| 奇台县| 平顶山市| 桑植县| 河津市| 广丰县| 新和县| 松阳县| 呼玛县| 安岳县| 互助| 凤台县| 阿城市| 博罗县| 广河县| 泽州县| 六安市| 紫金县| 永登县| 静海县| 朝阳县| 宝应县| 香河县| 介休市| 宁强县| 新田县| 柞水县| 上高县| 武穴市| 金沙县| 黔江区| 谷城县| 夏津县| 三台县|