22,209
社区成员
发帖
与我相关
我的任务
分享
declare @tb table([id] int,[name] nvarchar(5),[score] int,[uploadTime] Datetime)
Insert @tb
select 1,N'web',200,'2008-7-15' union all
select 2,N'web',300,'2008-7-15' union all
select 3,N'huang',400,'2008-6-15' union all
select 4,N'sh',400,'2008-6-16' union all
select 5,N'sh',400,'2008-6-16'
select max(id) as id,name,score,[uploadTime] from @tb t where [score] in(
select max(score) from @tb where datediff(dd,[uploadTime],t.[uploadTime])=0 and name=t.name
) group by name,score,[uploadTime]
order by id
declare @tb table([id] int,[name] nvarchar(5),[score] int,[uploadTime] Datetime)
Insert @tb
select 1,N'web',200,'2008-7-15' union all
select 2,N'web',300,'2008-7-15' union all
select 3,N'huang',400,'2008-6-15' union all
select 4,N'sh',400,'2008-6-16'
select * from @tb t where [score] in(
select max(score) from @tb where datediff(dd,[uploadTime],t.[uploadTime])=0 and name=t.name
)
select distinct convert(varchar(10),uploadtime,120) as 日期,max(score) as 成绩
from tbname
group by convert(varchar(10),uploadtime,120)
-->生成测试数据
declare @tb table([id] int,[name] nvarchar(5),[score] int,[uploadTime] Datetime)
Insert @tb
select 1,N'web',200,'2008-7-15' union all
select 2,N'web',300,'2008-7-15' union all
select 3,N'huang',400,'2008-6-15' union all
select 4,N'sh',400,'2008-6-16'
Select [name],[uploadTime],max([score]) as [score] from @tb
group by [name],[uploadTime]
select [name],[uploadTime],[score] from @tb t
where not exists(select 1 from @tb where [name] =t.[name] and [uploadTime] = t.[uploadTime] and [score]>t.[score])
/*
name uploadTime score
----- ----------------------- -----------
web 2008-07-15 00:00:00.000 300
huang 2008-06-15 00:00:00.000 400
sh 2008-06-16 00:00:00.000 400
*/