Используйте функции analyti c, см. Комментарии в коде:
with my_table as (
select stack(6,
'2018-01-15 17:56','A', '3' ,
'2018-01-15 17:56','A', '2' ,
'2018-10-23 23:43','B', 'True' ,
'2018-10-23 23:43','B', 'False',
'2018-10-23 23:43','A', '3' ,
'2018-10-23 23:43','B', 'True' ) as (col_1 , col_2, col_3)
)
select col_1, --final aggregation by col_1
max(avg) as A,
max(most_frequent) as B
from(
select col_1, col_2, col_3, cnt, --calculate avg and most_frequent
case when col_2='A' then avg(col_3) over(partition by col_1, col_2) else null end as avg,
case when col_2='B' then first_value(col_3) over(partition by col_1, col_2 order by cnt desc) else null end as most_frequent
from
(
select col_1, col_2, col_3, --calculate count
case when col_2='B' then count(*) over(partition by col_1, col_2, col_3) else null end as cnt
from my_table
)s
)s
group by col_1
;
Результат:
col_1 a b
2018-01-15 17:56 2.5 NULL
2018-10-23 23:43 3.0 True