首页 > 技术文章 > SQL——多条相似内容只取一条

fabao 2020-05-04 18:57 原文

代码

--sql多条相似内容取一条

--首先创建测试数据
if object_id('tempdb..#t') is not null drop table #t

create table #t
(
    time int,
    total int,
    name varchar(50)
)

insert into #t values (100,1000,'zhangsan')
insert into #t values (100,1001,'lisi')
insert into #t values (100,1002,'wangwu')
insert into #t values (101,1003,'zhaoliu')
insert into #t values (101,1004,'sunqi')
insert into #t values (102,1005,'hello')

select * from #t

--取一条--取的是total最大的
select * from #t as a 
where not exists(
    select 1 
    from #t 
    where a.time = time 
        and a.total <total)

 

推荐阅读