这里我来演示下在POSTGRESQL里面如何实现交叉表的展示,至于什么是交叉表,我就不多说了,度娘去哦。
原始表数据如下:

点击(此处)折叠或打开

  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. Time: 2.066 ms




想要实现以下的结果:
 

点击(此处)折叠或打开

  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展现出来

点击(此处)折叠或打开

  1. t_girl=# select name,
  2. t_girl-# sum(case when subject = 'English' then score else 0 end) as "English",
  3. t_girl-# sum(case when subject = 'Physics' then score else 0 end) as "Physics",
  4. t_girl-# sum(case when subject = 'Math' then score else 0 end) as "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. Time: 1.123 ms



2、用PostgreSQL 提供的第三方扩展 tablefunc 带来的函数实现
以下函数crosstab 里面的SQL必须有三个字段,name, 分类以及分类值来作为起始参数,必须以name,分类值作为输出参数。

点击(此处)折叠或打开

  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. Time: 2.059 ms




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

点击(此处)折叠或打开

  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. Time: 2.396 ms






4、 存储函数实现

点击(此处)折叠或打开

  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. t_girl=# select name,english,physics,math from func_ytt_crosstab_py();
  10.  name | english | physics | math
  11. -------+---------+---------+------
  12.  Simon | 90 | 76 | 79
  13.  Lucy | 100 | 90 | 85
  14.  Lily | 95 | 81 | 84
  15.  David | 100 | 86 | 89
  16. (4 rows)


  17. Time: 2.687 ms





5、 用PLPGSQL来实现

点击(此处)折叠或打开

  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. create or replace function func_ytt_crosstab ()
  5. returns setof ytt_crosstab
  6. as
  7. $ytt$
  8.   declare v_name text := '';
  9.                 v_english text := '';
  10. v_physics text := '';
  11. v_math text := '';
  12. v_tmp_result text := '';
  13.   declare cs1 cursor for select name,string_agg(subject||':'||score,',') from score group by name order by name desc;
  14. begin
  15.   open cs1;
  16.   loop
  17.     fetch cs1 into v_name,v_tmp_result;
  18.     exit when not found;
  19.     v_english = split_part(split_part(v_tmp_result,',',1),':',2);
  20.     v_physics = split_part(split_part(v_tmp_result,',',2),':',2);
  21.     v_math = split_part(split_part(v_tmp_result,',',3),':',2);
  22.     return query select v_name,v_english,v_physics,v_math;
  23.   end loop;
  24. end;
  25. $ytt$ language plpgsql;


  26. t_girl=# select name,English,Physics,Math from func_ytt_crosstab();
  27.  name | english | physics | math
  28. -------+---------+---------+------
  29.  Simon | 90 | 76 | 79
  30.  Lucy | 100 | 90 | 85
  31.  Lily | 95 | 81 | 84
  32.  David | 100 | 86 | 89
  33. (4 rows)


  34. Time: 2.127 ms



02-05 16:43