2

with a table like below, need to compute the mean and median for val1 to val4 for each row as val_mean and val_median respectively.

+----+------+------+------+------+------+
| id | loc  | val1 | val2 | val3 | val4 |
+----+------+------+------+------+------+
|  1 | loc1 |   10 | 190  | null |   20 |
|  2 | loc2 |   20 | null | 10   |   10 |
+----+------+------+------+------+------+

In general how do we go about handling aggregation across columns ?

one approach could be to split val1 to val4 into 4 different rows then aggregate them by id, loc to find the mean/median. So the above table would need to be transformed into below format first - how might I go abt this ?

+----+------+--------+
| id | loc  |  val   |
+----+------+--------+
|  1 | loc1 |   10   |
|  1 | loc1 |   190  |
|  1 | loc1 |   null |
|  1 | loc1 |   20   |
|  2 | loc2 |   20   |
|  2 | loc2 |   null |
|  2 | loc2 |   10   |
|  2 | loc2 |   10   |
+----+------+--------+
user3206440
  • 3,707
  • 9
  • 48
  • 93

1 Answers1

1
select id, loc, avg(val), percentile_disc(0.5) within group (order by val)
from (
  select id, loc, val1 as val
  from t
  union all
  select id, loc, val2
  from t
  union all
  select id, loc, val3
  from t
  union all
  select id, loc, val4
  from t
) s
group by id, loc
;
 id | loc  |         avg         | percentile_disc 
----+------+---------------------+-----------------
  1 | loc1 | 73.3333333333333333 |              20
  2 | loc2 | 13.3333333333333333 |              10
Clodoaldo Neto
  • 98,807
  • 21
  • 191
  • 235