各种查资料,都没找到,建议还是多看文档
方法1 :简单便捷: order by date,count desc limit 5 by date (取每天前5条数据,5可以随便换)
sql可以随便写:
统计每天学生人数,按降序排序,取每天的前5条数据 |
select date,username,count(*) as cc from 表 where date between '' and '' group by date,username order by date,cc desc limit 5 by date (红字部分很重要) |
效果如下:
方法2:使用groupArray(5)(name) as name , arrayJoin(name)
方法2:弊端:如果想取3列的就不可取了,感兴趣的可以去试试
SELECT date,arrayJoin(name) from
(
select date,groupArray(5)(Name) as name from
(
select toDate(EventTime) date,Name from hits_tt10 ht
order by date asc
) group by date
)
结果如下:
通过以下sql取出了每天前5条数据:弊端:如果是取3列的情况下:该方法不可取
有问题欢迎交流
补充: limit 5 by date/username/ 想根据哪个字段分组就by 某个字段即可,有问题放讨论区