求和PRESTO中数组联接后的唯一值



下面有3列

col1 col2 价格
abc 1234 10
abc 1234 15
bcd 45689 15
abc 78945 20
bcd 54782 13
def 1234 10
def 1234 10

我没有访问presto进行测试的权限,所以我在mySQL中进行了测试并进行了转换
我使用了一个子查询来获得使用avg()的每个col1-col2对的平均价格。如果愿意或在presto中未识别出avg(),则可以使用min()max()

select
col1,
array_join(array_agg(col2), ','),
sum(av_price)
from 
(  select 
col1,col2, avg(price) av_price
from p
group by col1,col2
) as subQuery
group by col1
order by col1;

以下是mySQL的版本、结果和dbFiddle链接

select
col1,
group_concat(col2),
sum(av_price)
from 
(  select 
col1,col2, avg(price) av_price
from p
group by col1,col2
) as subQuery
group by col1
order by col1
col1|group_concat(col2(|sum(av_price(:---|:------------------|----------------abc | 1234578945 | 32.5000bcd |4568954782 |28.000def | 12345 | 10.0000

db<gt;小提琴这里

最新更新