DEV Community

Michael
Michael

Posted on

Using unpivot to Turn Columns into Rows in GBase 8a

unpivot is the inverse of pivot: it turns columns into rows. This post walks through the syntax, the null-handling options and a set of runnable examples for the China-domestically developed GBase 8a MPP database from GBASE. The source note marks itself as still being refined, so one parameter description is deliberately left short.

Reference

The companion post on pivot, which turns rows into columns in GBase 8a.

Syntax

unpivot 
  [{include_nulls | exclude_nulls}]
  (
    COLUMN | (
      COLUMN[,COLUMN]..
    )
    FOR COLUMN | (
      COLUMN[,COLUMN]..
    )
    IN COLUMN | (
      COLUMN[,COLUMN]...
    ) [ AS LITERAL | (
        LITERAL[,LITERAL]...
      )
    ]
  )
Enter fullscreen mode Exit fullscreen mode

Notes

NULLS

  • INCLUDE_NULLS: when the column value to be converted is null, do not filter it — the row is still shown.
  • EXCLUDE_NULLS: when the column value is null, filter it out (is not null) and the row is not shown. This is the default.

unpivot_COLUMN

The name of the newly added column that stores the value; the source note stops here and leaves this section unfinished.

Examples

The sample table tpp has four score columns per student. Student names and the subject labels written with AS LITERAL are translated into English below, while all column names, numeric values and row orders stay exactly as in the original output:

gbase> select * from tpp;
+-----------+--------+--------+----------+----------+
| 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.01)
Enter fullscreen mode Exit fullscreen mode

Unpivoting a single column adds a class column holding the literal and a score column holding the value, while the remaining columns stay untouched:

gbase> select * from tpp unpivot(score for class in (yw_sum as 'Chinese Total'));
+----------+--------+----------+-----------+---------------+-------+
| yw_avg   | sx_sum | sx_avg   | name      | class         | score |
+----------+--------+----------+-----------+---------------+-------+
| 120.0000 |    228 | 114.0000 | Zhang San | Chinese Total |   240 |
| 110.0000 |    272 | 136.0000 | Wang Wu   | Chinese Total |   220 |
| 145.0000 |    216 | 108.0000 | Li Si     | Chinese Total |   290 |
+----------+--------+----------+-----------+---------------+-------+
3 rows in set (Elapsed: 00:00:00.01)
Enter fullscreen mode Exit fullscreen mode

Adding a second column to IN produces one row per student and literal pair:

gbase> select * from tpp unpivot(score for class in (yw_sum as 'Chinese Total',sx_sum as 'Math Total'));
+----------+----------+-----------+---------------+-------+
| yw_avg   | sx_avg   | name      | class         | score |
+----------+----------+-----------+---------------+-------+
| 145.0000 | 108.0000 | Li Si     | Chinese Total |   290 |
| 145.0000 | 108.0000 | Li Si     | Math Total    |   216 |
| 120.0000 | 114.0000 | Zhang San | Chinese Total |   240 |
| 110.0000 | 136.0000 | Wang Wu   | Chinese Total |   220 |
| 120.0000 | 114.0000 | Zhang San | Math Total    |   228 |
| 110.0000 | 136.0000 | Wang Wu   | Math Total    |   272 |
+----------+----------+-----------+---------------+-------+
6 rows in set (Elapsed: 00:00:00.01)
Enter fullscreen mode Exit fullscreen mode

With three columns in IN, only sx_avg is left as a passthrough column:

gbase> select * from tpp unpivot(score for class in (yw_sum as 'Chinese Total',sx_sum as 'Math Total',yw_avg as 'Chinese Average'));
+----------+-----------+-----------------+----------+
| sx_avg   | name      | class           | score    |
+----------+-----------+-----------------+----------+
| 114.0000 | Zhang San | Chinese Total   | 240.0000 |
| 136.0000 | Wang Wu   | Chinese Total   | 220.0000 |
| 114.0000 | Zhang San | Math Total      | 228.0000 |
| 136.0000 | Wang Wu   | Math Total      | 272.0000 |
| 114.0000 | Zhang San | Chinese Average | 120.0000 |
| 136.0000 | Wang Wu   | Chinese Average | 110.0000 |
| 108.0000 | Li Si     | Chinese Total   | 290.0000 |
| 108.0000 | Li Si     | Math Total      | 216.0000 |
| 108.0000 | Li Si     | Chinese Average | 145.0000 |
+----------+-----------+-----------------+----------+
9 rows in set (Elapsed: 00:00:00.01)
Enter fullscreen mode Exit fullscreen mode

Unpivoting all four columns leaves just name, class and score:

gbase> select * from tpp unpivot(score for class in (yw_sum as 'Chinese Total',sx_sum as 'Math Total',yw_avg as 'Chinese Average',sx_avg as 'Math Average'));
+-----------+-----------------+----------+
| name      | class           | score    |
+-----------+-----------------+----------+
| Zhang San | Chinese Total   | 240.0000 |
| Wang Wu   | Chinese Total   | 220.0000 |
| Zhang San | Math Total      | 228.0000 |
| Wang Wu   | Math Total      | 272.0000 |
| Zhang San | Chinese Average | 120.0000 |
| Wang Wu   | Chinese Average | 110.0000 |
| Zhang San | Math Average    | 114.0000 |
| Wang Wu   | Math Average    | 136.0000 |
| Li Si     | Chinese Total   | 290.0000 |
| Li Si     | Math Total      | 216.0000 |
| Li Si     | Chinese Average | 145.0000 |
| Li Si     | Math Average    | 108.0000 |
+-----------+-----------------+----------+
12 rows in set (Elapsed: 00:00:00.01)

gbase>
Enter fullscreen mode Exit fullscreen mode

The row count grows with the number of columns listed in IN — one column gives 3 rows, two give 6, three give 9, and all four give 12 for these 3 students.

Going forward, treat unpivot as a reshape step before the result reaches a reporting query: on a gbase cluster the column list in IN decides how many rows come back, so pin it explicitly and choose include_nulls or exclude_nulls on purpose. That keeps the row count of a gbase database result predictable instead of letting the default null filter quietly drop records.

Top comments (0)