34,590
社区成员
发帖
与我相关
我的任务
分享
declare @table1 table
(
id int identity(1,1) not null,
total float null,
time datetime null
)
insert into @table1
select 11,'2011-1-3 12:10:01' union all
select 15,'2011-1-3 21:04:01' union all
select 15,'2011-1-3 22:10:01' union all
select 12,'2011-1-4 11:04:01' union all
select 11,'2011-1-4 20:10:01' union all
select 13,'2011-1-4 21:05:01' union all
select 13,'2011-1-4 11:05:01'
select total as maxtotal,time
from @table1 a
where not exists(select 1 from @table1 where datediff(dd,[time],a.[time]) = 0
and (total > a.total or (total = a.total and id < a.id)))
/**************
maxtotal time
---------------------- -----------------------
15 2011-01-03 21:04:01.000
13 2011-01-04 21:05:01.000
(2 行受影响)
declare @table1 table
(
id int identity(1,1) not null,
total float null,
time datetime null
)
insert into @table1
select 10,'2011-1-3 12:10:01' union all
select 11,'2011-1-3 21:04:01' union all
select 15,'2011-1-3 22:10:01' union all
select 12,'2011-1-4 11:04:01' union all
select 11,'2011-1-4 20:10:01' union all
select 13,'2011-1-4 21:05:01' union all
select 9,'2011-1-4 11:05:01'
;with cte as
(
select *,ROW_NUMBER() OVER(PARTITION BY Convert(Date,TIME) ORDER BY total desc) as RN from @table1
)
select * from cte where RN=1
select total as maxtotal,time
from t a
where not exists(select 1 from t where datediff(dd,[time],a.[time]) = 0
and total > a.total and [time]<a.[time])
----------------------------------
3 15 2011-01-03 22:10:02.000
6 13 2011-01-04 21:05:21.000
select * from a where
time in (select max(time) from a group by convert(varchar(10),time,120))
select
total as maxtotal,time
from
tb t
where
total=(select max(total) from tb where datediff(dd,[time],t.[time]) = 0)
select total as maxtotal,time
from t a
where not exists(select 1 from t where datediff(dd,[time],a.[time]) = 0 and total > a.total)
select total as maxtotal,time
from t a
where not exists(select 1 from t where datediff(dd,[time],a.[time])=0 and total>a.total)