8.3 Bidirectional transposition
A bidirectional transposition performs both row-to-column transposition and column-to-row transposition simultaneously.
Below are sales records grouped by channel:
Day | Online | Store |
---|---|---|
20190101 | 2400 | 1863 |
20190102 | 1814 | 670 |
20190103 | 3730 | 1444 |
Desired transposition result:
Category | 20190101 | 20190102 | 20190103 |
---|---|---|---|
Online | 2400 | 1814 | 3730 |
Store | 1863 | 670 | 1444 |
First, we transpose columns to rows by transforming Online and Store to values of the new Category field.
Day | Category | Sales |
---|---|---|
20190101 | Online | 2400 |
20190101 | Store | 1863 |
20190102 | Online | 1814 |
20190102 | Store | 670 |
20190103 | Online | 3730 |
20190103 | Store | 1444 |
Then we transpose rows to columns by transforming distinct values under Day field to new column names.
Category | 20190101 | 20190102 | 20190103 |
---|---|---|---|
Online | 2400 | 1814 | 3730 |
Store | 1863 | 670 | 1444 |
SPL script:
A | |
---|---|
1 | =connect(“db”) |
2 | =A1.query@x(“select * from Sales”) |
3 | =A2.pivot@r(Day; Category, Sales) |
4 | =A3.pivot(Category; Day, Sales) |
A3 Use pivot@r function to perform column-to-row transposition that uses channel types Online and Store as values of new field Category.
A4 Use pivot() function to perform row-to-column transposition that converts distinct values of Day field to new field names.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL