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  

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Cache Fusion 是 Oracle 数据库的一种内存共享技术,它允许多个实例共享同一个数据块缓存区域,以提高系统性能。在 PostgreSQL 中,实现类似的功能需要使用共享内存机制。 具体实现步骤如下: 1. 创建共享内存区域 使用 shmget() 系统调用创建一个共享内存区域,该区域用于存储缓存块信息。可以使用一个结构体来描述缓存块,包括块号、使用计数、锁等信息。 2. 为共享内存区域附加进程 使用 shmat() 系统调用将共享内存区域附加到当前进程的地址空间中,以便对其进行读写操作。 3. 启动多个 PostgreSQL 实例 启动多个 PostgreSQL 实例,它们将共享同一个缓存块区域。 4. 在每个实例中实现缓存块锁定机制 由于多个实例共享同一个缓存块区域,因此需要使用锁定机制来保证数据的一致性。可以使用信号量机制实现缓存块的互斥访问。 5. 实现缓存块的读写操作 在每个实例中实现缓存块的读写操作。当一个实例需要读取一个缓存块时,它需要先检查缓存块是否已经被锁定。如果缓存块已经被锁定,则需要等待锁释放后再进行读取操作。如果缓存块没有被锁定,则可以直接读取数据。当一个实例需要写入一个缓存块时,它必须先锁定该缓存块,然后进行写入操作,最后释放锁。 6. 实现缓存块的更新机制 当一个实例对缓存块进行写入操作后,需要通知其他实例更新该缓存块。可以使用共享内存中的标志位来实现缓存块的更新机制。当一个实例对缓存块进行写入操作后,它将设置标志位,其他实例在读取该缓存块时,会检查标志位并进行更新。 7. 实现缓存块的回收机制 当一个缓存块长时间没有被访问时,它可以被回收以释放内存。可以使用 LRU(Least Recently Used)算法来实现缓存块的回收机制。当一个缓存块长时间没有被访问时,它将被标记为“未使用”,当系统需要释放内存时,将优先回收这些“未使用”的缓存块。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值