如何按第一个字母分组

时间:2017-12-20 14:26:08

标签: mysql sql

我在MySQL中有以下查询:

select val,count(val)
from ....
where ...
group by val

它给出了:

val   count
CE3    4
CE5    1
A3     12
BRICK4  5
BRICK2  2

我想只显示每首字母数最多的行。 这意味着以val开头的所有A都是一个群组,所有以val开头的B都是另一个群组等...

预期结果是:

val   count
CE3    4     /  CE3  CE5  are in group C , CE3 has higher count
A3     12    /  A3 is the only one in group A
BRICK4  5    / BRICK4  BRICK2  are in group B, BRICK4   has higher count

我该怎么做?

修改 我想要做的是在查询中创建一个临时列,代表组如下:

val   count  group
CE3    4      C
CE5    1      C
A3     12     A
BRICK4  5     B
BRICK2  2     B

然后搜索每组计数值最高的行。 但我不确定这是最好的方法

3 个答案:

答案 0 :(得分:6)

尝试这样的事情:

select
    val
    ,MAX(count) as count
    ,left(val,1) as first_letter
from (
    select
        val
        ,count(val) as count
    from tbl
    group by val
) a
group by left(val, 1);

首先从count获取val,然后从此结果中获得MAX次分组的第一个字母

更新 :(感谢 Vamsi Prabhala 指出我的第一个解决方案不是最好的解决方案)

获得countval后,我使用一个变量重做ROW_NUMBER()功能(来自MS-SQL)并从结果中选择第一行,按{{1}排序}和first_letter desc

count

答案 1 :(得分:3)

这可以通过变量来完成,以根据计数对行进行排名。

select val,val_cnt 
from (
select val,val_cnt,@rn:=case when @prev=left(val,1) then @rn+1 else 1 end as rnum,
@prev:=left(val,1)
from (select val,count(val) as val_cnt
      from ....
      where ...
      group by val
     ) t
cross join (select @rn:=0,@prev:='') r
order by left(val,1),val_cnt desc,val --added val to order by to break ties 
) t 
where rnum=1

答案 2 :(得分:1)

不确定这是你想要的:

让我们从每个起始字母得到最大值:

 SELECT LEFT(val, 1) as l_val, MAX(count_val)
 FROM (
   select val,count(val) as count_val
   from ....
   where ...
   group by val
 ) t
 GROUP BY l_val

然后你想要出现val:

SELECT t2.val, t1.max_val
FROM (
 SELECT LEFT(val, 1) as l_val, MAX(count_val) as max_val
 FROM (
   select val,count(val) as count_val
   from ....
   where ...
   group by val
 ) t
 GROUP BY l_val) t1
 INNER JOIN `YOUR_table` t2 ON LEFT(t2.val,1) = t1.l_val