【问题标题】:How do I use group by for only selected number of rows?如何仅对选定的行数使用 group by?
【发布时间】:2021-09-09 18:40:18
【问题描述】:

我是学习 sql 的新手,非常感谢您的帮助。

ID Alphabet
1 a
2 b
3 b
4 c
5 e
6 d
7 d
8 f
9 a
10 c

我想知道每个字母表只出现在前五行的次数。 在上述情况下,我想得到 a: 1, b: 2 c: 1, e: 1

我试过select Alphabet, count(Alphabet) from TABLENAME group by Alphabet limit 5 但这给出了五组 Alphabet 及其在所有行中的计数。如何修复此代码?再次感谢。

【问题讨论】:

    标签: android sql sqlite


    【解决方案1】:

    您将使用子查询:

    select alphabet, count(*)
    from (select t.*
          from tablename t
          order by id
          limit 5
         ) t
    group by alphabet;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-11
      • 2015-10-12
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 2015-04-15
      相关资源
      最近更新 更多