SQL COUNT(DISTINCT(field1))GROUP BY MAX(filed2)

时间:2019-04-17 16:25:27

标签: sql amazon-athena presto

我有一张类似的桌子

name  num_try

John     2
John     1
Mike     3
Mike     2
Linda    2

我想知道按MAX(num_try)来计数不同的名称组。

所需的结果应为

MAX(num_try)  COUNT(DISTINCT(names))
     2            2
     3            1

您能帮我这个疑问吗?

1 个答案:

答案 0 :(得分:3)

select max_num_try, count(*) from 
(
   select name, max(num_try) as max_num_try
   from table1
   group by name
) a
group by max_num_try
order by max_num_try desc