博客
关于我
SQL必知必会 第10课 分组数据
阅读量:179 次
发布时间:2019-02-28

本文共 694 字,大约阅读时间需要 2 分钟。

数据分组与过滤

在数据分析中,合理的分组和过滤是提升效率的关键步骤。以下是关于SQL分组的详细指导。

10.2 创建分组

GROUP BY子句用于将数据按指定字段分组。以下示例展示了如何按vend_id分组并计算每个供应商的产品数量:

select vend_id, count(*) as num_prodsfrom productsgroup by vend_id;

10.3 过滤分组

对于分组后的数据,HAVING子句用于进行组级过滤。要过滤出订单数超过2的客户,可以使用以下查询:

select cust_id, count(*) as ordersfrom ordersgroup by cust_idhaving count(*) >= 2;

10.4 分组与排序

GROUP BY和ORDER BY的主要区别在于作用时间和排序方式。

特性 GROUP BY ORDER BY
作用 分组数据 排序输出
可用字段 只能使用选择列或表达式 可以使用任何字段
是否需要 不需要 可选

在使用GROUP BY时,建议配合ORDER BY子句使用,以确保输出的数据按指定顺序排列。

10.5 SELECT子句顺序

SELECT子句中的子句顺序不影响结果,但理解每个子句的作用对写出高效查询至关重要。

子句 说明 是否必须使用
SELECT 返回的列或表达式
FROM 数据源表 仅在选择数据时使用
WHERE 行级过滤
GROUP BY 分组说明 仅在需要聚集时使用
HAVING 组级过滤
ORDER BY 排序 可选

通过合理配置这些子句,可以高效地组织和分析数据。

转载地址:http://jgpn.baihongyu.com/

你可能感兴趣的文章
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node-RED中使用JSON数据建立web网站
查看>>
Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
查看>>
Node-RED中使用Notification元件显示警告讯息框(温度过高提示)
查看>>