数据库查询,急。

netwar 2008-07-18 01:55:31
关大家一个问题,比较急。
数据如图所示,我怎么取得每天的最高分数,用户名。。
select max(score),convert(char(10),uploadtime,120) from scores group by convert(char(10),uploadtime,120)

这样写只能取最高分数,当天日期,但是取不出来用户帐号,

求高手指点。

id name score uploadTime
1 web 200 2008-7-15 12:30:00
2 web 300 2008-7-15
3 huang 400 2008-6-15
4 sh 400 2008-6-16
...全文
68 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
netwar 2008-07-18
  • 打赏
  • 举报
回复
String strSQL = "select * from scores t where [score] in(select max(score) from scores where datediff(dd,t.uploadTime,scores.uploadTime)=0 group by convert(char(10),uploadTime,120) )";
strSQL += " order by score desc,playTime desc,uploadTime desc";

多谢你给我的思路,我已经取到了。谢谢。
wzy_love_sly 2008-07-18
  • 打赏
  • 举报
回复
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


id name score uploadTime
2 web 300 2008-07-15 00:00:00.000
3 huang 400 2008-06-15 00:00:00.000
5 sh 400 2008-06-16 00:00:00.000

取大id,再group by 一下
netwar 2008-07-18
  • 打赏
  • 举报
回复
帅哥,不正确的,如果后面还有一样的分数,就出现错误
wzy_love_sly 2008-07-18
  • 打赏
  • 举报
回复
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
)


id name score uploadTime
2 web 300 2008-07-15 00:00:00.000
3 huang 400 2008-06-15 00:00:00.000
4 sh 400 2008-06-16 00:00:00.000
sdxiong 2008-07-18
  • 打赏
  • 举报
回复
select *
from scores a
where not exists(select * from scores where uploadtime=a.uploadtime and score>a.score)
wzy_love_sly 2008-07-18
  • 打赏
  • 举报
回复
select distinct convert(varchar(10),uploadtime,120) as 日期,max(score) as 成绩
from tbname
group by convert(varchar(10),uploadtime,120)
hery2002 2008-07-18
  • 打赏
  • 举报
回复
-->生成测试数据

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
*/

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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