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

PostgreSQL實現交叉表(行列轉換)的5種方法示例

 更新時間:2018年08月26日 15:18:49   作者:Hason_Huang  
這篇文章主要給大家介紹了關于PostgreSQL實現交叉表(行列轉換)的5種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

交叉表

交叉表(Cross Tabulations)是一種常用的分類匯總表格。使用交叉表查詢,顯示源于表中某個字段的匯總值,并將它們分組,其中一組列在數據表的左側,另一組列在數據表的上部。行和列的交叉處可以對數據進行多種匯總計算,如:求和、平均值、記數、最大值、最小值等。使用交叉表查詢數據非常直觀明了,被廣泛應用。交叉表查詢也是數據庫的一個特點。

例如:

  select 表1.組名,
  (select 表1.成員姓名 from 表2 b where 表1.成員1id=表2.成員id) as 成員1id,
  (select 表1.成員姓名 from 表2 b where 表1.成員2id=表2.成員id) as 成員2id,
  (select 表1.成員姓名 from 表2 b where 表1.成員3id=表2.成員id) as 成員3id
  from 表1,表2
  --這種就是交叉表查詢

交叉報表是報表當中常見的類型,屬于基本的報表,是行、列方向都有分組的報表。這里牽涉到另外一個概念即分組報表。這是所有報表當中最普通,最常見的報表類型,也是所有報表工具都支持的一種報表格式。從一般概念上來講,分組報表就是只有縱向的分組。傳統(tǒng)的分組報表制作方式是把報表劃分為條帶狀,用戶根據一個數據綁定向導指定分組,匯總字段,生成標準的分組報表。

這里我來演示下在POSTGRESQL里面如何實現交叉表的展示,下面話不多說了,來一起看看詳細的介紹吧

原始表數據如下:

t_girl=# select * from score; 
 name | subject | score 
-------+---------+------- 
 Lucy | English | 100 
 Lucy | Physics | 90 
 Lucy | Math | 85 
 Lily | English | 95 
 Lily | Physics | 81 
 Lily | Math | 84 
 David | English | 100 
 David | Physics | 86 
 David | Math | 89 
 Simon | English | 90 
 Simon | Physics | 76 
 Simon | Math | 79 
(12 rows) 
 
 
Time: 2.066 ms 

想要實現以下的結果:

name | English | Physics | Math 
------+---------+---------+------ 
Simon |  90 |  76 | 79 
Lucy |  100 |  90 | 85 
Lily |  95 |  81 | 84 
David |  100 |  86 | 89 

大致有以下幾種方法:

1、用標準SQL展現出來

t_girl=# select name, 
t_girl-# sum(case when subject = 'English' then score else 0 end) as "English", 
t_girl-# sum(case when subject = 'Physics' then score else 0 end) as "Physics", 
t_girl-# sum(case when subject = 'Math' then score else 0 end) as "Math" 
t_girl-# from score 
t_girl-# group by name order by name desc; 
 name | English | Physics | Math 
-------+---------+---------+------ 
 Simon |  90 |  76 | 79 
 Lucy |  100 |  90 | 85 
 Lily |  95 |  81 | 84 
 David |  100 |  86 | 89 
(4 rows) 
 
 
Time: 1.123 ms 

2、用PostgreSQL 提供的第三方擴展 tablefunc 帶來的函數實現

以下函數crosstab 里面的SQL必須有三個字段,name, 分類以及分類值來作為起始參數,必須以name,分類值作為輸出參數。

t_girl=# SELECT * 
FROM crosstab('select name,subject,score from score order by name desc',$$values ('English'::text),('Physics'::text),('Math'::text)$$) 
AS score(name text, English int, Physics int, Math int); 
 name | english | physics | math 
-------+---------+---------+------ 
 Simon |  90 |  76 | 79 
 Lucy |  100 |  90 | 85 
 Lily |  95 |  81 | 84 
 David |  100 |  86 | 89 
(4 rows) 
 
 
Time: 2.059 ms 

3、用PostgreSQL 自身的聚合函數實現

t_girl=# select name,split_part(split_part(tmp,',',1),':',2) as "English", 
t_girl-# split_part(split_part(tmp,',',2),':',2) as "Physics", 
t_girl-# split_part(split_part(tmp,',',3),':',2) as "Math" 
t_girl-# from 
t_girl-# ( 
t_girl(# select name,string_agg(subject||':'||score,',') as tmp from score group by name order by name desc 
t_girl(# ) as T; 
 name | English | Physics | Math 
-------+---------+---------+------ 
 Simon | 90  | 76  | 79 
 Lucy | 100  | 90  | 85 
 Lily | 95  | 81  | 84 
 David | 100  | 86  | 89 
(4 rows) 
 
 
Time: 2.396 ms 

4、 存儲函數實現

create or replace function func_ytt_crosstab_py () 
returns setof ytt_crosstab 
as 
$ytt$ 
 for row in plpy.cursor("select name,string_agg(subject||':'||score,',') as tmp from score group by name order by name desc"): 
  a = row['tmp'].split(',') 
  yield (row['name'],a[0].split(':')[1],a[1].split(':')[1],a[2].split(':')[1]) 
$ytt$ language plpythonu; 
 
 
t_girl=# select name,english,physics,math from func_ytt_crosstab_py(); 
 name | english | physics | math 
-------+---------+---------+------ 
 Simon | 90  | 76  | 79 
 Lucy | 100  | 90  | 85 
 Lily | 95  | 81  | 84 
 David | 100  | 86  | 89 
(4 rows) 
 
 
Time: 2.687 ms 

5、 用PLPGSQL來實現

t_girl=# create type ytt_crosstab as (name text, English text, Physics text, Math text); 
CREATE TYPE 
Time: 22.518 ms 
 
 
create or replace function func_ytt_crosstab () 
returns setof ytt_crosstab 
as 
$ytt$ 
 declare v_name text := ''; 
    v_english text := ''; 
  v_physics text := ''; 
  v_math text := ''; 
  v_tmp_result text := ''; 
 declare cs1 cursor for select name,string_agg(subject||':'||score,',') from score group by name order by name desc; 
begin 
 open cs1; 
 loop 
 fetch cs1 into v_name,v_tmp_result; 
 exit when not found; 
 v_english = split_part(split_part(v_tmp_result,',',1),':',2); 
 v_physics = split_part(split_part(v_tmp_result,',',2),':',2); 
 v_math = split_part(split_part(v_tmp_result,',',3),':',2); 
 return query select v_name,v_english,v_physics,v_math; 
 end loop; 
end; 
$ytt$ language plpgsql; 
 
 
t_girl=# select name,English,Physics,Math from func_ytt_crosstab(); 
 name | english | physics | math 
-------+---------+---------+------ 
 Simon | 90  | 76  | 79 
 Lucy | 100  | 90  | 85 
 Lily | 95  | 81  | 84 
 David | 100  | 86  | 89 
(4 rows) 
 
 
Time: 2.127 ms 

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

  • postgresql行轉列與列轉行圖文教程

    postgresql行轉列與列轉行圖文教程

    PostgreSQL是一種開源的關系型數據庫,它提供了多種管理工具來操作數據庫,下面這篇文章主要給大家介紹了關于postgresql行轉列與列轉行的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-06-06
  • Docker修改Postgresql密碼的方法詳解

    Docker修改Postgresql密碼的方法詳解

    在Docker環(huán)境中,對已運行的PostgreSQL數據庫實例進行密碼更改是一項常見的維護操作,下面將詳述如何通過一系列命令行操作來實現這一目標,需要的朋友可以參考下
    2024-07-07
  • postgreSQL 數字與字符串類型轉換操作

    postgreSQL 數字與字符串類型轉換操作

    這篇文章主要介紹了postgreSQL 數字與字符串類型轉換操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • postgresql實現對已有數據表分區(qū)處理的操作詳解

    postgresql實現對已有數據表分區(qū)處理的操作詳解

    這篇文章主要為大家詳細介紹了postgresql實現對已有數據表分區(qū)處理的操作的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-12-12
  • PostgreSQL數據庫中DISTINCT關鍵字的四種用法詳解

    PostgreSQL數據庫中DISTINCT關鍵字的四種用法詳解

    PostgreSQL 不但高度兼容 SQL 標準,同時還對很多語法進行了擴展,可以用于實現一些特殊的功能,今天我們就來介紹一下 PostgreSQL 數據庫中 DISTINCT 關鍵字的 4 種不同用法,需要的朋友可以參考下
    2024-04-04
  • postgres之jsonb屬性的使用操作

    postgres之jsonb屬性的使用操作

    這篇文章主要介紹了postgres之jsonb屬性的使用操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 使用pg_basebackup對Postgre進行備份與恢復的實現

    使用pg_basebackup對Postgre進行備份與恢復的實現

    這篇文章主要介紹了使用pg_basebackup對Postgre進行備份與恢復的實現操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • postgresql數據庫表ID自增的實現代碼

    postgresql數據庫表ID自增的實現代碼

    postgresql數據庫可以創(chuàng)建主鍵,但是沒有像mysql那樣直接指定主鍵自增的auto_increment關鍵字,因此如果在postgresql中創(chuàng)建表指定主鍵自增使用auto_increment會報錯,本文通過一個實例給大家演示自增ID的實現,需要的朋友可以參考下
    2023-12-12
  • PgSQl臨時表創(chuàng)建及應用實例解析

    PgSQl臨時表創(chuàng)建及應用實例解析

    這篇文章主要介紹了PgSQl臨時表創(chuàng)建及應用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-12-12
  • 淺談PostgreSQL的客戶端認證pg_hba.conf

    淺談PostgreSQL的客戶端認證pg_hba.conf

    這篇文章主要介紹了淺談PostgreSQL的客戶端認證pg_hba.conf,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01

最新評論

泾源县| 丰原市| 师宗县| 柳州市| 扎兰屯市| 西安市| 云霄县| 色达县| 吉木乃县| 宜都市| 长宁县| 会理县| 福泉市| 彰化县| 霍城县| 天峻县| 鄂托克前旗| 牟定县| 高碑店市| 塔河县| 萨迦县| 抚顺县| 神池县| 墨江| 馆陶县| 东乌| 通城县| 郸城县| 伊川县| 岳西县| 平谷区| 盐津县| 宣恩县| 康保县| 雷波县| 九江县| 海城市| 巧家县| 武平县| 甘孜| 铜陵市|