滤重后,加上第一次出现的时间,帮忙!拜托!

jason_sun22 2012-01-16 10:32:14
name class time
------------------------------------
jason 1 2012-01-15
jason 1 2012-01-16
mike 2 2012-01-16

需要对name+class滤重,可用group by name,class
但是还要加上时间就不会了,请大家帮忙~~

要求结果:name+class,第一次出现的时间
即如下:

name class time
------------------------------------
jason 1 2012-01-15
mike 2 2012-01-16
...全文
93 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jason_sun22 2012-01-16
  • 打赏
  • 举报
回复
嗯,不用 group by 效率高,谢谢~~
苦苦的潜行者 2012-01-16
  • 打赏
  • 举报
回复
--query 1
select name,class,time=MIN(time) from t1
group by name,class
go
--query 2
select * from t1 as a
where not exists(select 1 from t1
where a.name=name and a.class=class and a.time>time)


效率对比:
jason_sun22 2012-01-16
  • 打赏
  • 举报
回复
xiaolinyouni朋友的方法也不错,谢谢~~
苦苦的潜行者 2012-01-16
  • 打赏
  • 举报
回复
不用group by
select * from t1 as a
where not exists(select 1 from t1
where a.name=name and a.class=class and a.time>time)
/*
name class time
--- ---- ----
jason 1 2012-01-15 00:00:00.000
mike 2 2012-01-16 00:00:00.000
*/
jmx123456789 2012-01-16
  • 打赏
  • 举报
回复

if OBJECT_ID('tb') is not null
drop table tb
go
create table tb (name varchar(50),class int ,[time] varchar(50))
insert into tb select 'jason', 1, '2012-01-15' union all
select 'jason' ,1, '2012-01-16' union all
select 'mike' ,1, '2012-01-16'


select name,class,MIN(time) from tb group by name,class

------------------------------
jason 1 2012-01-15
mike 1 2012-01-16
jason_sun22 2012-01-16
  • 打赏
  • 举报
回复
原来可以这样用,谢谢各位,已结帖~~
勿勿 2012-01-16
  • 打赏
  • 举报
回复
--name class time
--------------------------------------
--jason 1 2012-01-15
--jason 1 2012-01-16
--mike 2 2012-01-16

--需要对name+class滤重,可用group by name,class
--但是还要加上时间就不会了,请大家帮忙~~

--要求结果:name+class,第一次出现的时间
--即如下:

--name class time
--------------------------------------
--jason 1 2012-01-15
--mike 2 2012-01-16

if OBJECT_ID('tb') is not null
drop table tb
go
create table tb (name varchar(50),class int ,[time] varchar(50))
insert into tb select 'jason', 1, '2012-01-15' union all
select 'jason' ,1, '2012-01-16' union all
select 'mike' ,1, '2012-01-16'
select name,class,min(time) as time
from tb
group by name,class


name class time
-------------------------------------------------- ----------- --------------------------------------------------
jason 1 2012-01-15
mike 1 2012-01-16

(2 行受影响)


AcHerat 元老 2012-01-16
  • 打赏
  • 举报
回复

select name,class,min([time]) as minTime
from tb
group by name,class
勿勿 2012-01-16
  • 打赏
  • 举报
回复
--name class time
--------------------------------------
--jason 1 2012-01-15
--jason 1 2012-01-16
--mike 2 2012-01-16

--需要对name+class滤重,可用group by name,class
--但是还要加上时间就不会了,请大家帮忙~~

--要求结果:name+class,第一次出现的时间
--即如下:

--name class time
--------------------------------------
--jason 1 2012-01-15
--mike 2 2012-01-16

if OBJECT_ID('tb') is not null
drop table tb
go
create table tb (name varchar(50),class int ,[time] varchar(50))
insert into tb select 'jason', 1, '2012-01-15' union all
select 'jason' ,1, '2012-01-16' union all
select 'mike' ,1, '2012-01-16'

select * from tb t where [time]=(select min(time) from tb where name=t.name)


name class time
-------------------------------------------------- ----------- --------------------------------------------------
jason 1 2012-01-15
mike 1 2012-01-16

(2 行受影响)


百年树人 2012-01-16
  • 打赏
  • 举报
回复
select name,class,min(time) as time
from tb
group by name,class

--or

select *
from tb t
where not exists(select 1 from tb where name=t.name and time<t.time)
快溜 2012-01-16
  • 打赏
  • 举报
回复
select name,class,min([time]) from tb group by name,class

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧