PostgreSQL實(shí)現(xiàn)交叉表(行列轉(zhuǎn)換)的5種方法示例
交叉表
交叉表(Cross Tabulations)是一種常用的分類匯總表格。使用交叉表查詢,顯示源于表中某個字段的匯總值,并將它們分組,其中一組列在數(shù)據(jù)表的左側(cè),另一組列在數(shù)據(jù)表的上部。行和列的交叉處可以對數(shù)據(jù)進(jìn)行多種匯總計(jì)算,如:求和、平均值、記數(shù)、最大值、最小值等。使用交叉表查詢數(shù)據(jù)非常直觀明了,被廣泛應(yīng)用。交叉表查詢也是數(shù)據(jù)庫的一個特點(diǎn)。
例如:
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
--這種就是交叉表查詢
交叉報表是報表當(dāng)中常見的類型,屬于基本的報表,是行、列方向都有分組的報表。這里牽涉到另外一個概念即分組報表。這是所有報表當(dāng)中最普通,最常見的報表類型,也是所有報表工具都支持的一種報表格式。從一般概念上來講,分組報表就是只有縱向的分組。傳統(tǒng)的分組報表制作方式是把報表劃分為條帶狀,用戶根據(jù)一個數(shù)據(jù)綁定向?qū)е付ǚ纸M,匯總字段,生成標(biāo)準(zhǔn)的分組報表。
這里我來演示下在POSTGRESQL里面如何實(shí)現(xiàn)交叉表的展示,下面話不多說了,來一起看看詳細(xì)的介紹吧
原始表數(shù)據(jù)如下:
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
想要實(shí)現(xiàn)以下的結(jié)果:
name | English | Physics | Math ------+---------+---------+------ Simon | 90 | 76 | 79 Lucy | 100 | 90 | 85 Lily | 95 | 81 | 84 David | 100 | 86 | 89
大致有以下幾種方法:
1、用標(biāo)準(zhǔn)SQL展現(xiàn)出來
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 提供的第三方擴(kuò)展 tablefunc 帶來的函數(shù)實(shí)現(xiàn)
以下函數(shù)crosstab 里面的SQL必須有三個字段,name, 分類以及分類值來作為起始參數(shù),必須以name,分類值作為輸出參數(shù)。
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 自身的聚合函數(shù)實(shí)現(xiàn)
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、 存儲函數(shù)實(shí)現(xiàn)
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來實(shí)現(xiàn)
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
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- sql 普通行列轉(zhuǎn)換
- 一個簡單的SQL 行列轉(zhuǎn)換語句
- sqlserver2005 行列轉(zhuǎn)換實(shí)現(xiàn)方法
- Sql實(shí)現(xiàn)行列轉(zhuǎn)換方便了我們存儲數(shù)據(jù)和呈現(xiàn)數(shù)據(jù)
- 玩轉(zhuǎn)-SQL2005數(shù)據(jù)庫行列轉(zhuǎn)換
- 深入SQL中PIVOT 行列轉(zhuǎn)換詳解
- sql server通過pivot對數(shù)據(jù)進(jìn)行行列轉(zhuǎn)換的方法
- SQL Server 使用 Pivot 和 UnPivot 實(shí)現(xiàn)行列轉(zhuǎn)換的問題小結(jié)
- SQL Server使用PIVOT與unPIVOT實(shí)現(xiàn)行列轉(zhuǎn)換
- MySQL實(shí)現(xiàn)行列轉(zhuǎn)換
- SQL行列轉(zhuǎn)換超詳細(xì)四種方法詳解
- SQL Server行列轉(zhuǎn)換的實(shí)現(xiàn)示例
- SQLServer使用 PIVOT 和 UNPIVOT行列轉(zhuǎn)換
相關(guān)文章
詳解如何定位postgreSQL數(shù)據(jù)庫中未被使用過的索引
在生產(chǎn)環(huán)境上,由于不規(guī)范的優(yōu)化措施,數(shù)據(jù)庫中可能存在大量的索引,并且相當(dāng)一部分的索引重未被使用過,今天帶大家如何找出這些索引,本文給大家介紹了定位postgreSQL數(shù)據(jù)庫中未被使用過的索引的方法,需要的朋友可以參考下2024-03-03
PostgreSQL查詢和處理JSON數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于PostgreSQL查詢和處理JSON數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2023-11-11
postgreSQL數(shù)據(jù)庫默認(rèn)用戶postgres常用命令分享
這篇文章主要介紹了postgreSQL數(shù)據(jù)庫默認(rèn)用戶postgres常用命令分享,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
postgresql修改完端口后直接psql連接數(shù)據(jù)庫報錯的解決
這篇文章主要介紹了postgresql修改完端口后直接psql連接數(shù)據(jù)庫報錯的解決,具有很好的參考價值,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
PostgreSQL數(shù)據(jù)庫遷移部署實(shí)戰(zhàn)教程
這篇文章主要介紹了PostgreSQL數(shù)據(jù)庫遷移部署實(shí)戰(zhàn)教程,由于項(xiàng)目本身就是基于PostgreSQL數(shù)據(jù)庫構(gòu)建的,因此數(shù)據(jù)庫遷移將變得十分便捷,接下來,我將簡要介紹我們的遷移步驟,需要的朋友可以參考下2023-07-07
在PostgreSQL中實(shí)現(xiàn)跨數(shù)據(jù)庫的關(guān)聯(lián)查詢
在 PostgreSQL 中,通常情況下的關(guān)聯(lián)查詢是在同一個數(shù)據(jù)庫的不同表之間進(jìn)行的,然而,在某些復(fù)雜的應(yīng)用場景中,可能需要實(shí)現(xiàn)跨數(shù)據(jù)庫的關(guān)聯(lián)查詢,本文將詳細(xì)探討如何在 PostgreSQL 中實(shí)現(xiàn)這一需求,并通過示例代碼進(jìn)行說明,需要的朋友可以參考下2024-08-08

