这个SQL怎么写?

伟大de虫子 2003-04-24 09:02:35
ID Name Score LastTime

1 jack 1000 2003-4-20 xx:xx:xx
2 merry 1300 2003-4-20 xx:xx:xx
3 lily 2000 2003-4-21 xx:xx:xx
.. ....... ...... .................
.. ....... ...... .................
22 jack 3100 2003-5-12 xx:xx:xx
23 mark 1800 2003-5-12 xx:xx:xx
... ..... ...... ...................
-----------------------------------------------------------

如上面的结构,使用SQl取出每一个月中的第一名,即算出每个月份中score最高的那个人的name,不是单算一个月的,是所有记录中每一个月中的第一名:

2003-4月最高分:xxx
2003-5月最高分:xxx
...............
..............
...全文
27 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
伟大de虫子 2003-04-26
  • 打赏
  • 举报
回复
我试试先。
伟大de虫子 2003-04-25
  • 打赏
  • 举报
回复
谢谢,我真的需要
stormhu 2003-04-25
  • 打赏
  • 举报
回复
不好意思,忘了表名,我的在sql server下通过,我再试试Acess
jacklinchen 2003-04-25
  • 打赏
  • 举报
回复

select year(last_time) y,month(last_time) m,max(score) from tablename group by y,m order by y,m
mybeetle 2003-04-25
  • 打赏
  • 举报
回复
要用到子查询
stormhu 2003-04-25
  • 打赏
  • 举报
回复
这样更合理一些

SELECT name, score, lasttime
FROM MonthWin
WHERE score in
(SELECT Max(score) FROM MonthWin
GROUP BY month(lasttime))
ORDER BY lasttime
stormhu 2003-04-25
  • 打赏
  • 举报
回复
Access 中通过

但是同样的成绩比如某个月有两个人以上,则都会显示

select name,score,lasttime from MonthWin where score in
(SELECT Max(score) FROM MonthWin
GROUP BY Left(lasttime,7))
ORDER BY lasttime
youngest 2003-04-24
  • 打赏
  • 举报
回复
每次将数据写入数据库前先取得该月的月数(比如2003-5-15,把5取出来)

然后把取出来的月份一同写入数据库

当要列出没月第一名的时候可以根据月份的字段查询
Fengtengxiaoyun 2003-04-24
  • 打赏
  • 举报
回复
select max(score) as maxscroe group by year(lastyear)&month(lastyear)
wangwanfu 2003-04-24
  • 打赏
  • 举报
回复
关注
billyqiao 2003-04-24
  • 打赏
  • 举报
回复
试试:select max(score) as maxscore group by cstr(year(lastyear)) & cstr(month(lastyear))
伟大de虫子 2003-04-24
  • 打赏
  • 举报
回复
也即是说如果记录中有1、2、3、4、6、8这几个月的记录,那么是一齐取出这6个月的第一名。
就是有6个第一名(一个月一个)
伟大de虫子 2003-04-24
  • 打赏
  • 举报
回复
好象都不对啊。是把所有月份中的第一名取出来
  • 打赏
  • 举报
回复
select top 1 name from tablename where month(lasttime)=month order by score desc

或者 写
select max(score),name from tablename where lasttime between monthbegin and monthend

不知道对不对,请大家多指教。
cpp2017 2003-04-24
  • 打赏
  • 举报
回复
access:
select cstr(year(last_time)) +'-'+ cstr(month(last_time)),max(score)
from table
group by cstr(year(last_time)) +'-'+ cstr(month(last_time))

sql server:
select cast(year(last_time)
select cast(year(last_time) as varchar(4)) +'-'+ cast(month(last_time) as varchar(2)),max(score)
from table
group by cast(year(last_time) as varchar(4)) +'-'+ cast(month(last_time) as varchar(2))
eafin 2003-04-24
  • 打赏
  • 举报
回复
select name from [table] order by score DESC
伟大de虫子 2003-04-24
  • 打赏
  • 举报
回复
up
伟大de虫子 2003-04-24
  • 打赏
  • 举报
回复
要显示的内容如下:

姓名 积分 日期

xxx 2000 2003-xx-xx
xxx 3000 2003-xx-xx
xxx 2400 2003-xx-xx
... ...... ..........

----------------------------------------
你上面给出的也不能正常运行,使用的是access,表名为MonthWin
stormhu 2003-04-24
  • 打赏
  • 举报
回复
是否还想显示 Name,Id
那肯定得变通一下才行
伟大de虫子 2003-04-24
  • 打赏
  • 举报
回复
好象提供的都不行啊。都不完善的
加载更多回复(2)

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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