作者:小5聊基础
简介:一只喜欢全栈方向的程序员,欢迎咨询,尽绵薄之力答疑解惑
编程原则:Write Less Do More
【开发环境】
开发系统:Windows 10
开发语言:SQL Server
开发工具:SQL Server 2008 R2
文章作用:记录、备忘、总结、分享、理解
人生格言:勤能补拙
【主要知识点】
编号 | 知识点 | 解释 |
---|---|---|
1 | group by | 分组 |
2 | having | 筛选条件 |
3 | count() | 函数,总记录数 |
4 | min() | 函数,最小值 |
5 | max() | 函数,最大值 |
6 | union all | 记录叠加,重复不合并,但是字段必须一致 |
7 | union和union all的区别 |
【知识点应用】
1、假如查询记录如下
select * from(
select 1 id,'语文' as kecheng union all
select 2 id,'数学' as kecheng union all
select 3 id,'语文' as kecheng union all
select 4 id,'英语' as kecheng union all
select 5 id,'语文' as kecheng union all
select 6 id,'数学' as kecheng
) as table_
2、分组运用,以及函数min、max、count的运用
select min(id) as minid,max(id) as maxid,count(1) as shuliang,kecheng from (
select 1 id,'语文' as kecheng union all
select 2 id,'数学' as kecheng union all
select 3 id,'语文' as kecheng union all
select 4 id,'英语' as kecheng union all
select 5 id,'语文' as kecheng union all
select 6 id,'数学' as kecheng
) as table_ group by kecheng
3、结合having筛选的使用
select min(id) as minid,max(id) as maxid,count(1) as shuliang,kecheng from (
select 1 id,'语文' as kecheng union all
select 2 id,'数学' as kecheng union all
select 3 id,'语文' as kecheng union all
select 4 id,'英语' as kecheng union all
select 5 id,'语文' as kecheng union all
select 6 id,'数学' as kecheng
) as table_ group by kecheng
having count(kecheng)>2
因此,从上面步骤即可排查出现重复的记录
【撸码写文档,我们是认真的】,