当前位置:网站首页>SQL中去去重的三种方式
SQL中去去重的三种方式
2022-07-26 16:52:00 【斯沃福德】
SQL去重是数据分析工作中比较常见的一个场景;
在 MySQL 中通常是使用 distinct 或 group by子句,但在支持窗口函数的 sql(如Hive SQL、Oracle等等) 中还可以使用 row_number 窗口函数进行去重。

需求: 统计 emp表中的员工数据中共有多少个部门 ?
1. distinct
select count(DISTINCT deptno ) from emp;
distinct 通常效率较低。它不适合用来展示去重后具体的值,一般与 count 配合用来计算条数。
注: distinct前面不能再有其他字段!
错误用法: SELECT ename , DISTINCT deptno FROM emp;
2. group by
select count(deptno) from
(select deptno from emp group by deptno)q;
3. row_number
使用ROW_NUMBER 记录每个partition内的排序,再用sum 记录排序中为1的,即为deptno的数量’
select sum( if(r =1,1,0) ) from
( select row_number() over(partition by deptno)as r from emp)q;
或者
select sum( case when r=1 then 1 else 0 end ) from
( select row_number() over(partition by deptno )as r from emp)q;
参考:https://blog.csdn.net/xienan_ds_zj/article/details/103869048
边栏推荐
- 2.1.2 同步始终失败
- Good afternoon, everyone. Please ask a question: how to start a job submitted in SQL from the savepoint? Problem Description: using SQL in Cl
- How to write plug-ins quickly with elisp
- In May, 2022, video user insight: user use time increased, and the platform achieved initial results in cost reduction and efficiency increase
- 2019普及组总结
- Reuse idea through registry
- 使用 replace-regexp 在行首添加序号
- How to connect tdengine with idea database tool?
- Machine learning - what are machine learning, supervised learning, and unsupervised learning
- 简述CUDA镜像构建
猜你喜欢

Implementing DDD based on ABP -- aggregation and aggregation root practice

In May, 2022, video user insight: user use time increased, and the platform achieved initial results in cost reduction and efficiency increase

Heavy announcement! Icml2022 Awards: 15 outstanding papers, selected by Fudan University, Xiamen University and Shanghai Jiaotong University

Relationship between standardization, normalization and regularization

Comparison between dimensional modeling and paradigm modeling

GAN (Generative Adversarial Network,GAN)生成式对抗网络

Come on developer! Not only for the 200000 bonus, try the best "building blocks" for a brainstorming

(24) the top menu of blender source code analysis shows code analysis

FIR filter design

the loss outweighs the gain! Doctors cheated 2.1 million yuan and masters cheated 30000 yuan of talent subsidies, all of which were sentenced!
随机推荐
On the growth of data technicians
图的遍历的定义以及深度优先搜索和广度优先搜索(一)
Just this time! Talk about the technical solutions of distributed system in detail
Week 4 Recurrent Neural Networks
Small application of C language using structure to simulate election
Xiaomi Wuhan headquarters building starts today! Lei Jun: planned according to the scale of 10000 people
JS 闭包 模拟私有变量 面试题 立即执行函数IIFE
uni-app
Implementing dropout with mxnet from zero sum
the loss outweighs the gain! Doctors cheated 2.1 million yuan and masters cheated 30000 yuan of talent subsidies, all of which were sentenced!
2019 popularization group summary
CCS tm4c123 new project
现在网上开户安全么?股票开户要找谁?
树形dp问题
Come on developer! Not only for the 200000 bonus, try the best "building blocks" for a brainstorming
Speaker recruitment | AI time recruit icml/ijcai 2022 as a Chinese speaker!!!
机器视觉在服务机器人中的应用
OpenWrt之feeds.conf.default详解
【机器学习】Mean Shift原理及代码
About the difference between BigDecimal conversion string toengineeringstring, toplainstring and toString