PostgreSQL 实现交叉表(行列转换)的五种方法

这里我来演示下在POSTGRESQL里面如何实现交叉表的展示,至于什么是交叉表,我就不多说了,度娘去哦。
原始表数据如下:
[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. t_girl=# select * from score;  
  2.  name  | subject | score   
  3. -------+---------+-------  
  4.  Lucy  | English |   100  
  5.  Lucy  | Physics |    90  
  6.  Lucy  | Math    |    85  
  7.  Lily  | English |    95  
  8.  Lily  | Physics |    81  
  9.  Lily  | Math    |    84  
  10.  David | English |   100  
  11.  David | Physics |    86  
  12.  David | Math    |    89  
  13.  Simon | English |    90  
  14.  Simon | Physics |    76  
  15.  Simon | Math    |    79  
  16. (12 rows)  
  17.   
  18.   
  19. Time: 2.066 ms  




想要实现以下的结果:
[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. name  | English | Physics | Math   
  2. ------+---------+---------+------  
  3. Simon |      90 |      76 |   79  
  4. Lucy  |     100 |      90 |   85  
  5. Lily  |      95 |      81 |   84  
  6. David |     100 |      86 |   89  




大致有以下几种方法:


1、用标准SQL展现出来
[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. t_girl=# select name,   
  2. t_girl-#  sum(case when subject = 'English' then score else 0 endas "English",  
  3. t_girl-#  sum(case when subject = 'Physics' then  score else 0 endas "Physics",  
  4. t_girl-#  sum(case when subject = 'Math'   then score else 0 endas "Math"   
  5. t_girl-#  from score  
  6. t_girl-#  group by name order by name desc;  
  7.  name  | English | Physics | Math   
  8. -------+---------+---------+------  
  9.  Simon |      90 |      76 |   79  
  10.  Lucy  |     100 |      90 |   85  
  11.  Lily  |      95 |      81 |   84  
  12.  David |     100 |      86 |   89  
  13. (4 rows)  
  14.   
  15.   
  16. Time: 1.123 ms  




2、用PostgreSQL 提供的第三方扩展 tablefunc 带来的函数实现
以下函数crosstab 里面的SQL必须有三个字段,name, 分类以及分类值来作为起始参数,必须以name,分类值作为输出参数。
[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. t_girl=# SELECT *  
  2. FROM crosstab('select name,subject,score from score order by name desc',$$values ('English'::text),('Physics'::text),('Math'::text)$$)  
  3. AS score(name text, English int, Physics int, Math int);  
  4.  name  | english | physics | math   
  5. -------+---------+---------+------  
  6.  Simon |      90 |      76 |   79  
  7.  Lucy  |     100 |      90 |   85  
  8.  Lily  |      95 |      81 |   84  
  9.  David |     100 |      86 |   89  
  10. (4 rows)  
  11.   
  12.   
  13. Time: 2.059 ms  




3、用PostgreSQL 自身的聚合函数实现

[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. t_girl=# select name,split_part(split_part(tmp,',',1),':',2) as "English",  
  2. t_girl-# split_part(split_part(tmp,',',2),':',2) as "Physics",  
  3. t_girl-# split_part(split_part(tmp,',',3),':',2) as "Math"  
  4. t_girl-# from  
  5. t_girl-# (  
  6. t_girl(# select name,string_agg(subject||':'||score,','as tmp from score group by name order by name desc  
  7. t_girl(# ) as T;  
  8.  name  | English | Physics | Math   
  9. -------+---------+---------+------  
  10.  Simon | 90      | 76      | 79  
  11.  Lucy  | 100     | 90      | 85  
  12.  Lily  | 95      | 81      | 84  
  13.  David | 100     | 86      | 89  
  14. (4 rows)  
  15.   
  16.   
  17. Time: 2.396 ms  






4、 存储函数实现

[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. create or replace function func_ytt_crosstab_py ()  
  2. returns setof ytt_crosstab  
  3. as   
  4. $ytt$  
  5.   for row in plpy.cursor("select name,string_agg(subject||':'||score,',') as tmp from score group by name order by name desc"):  
  6.       a = row['tmp'].split(',')  
  7.       yield (row['name'],a[0].split(':')[1],a[1].split(':')[1],a[2].split(':')[1])  
  8. $ytt$ language plpythonu;  
  9.   
  10.   
  11. t_girl=# select name,english,physics,math from  func_ytt_crosstab_py();  
  12.  name  | english | physics | math   
  13. -------+---------+---------+------  
  14.  Simon | 90      | 76      | 79  
  15.  Lucy  | 100     | 90      | 85  
  16.  Lily  | 95      | 81      | 84  
  17.  David | 100     | 86      | 89  
  18. (4 rows)  
  19.   
  20.   
  21. Time: 2.687 ms  




5、 用PLPGSQL来实现

[sql]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. t_girl=# create type ytt_crosstab as (name text, English text, Physics text, Math text);  
  2. CREATE TYPE  
  3. Time: 22.518 ms  
  4.   
  5.   
  6. create or replace function func_ytt_crosstab ()  
  7. returns setof ytt_crosstab  
  8. as   
  9. $ytt$  
  10.   declare v_name text := '';  
  11.                 v_english text := '';  
  12.         v_physics text := '';  
  13.         v_math text := '';  
  14.         v_tmp_result text := '';  
  15.   declare cs1 cursor for select name,string_agg(subject||':'||score,','from score group by name order by name desc;  
  16. begin  
  17.   open cs1;  
  18.   loop  
  19.     fetch cs1 into v_name,v_tmp_result;  
  20.     exit when not found;  
  21.     v_english = split_part(split_part(v_tmp_result,',',1),':',2);  
  22.     v_physics = split_part(split_part(v_tmp_result,',',2),':',2);  
  23.     v_math = split_part(split_part(v_tmp_result,',',3),':',2);  
  24.     return query select v_name,v_english,v_physics,v_math;  
  25.   end loop;  
  26. end;  
  27. $ytt$ language plpgsql;  
  28.   
  29.   
  30. t_girl=# select name,English,Physics,Math from func_ytt_crosstab();  
  31.  name  | english | physics | math   
  32. -------+---------+---------+------  
  33.  Simon | 90      | 76      | 79  
  34.  Lucy  | 100     | 90      | 85  
  35.  Lily  | 95      | 81      | 84  
  36.  David | 100     | 86      | 89  
  37. (4 rows)  
  38.   
  39.   
  40. Time: 2.127 ms  

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值