i'm trying to: 1) Select the min value between col_1, col_2, and col_3 2) Calculate the sum of the minumum values based on the id_col value.
Using the example table below, the result I'm expecting is:
+--------+--------------+
| id_col | sum |
+--------+--------------+
| 123 | 523.99996667 |
+--------+--------------+
example_table
+--------+--------------+----------+---------+------+
| id_col | col_1 | col_2 | col_3 | id |
+--------+--------------+----------+---------+------+
| 123 | 175.00000000 | 150.0000 | NULL | 999 |
| 123 | 175.00000000 | 150.0000 | NULL | 999 |
| 123 | 175.00000000 | 150.0000 | NULL | 999 |
| 123 | 41.66666667 | 50.0000 | NULL | 4444 |
| 123 | 50.00000000 | 100.0000 | 32.3333 | 5555 |
+--------+--------------+----------+---------+------+
I've tried the below to select the min value between the 3 columns, but it's only selecting the min value within the entire table instead.
select id_col,
SUM(CASE WHEN col_1 < col_2 AND col_1 < col_3 THEN col_1
WHEN col_2 < col_1 AND col_2 < col_3 THEN col_2
ELSE col_3 END) sum
from example_table
group by 1```