| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3627 人关注过本帖
标题:根据时间排序的sql语句
只看楼主 加入收藏
小赵q1
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:4
帖 子:492
专家分:777
注 册:2011-8-26
结帖率:100%
收藏
已结贴  问题点数:100 回复次数:3 
根据时间排序的sql语句
在工作中遇到一个问题:
一个活动有两个游戏关卡:第一关、第二关。
用户进入某一关且在要求的时间内闯关成功,就会在数据库中记录用户闯过这一关的时间,否则这一关的时间默认为0;
数据库中保存的数据格式如下:

图片附件: 游客没有浏览图片的权限,请 登录注册


目前项目需求是:
如果用户通过了第一关和第二关,就显示在最上面;
如果用户只通过了第一关,就显示在中间;
如果用户两关都没有参加或通过,就显示在最后;
以上是显示的先后顺序,且这些数据都要按照时间相加后从小到大排列。
求高手帮写个sql语句实现效果
格式如下:
排序号   姓名        第一关时间        第二关时间
1         a               10.32            23.37
2         b               8.43             15.33
3         c               11.35            28.47
4         d               11.00            31.27
5         e               19.55            0
6         f               21.37            0
7         g               21.42            0
8         h               30.33            0
9         i               33.00            0
10        j               0                0
11        k               0                0
12        l               0                0
13        m               0                0

数据库附件如下:
puzzle_user.zip (2.51 KB)


[此贴子已经被作者于2016-5-27 11:09编辑过]

搜索更多相关主题的帖子: sql语句 数据库 闯关 记录 项目 
2016-05-27 11:07
mywisdom88
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:191
帖 子:3146
专家分:8408
注 册:2015-3-25
收藏
得分:60 
--测试环境 SQL2000,建立测试数据,#games 为SQL2000的临时表名称
create table #games(NickName varchar(12),One_Best_Time numeric(5,2),Two_Best_Time numeric(5,2))
insert into #games(NickName,One_Best_Time,Two_Best_Time) values('张1',20.12,24.21)
insert into #games(NickName,One_Best_Time,Two_Best_Time) values('张2',10.22,20.12)
insert into #games(NickName,One_Best_Time,Two_Best_Time) values('张3',21.22,20.12)
insert into #games(NickName,One_Best_Time,Two_Best_Time) values('张4',13.22,22.12)
insert into #games(NickName,One_Best_Time,Two_Best_Time) values('张5',14.21,42.12)
insert into #games(NickName,One_Best_Time,Two_Best_Time) values('张6',15.22,12.12)
insert into #games(NickName,One_Best_Time,Two_Best_Time) values('张7',16.24,22.12)
insert into #games(NickName,One_Best_Time,Two_Best_Time) values('张8',17.52,24.12)
insert into #games(NickName,One_Best_Time,Two_Best_Time) values('张9',18.62,12.12)

insert into #games(NickName,One_Best_Time,Two_Best_Time) values('李1',21.22,0.0)
insert into #games(NickName,One_Best_Time,Two_Best_Time) values('李2',11.32,0.0)
insert into #games(NickName,One_Best_Time,Two_Best_Time) values('李3',21.42,0.0)
insert into #games(NickName,One_Best_Time,Two_Best_Time) values('李4',41.65,0.0)

insert into #games(NickName,One_Best_Time,Two_Best_Time) values('王1',0.0,0.0)
insert into #games(NickName,One_Best_Time,Two_Best_Time) values('王2',0.0,0.0)
insert into #games(NickName,One_Best_Time,Two_Best_Time) values('王3',0.0,0.0)
insert into #games(NickName,One_Best_Time,Two_Best_Time) values('王4',0.0,0.0)

--借1个字段来完成排续,没完成的默认完成最大时间为 999.99
select *,(one_best_time + two_best_time)id from #games where one_best_time>0 and two_best_time>0
union all
select *,(one_best_time + 999.99)id from #games where one_best_time>0 and two_best_time<=0
union all
select *,(999.99 + 999.99)id from #games where one_best_time<=0 and two_best_time<=0
order by id
图片附件: 游客没有浏览图片的权限,请 登录注册
2016-05-28 11:13
mywisdom88
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:191
帖 子:3146
专家分:8408
注 册:2015-3-25
收藏
得分:40 
2楼的缺点是:有个默认最大时间 999.99,下面的缺是 TOP 99999,超过99999条记录后,每种的就不显示后面的。
select * from (select top 99999 (one_best_time + two_best_time) id,* from #games where one_best_time>0 and two_best_time>0 order by id asc)g1
union all
select * from (select top 99999 (one_best_time + two_best_time) id,* from #games where one_best_time>0 and two_best_time=0 order by id asc)g2
union all
select * from (select top 99999 (one_best_time + two_best_time) id,* from #games where one_best_time=0 and two_best_time=0 order by id asc)g3
图片附件: 游客没有浏览图片的权限,请 登录注册


[此贴子已经被作者于2016-5-28 11:43编辑过]

2016-05-28 11:38
小赵q1
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:4
帖 子:492
专家分:777
注 册:2011-8-26
收藏
得分:0 
回复 2楼 mywisdom88
谢谢大牛,第一种方式突破了top的那种局限,是很好用的,第二种方式代码会稍微简单一点,我都测试了一下,能得到想要的效果,又get了一种新技能呀,哈哈,谢谢帮忙。
2016-05-30 13:47
快速回复:根据时间排序的sql语句
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018699 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved