GBase 8a provides pivot for turning rows into columns, also called a data pivot. This post walks through the syntax and a progressive set of examples for the China-domestically developed GBase 8a MPP database from GBASE. Student names and the subject labels in the sample tables are translated into English below; all column names, numeric values and row orders stay exactly as in the original output.
Syntax
pivot(
<aggregate_function>(<column_to_aggregate>)[ as alias][,<aggregate_function>(<column_to_aggregate>)[ as alias]...]
for <column1[,column2...]>
in (constant1 [as alias1] [,constant2[ as alias2]...])
)
Notes
- The aggregate part supplies the values of the new columns, and several aggregates are allowed.
-
fornames the column to rotate. Eligible column values are listed inin. With a single column the parentheses can be omitted (for column_name in ...); with several columns the parentheses are required, and the values of those columns together form one constant inin. -
inturns each constant into a new column. Character or date constants need single quotes. The new column takes the constant value or its alias; when the aggregate has an alias, an underscore and that alias are appended, for exampleChinese_sum. For multi-column constants, aliases are strongly recommended because the default column names are awkward to use. - Single column:
for column in (constant1, constant2 ...). - Several columns:
for (column1,column2) in ( (col1_value,col2_value) name1,(col1_value,col2_value) name2,(col1_value,col2_value). name3..)
- Every column not used by
foror by an aggregate is grouped by default. A nested query limits the columns involved:
select * from ( select XX from tp) tmp pivot (...)
Tested version
gbase> select version();
+---------------------------+
| version() |
+---------------------------+
| 9.5.3.28.4_patch.1bcc63ff |
+---------------------------+
1 row in set (Elapsed: 00:00:00.00)
Sample table
gbase> desc tp;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(10) | YES | | NULL | |
| class | varchar(10) | YES | | NULL | |
| score | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (Elapsed: 00:00:00.01)
Sample data
Each student has several exam scores per subject.
gbase> select * from tp order by class,name,score;
+-----------+---------+-------+
| name | class | score |
+-----------+---------+-------+
| Zhang San | Math | 99 |
| Zhang San | Math | 129 |
| Li Si | Math | 88 |
| Li Si | Math | 128 |
| Wang Wu | Math | 111 |
| Wang Wu | Math | 161 |
| Zhang San | Chinese | 120 |
| Zhang San | Chinese | 120 |
| Li Si | Chinese | 140 |
| Li Si | Chinese | 150 |
| Wang Wu | Chinese | 100 |
| Wang Wu | Chinese | 120 |
+-----------+---------+-------+
12 rows in set (Elapsed: 00:00:00.02)
A single aggregate
Total score per subject for each student:
gbase> select * from tp pivot(sum(score) for class in ('Chinese','Math'));
+-----------+---------+------+
| name | Chinese | Math |
+-----------+---------+------+
| Li Si | 290 | 216 |
| Zhang San | 240 | 228 |
| Wang Wu | 220 | 272 |
+-----------+---------+------+
3 rows in set (Elapsed: 00:00:00.06)
Several aggregates
Average and total score per subject. Give each aggregate an alias to avoid duplicate column names; an underscore is added automatically. One aggregate may keep no alias, in which case its column name is simply the constant value.
gbase> select * from tp pivot(sum(score)sum, avg(score)avg for class in ('Chinese','Math'));
+-----------+-------------+----------+-------------+----------+
| name | Chinese_sum | Math_sum | Chinese_avg | Math_avg |
+-----------+-------------+----------+-------------+----------+
| Li Si | 290 | 216 | 145.0000 | 108.0000 |
| Zhang San | 240 | 228 | 120.0000 | 114.0000 |
| Wang Wu | 220 | 272 | 110.0000 | 136.0000 |
+-----------+-------------+----------+-------------+----------+
3 rows in set (Elapsed: 00:00:00.06)
gbase> select * from tp pivot(sum(score), avg(score)avg for class in ('Chinese','Math'));
+-----------+---------+------+-------------+----------+
| name | Chinese | Math | Chinese_avg | Math_avg |
+-----------+---------+------+-------------+----------+
| Zhang San | 240 | 228 | 120.0000 | 114.0000 |
| Wang Wu | 220 | 272 | 110.0000 | 136.0000 |
| Li Si | 290 | 216 | 145.0000 | 108.0000 |
+-----------+---------+------+-------------+----------+
3 rows in set (Elapsed: 00:00:00.06)
Aliasing the constants
Give the subject names their own column names:
gbase> select * from tp pivot(sum(score)sum, avg(score)avg for class in ('Chinese' yw,'Math' sx));
+-----------+--------+--------+----------+----------+
| name | yw_sum | sx_sum | yw_avg | sx_avg |
+-----------+--------+--------+----------+----------+
| Li Si | 290 | 216 | 145.0000 | 108.0000 |
| Zhang San | 240 | 228 | 120.0000 | 114.0000 |
| Wang Wu | 220 | 272 | 110.0000 | 136.0000 |
+-----------+--------+--------+----------+----------+
3 rows in set (Elapsed: 00:00:00.06)
Multiple FOR columns
A second table tp2 adds an age column:
gbase> desc tp2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(10) | YES | | NULL | |
| class | varchar(10) | YES | | NULL | |
| score | int(11) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (Elapsed: 00:00:00.00)
gbase> select * from tp2;
+-----------+---------+-------+-----+
| name | class | score | age |
+-----------+---------+-------+-----+
| Zhang San | Chinese | 120 | 40 |
| Li Si | Chinese | 140 | 45 |
| Wang Wu | Chinese | 100 | 50 |
| Zhang San | Math | 99 | 40 |
| Li Si | Math | 88 | 45 |
| Wang Wu | Math | 111 | 50 |
| Zhang San | Chinese | 120 | 40 |
| Li Si | Chinese | 150 | 45 |
| Wang Wu | Chinese | 120 | 50 |
| Zhang San | Math | 129 | 40 |
| Li Si | Math | 128 | 45 |
| Wang Wu | Math | 161 | 50 |
+-----------+---------+-------+-----+
12 rows in set (Elapsed: 00:00:00.01)
gbase> select * from tp2 order by class,name,score;
+-----------+---------+-------+-----+
| name | class | score | age |
+-----------+---------+-------+-----+
| Zhang San | Math | 99 | 40 |
| Zhang San | Math | 129 | 40 |
| Li Si | Math | 88 | 45 |
| Li Si | Math | 128 | 45 |
| Wang Wu | Math | 111 | 50 |
| Wang Wu | Math | 161 | 50 |
| Zhang San | Chinese | 120 | 40 |
| Zhang San | Chinese | 120 | 40 |
| Li Si | Chinese | 140 | 45 |
| Li Si | Chinese | 150 | 45 |
| Wang Wu | Chinese | 100 | 50 |
| Wang Wu | Chinese | 120 | 50 |
+-----------+---------+-------+-----+
12 rows in set (Elapsed: 00:00:00.03)
Constants made of several columns work the same way. Naming them is what keeps the result usable:
gbase> select * from tp2 pivot(sum(score)sum for (name,age) in (('Zhang San',40)zs40,('Li Si',45)ls45));
+---------+----------+----------+
| class | zs40_sum | ls45_sum |
+---------+----------+----------+
| Chinese | 240 | 290 |
| Math | 228 | 216 |
+---------+----------+----------+
2 rows in set (Elapsed: 00:00:00.06)
Without aliases, the default column names are messy and hard to reference:
gbase> select * from tp2 pivot(sum(score) for (name,age) in (('Zhang San',40),('Li Si',45)));
+---------+------------------+-------------+
| class | (('Zhang San',40 | ('Li Si',45 |
+---------+------------------+-------------+
| Chinese | 240 | 290 |
| Math | 228 | 216 |
+---------+------------------+-------------+
2 rows in set (Elapsed: 00:00:00.07)
Going forward, pin the constant list and the aggregate aliases explicitly in gbase SQL: the constant list decides the output columns, and any column left out of for and the aggregates is grouped implicitly. A nested query on a gbase database keeps that grouping set small and the result predictable.
Top comments (0)