【⼩5聊】SqlServer基础之分组查询重复出现多条记录的SQL
语句,以及groupby。。。
【开发环境】
开发系统:Windows 10
开发语⾔:SQL Server
开发⼯具:SQL Server 2008 R2
⽂章作⽤:记录、备忘、总结、分享、理解
相互学习:号-xgwkf566
⼈⽣格⾔:勤能补拙
【主要知识点】
group by 分组
having 筛选条件
count()函数,总记录数
min()函数,最⼩值
max()函数,最⼤值
union all,记录叠加,重复不合并,但是字段必须⼀致
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_
select min(id) as minid,max(id) as maxid,count(1) as shuliang,kecheng from (
groupby分组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
因此,从上⾯步骤即可排查出现重复的记录
【撸码写⽂档,我们是认真的】
【⼀句话⼀感想⼀⼼情】版本更新,增加测颜值推荐图⽂,来体验下句⼦的魅⼒,秀出你棒棒的⽂笔
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论