Starting Point:
I have a large table with many columns, of which the first is an identifier (ID) and the second a reversed rank over this ID (Rank). Each ID can have an arbitrary number of rows displaying either a NULL or a float value in each column with up to 50+ columns. There is no limiting range for the float values. Below is a small example table already grouped by the ID column.
ID | Rank | Column A | Column B | Column C | Column D | Column E |
---|---|---|---|---|---|---|
1 | 1 | NULL | 0.7 | 2.7 | NULL | 0.6 |
1 | 2 | 0.9 | 1.6 | 0.6 | NULL | NULL |
1 | 3 | NULL | 0.4 | 2.3 | NULL | 0.3 |
1 | 4 | 0.6 | NULL | 1.4 | NULL | NULL |
Goal:
I want to aggregate this table grouped by the ID, taking the non-NULL values with the highest rank. If only NULL values are found, a NULL value should be used. So the output for the table above should look like this:
ID | Rank | Column A | Column B | Column C | Column D | Column E |
---|---|---|---|---|---|---|
1 | 4 | 0.6 | 0.4 | 1.4 | NULL | 0.3 |
Standard aggregation functions such as MAX or MIN did not work for me because, exemplarily, in column B they would take 2.7 or 0.6, respectively, and not the correct value (1.4).
My current solution is outside of SQL and iterates for each ID over all columns returning the numeric value with the highest rank or NULL if there is no numeric value for a certain ID.
Any ideas on how to solve this in SQL?