PostgreSQL is an example of five ways to implement a crosstab (row and column conversion)

  • 2020-05-06 11:56:30
  • OfStack

cross sheet

A crosstab (Cross Tabulations) is a commonly used subtotal table. Use a cross-table query to display the summary values from a field in the table and group them, one on the left side of the table and the other on the top. The intersection of rows and columns can be used for a variety of summary calculations, such as: sum, average, number, maximum, minimum, and so on. The use of cross - table query data is very intuitive, is widely used. Cross-table queries are also a feature of databases.

For example:

Table 1. Group name,
(select table 1. Names of members from table 2. b where table 1 (select table 1. Names of members from table 2. b where table 1 (select table 1. Member names from table 2 b where table 1. Member 3id= table 2. Member id) as table 3id
from table 1, table 2
This is the cross-table query

The cross report is a common type in the report, which belongs to the basic report and is a report with groups in the direction of rows and columns. Another concept involved here is grouping reports. This is the most common and common report type of all reports, and a report format that all reporting tools support. In general terms, a grouping report is just a vertical grouping. The traditional way of making group report is to divide the report into strips. Users specify groups and summarize fields according to a data binding wizard to generate standard group report.

So I'm going to show you how to do this in POSTGRESQL, but I'm not going to say any more, so let's look at the details.

The original table data is as follows:


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 

You want to achieve the following result:


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

can be used in the following ways:

1. Show
with standard 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,
is implemented with the functions provided by PostgreSQL with the third-party extension tablefunc

SQL in the following function crosstab must have three fields, name, category and category value as the starting parameter, and name and category value as the output parameter.


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,
is realized by PostgreSQL's own aggregation function


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, storage function implementation


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,

with 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 

summary


Related articles: