I'm attempting to get the first non-null value in a set of many columns. I'm aware that I could accomplish this using a sub-query per column. In the name of performance, which really does count in this scenario, I'd like to do this in a single pass.
Take the following example data:
col1 col2 col3 sortCol
====================================
NULL 4 8 1
1 NULL 0 2
5 7 NULL 3
My dream query would find the first non-null value in each of the data columns, sorted on the sortCol
.
For example, when selecting the magical aggregate of the first three columns, sorted by the sortCol
descending.
col1 col2 col3
========================
5 7 0
Or when sorting ascending:
col1 col2 col3
========================
1 4 8
Does anyone know a solution?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…