返回顶部
首页 > 资讯 > 数据库 >sql分组统计“group by”
  • 173
分享到

sql分组统计“group by”

sql分组统计“groupby” 2016-07-01 11:07:25 173人浏览 绘本
摘要

准备数据 先准备数据,数据如图所示: 按app字段分组 select fun(logs.id) as c, -- fun表示聚合函数 logs.app from public.logs as logs group by

sql分组统计“group by”

准备数据

先准备数据,数据如图所示: 数据

按app字段分组

select
  fun(logs.id) as c, -- fun表示聚合函数
  logs.app
from public.logs as logs
group by logs.app;

计数 count

select
  count(logs.id) as c,
  logs.app
from public.logs as logs
group by logs.app
order by c desc
limit 10;

求和 sum

select
  sum(logs.packets) as s,
  logs.app
from public.logs as logs
group by logs.app
order by s desc
limit 10;

求均值 avg

select
  avg(logs.packets) as s,
  logs.app
from public.logs as logs
group by logs.app
order by s desc
limit 10;

取最大值 max

select
  max(logs.packets) as s,
  logs.app
from public.logs as logs
group by logs.app
order by s desc
limit 10;

取最小值 max

select
  min(logs.packets) as s,
  logs.app
from public.logs as logs
group by logs.app
order by s desc
limit 10;

取平均值大于11710, having

select
  avg(logs.packets) as s,
  logs.app
from public.logs as logs
group by logs.app
having avg(logs.packets) > 11710
order by s desc
limit 10;

大小写 upper 和 lower

select upper(logs.d_region), lower(logs.s_region)
from public.logs as logs
limit 10;

字符串长度

select length(logs.d_region) as length_of_region
from public.logs as logs
limit 10;

取平均值大于11710,并且四舍五入 round

select
  round(avg(logs.packets), 2) as s,
  logs.app,
  now() as date
from public.logs as logs
group by logs.app
HAVING avg(logs.packets) > 11710
order by s desc
limit 10;
您可能感兴趣的文档:

--结束END--

本文标题: sql分组统计“group by”

本文链接: https://lsjlt.com/news/4723.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作