sql 根据时间范围聚合提取数据。

来源:百度知道 编辑:UC知道 时间:2024/06/07 20:22:54
这个是关于提取指定日期范围内 提取玩家经验值最高的用户资料
总共有三个表 Game,player,user
Game表字段(该表记录用户登录游戏的资料,这里我只列举需要的字段)

GameID bigint
GameType int 游戏类型
SaveTime datetime 登录游戏时间

Player表字段(该表记录了游戏所的经验和用户ID,和游戏类型 )

GameID bigint (和Game表字段GameID关联)
Score int 经验值
PlayerID bigint 用户ID

User表字段(用户资料)
ID bigint 用户ID 对应 Plarer表 PlayerID
Nick nvarchar 用户呢称
Name 用户名

OK 现在我需要指定一段日期 如2008-9-17日 到2008-9-30 每天18:00至23:00 时间段最高经验值的玩家。

显示列表
日期 游戏类型 玩家呢称 经验值
2008-0-16 DOTA teshu 60
2008-0-17 DOTA vvvv 60
2008-0-18 DOTA bbbbbb 60
2008-0-19 DOTA mmmmm 60
2008-0-20 DOTA tyttyty 60
显示修改如下
日期 游戏类型 玩家呢称 经验值
2008-0-16 2 teshu 60
2008-0-17 2 vvvv 60
2008-0-18 2 bbbbbb 60
2008-0-19 2 mmmmm 60

select convert(varchar(10),bb.savetime,120) as 日期,bb.gametype as 游戏类型,cc.nick as 玩家昵称,aa.maxscore as 经验值 from (select p.playerid,a.gameid,max(b.score) as maxscore from game a inner join player b on a.gameid=b.gameid where (convert(varchar(8),a.savetime,112) between @DateTimeStar and @DateTimeEnd) and (datepart(hh,a.savetime) between 18 and 23) group by b.playerid,a.gameid) as aa inner join game bb on aa.gemeid=bb.gameid inner join player cc on aa.playerid=cc.playerid

select Game.datetime,user.Nick...其他条件 from user left join player on usr.ID=player.playerID left join Game on Pleyer.GameID=Game.GameID
where (Game.datetime between 2008-9-17 and 2008-9-30 ) and 其他条件 order by Player.Score desc

select savetime as 日期,t1.gametype as 游戏类型,
nick as 玩家昵称,max(score) as 经验值
from
(select t1.savetime,t1.gametype,t3.nick,t2.score
from game t1,player t2,user t3
where t1.gameid=t2.gameid and t2.playerid=t3.id,t1.GameType = 2
and t1.savetime between