SQL, get median of all columns in each row
In PostgreSQL database, table T has multiple numeric columns:
X | Y | Z | |
1 | 6 | 3 | 3 |
2 | 5 | 6 | NULL |
3 | 4 | 5 | 6 |
4 | 11 | 7 | 8 |
We want to find the median of all column values in each row. Below is the expected result:
MEDIAN | |
1 | 3 |
2 | 5 |
3 | 5 |
4 | 8 |
Write the following SPL code:
A | |
1 | =pg1.query("select * from T") |
2 | =A1.new(~.array().median()) |
new()function creates a new two-dimensional table; ~ represents the current record; array() function converts field values of a record to a sequence.
SPL Official Website 👉 https://www.esproc.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.esproc.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/sxd59A8F2W
Youtube 👉 https://www.youtube.com/@esProc_SPL