--> 测试数据:@表tt declare @表tt table([at] int,[rt] datetime) insert @表tt select 203,'20050403' union all select 203,'20060408' union all select 204,'20080506' union all select 204,'20090504'select at,MAX(rt) from @表tt group by at
select at,max(rt) from tt group by at..
select at, MAX(rt) as rt from 表tt group by at
if exists(select * from sysobjects where [name]='tb') drop table tb go create table tb([at] int,[rt] datetime) go insert into tb([at],[rt]) ( select 203,'20050403' union select 203,'20060408' union select 204,'20080506' union select 204,'20090504' ) go select [at],max([rt]) from tb group by [at] go查询结果 at ----------- ----------------------- 203 2006-04-08 00:00:00.000 204 2009-05-04 00:00:00.000
at rt ST WT 203 20050403 7 45 203 20060408 8 11 204 20080506 9 55 204 20090504 90 22我要取不同的at 和rt的时间最大的那个 怎么写sql 谢谢!增加了几列后 group by 会提示
select * from tt t where not exists(select 1 from tt where at = t.at and rt > t.rt)
Select at,CONVERT(DateTime,MAX(rt), 120) From 表tt Where IsDate(rt)=1 Group By at
declare @表tt table([at] int,[rt] datetime)
insert @表tt
select 203,'20050403' union all
select 203,'20060408' union all
select 204,'20080506' union all
select 204,'20090504'select at,MAX(rt) from @表tt group by at
at,
MAX(rt) as rt
from
表tt
group by
at
if exists(select * from sysobjects where [name]='tb')
drop table tb
go
create table tb([at] int,[rt] datetime)
go
insert into tb([at],[rt])
(
select 203,'20050403' union
select 203,'20060408' union
select 204,'20080506' union
select 204,'20090504'
)
go
select [at],max([rt]) from tb group by [at]
go查询结果
at
----------- -----------------------
203 2006-04-08 00:00:00.000
204 2009-05-04 00:00:00.000
203 20050403 7 45
203 20060408 8 11
204 20080506 9 55
204 20090504 90 22我要取不同的at 和rt的时间最大的那个 怎么写sql 谢谢!增加了几列后 group by 会提示
from tt t
where not exists(select 1 from tt where at = t.at and rt > t.rt)
Select at,CONVERT(DateTime,MAX(rt), 120) From 表tt Where IsDate(rt)=1 Group By at