眼冒绿光,跪求一条SQL语句~~~
表A——(A_id是选项的ID,title是选项的标题,num是这个选项所获得的票数)
A_id title num
1 吃饭 80
2 睡觉 75
3 看书 80
4 上网 92
5 游戏 70
表B——(每一条投票的具体信息,A_id是前面选项的ID,date是投票时的时间,istrue是这条投票是否有效)
B_id A_id date istrue
1 2 2006-8-27 11:42:08 是
2 2 2006-8-27 11:37:24 否
...
现在想统计出票数最多并且有效的前3个选项,按一、二、三等奖排名次,就比如表A的4,1,3这3条记录。但是有一个问题,其中1和3票数相同,同是80票,现在想让谁最先到80票,谁就得第2。请问这个SQL语句该怎么写,大家帮帮忙吧,谢谢了~~
问题点数:100、回复次数:38Top
1 楼YiZhiNet(九斤半)回复于 2006-08-27 12:47:54 得分 20
create table A(A_id int,title varchar(10),num int)
insert A
select 1 ,'吃饭',80 union all
select 2 ,'睡觉',75 union all
select 3 ,'看书',80 union all
select 4 ,'上网',92 union all
select 5 ,'游戏',70
--select * from A
create table B(B_id int,A_id int,date datetime,istrue char(2))
insert B
select 1,2,'2006-8-27 11:42:08','是' union all
select 1,1,'2006-8-25 11:42:08','是' union all
select 1,3,'2006-8-24 11:42:08','是' union all
select 1,3,'2006-8-23 11:42:08','是' union all
select 2,2,'2006-8-27 11:37:24','否'
--select * from B
select
*,
排名 = (select count(*) from A where num >c.num) + 1,
最后投票日期=(select max(date) from B where A_id=c.A_id)
from A c
order by 排名,(select max(date) from B where A_id=c.A_id)
drop table A,BTop
2 楼YiZhiNet(九斤半)回复于 2006-08-27 13:03:23 得分 0
那个排名1,2,2不知道怎么改成1,2,3
select * from A c order by num desc,(select max(date) from B where A_id=c.A_id)Top
3 楼i9988(冒牌j9988 V0.4)回复于 2006-08-27 13:40:51 得分 0
select A_id,title,num,(select count(*) from a a2 where num>a1.num or num=a1.num and (select max(date) from b where a_id=a2.a_id and istrue='是')<(select max(date) from b where a_id=a1.a_id and istrue='是')) as 排名
from a a1
order by 3 desc,4Top
4 楼i9988(冒牌j9988 V0.4)回复于 2006-08-27 13:41:18 得分 20
select A_id,title,num,(select count(*) from a a2 where num>a1.num or num=a1.num and (select max(date) from b where a_id=a2.a_id and istrue='是')<(select max(date) from b where a_id=a1.a_id and istrue='是')) as 排名
from a a1
order by 4
Top
5 楼i9988(冒牌j9988 V0.4)回复于 2006-08-27 13:45:12 得分 0
排名改下,从1开始
select A_id,title,num,1+(select count(*) from a_T a2 where num>a1.num or (num=a1.num and (select max(date) from b_T where a_id=a2.a_id and istrue='是')<(select max(date) from b_T where a_id=a1.a_id and istrue='是'))) as 排名
from a_T a1
order by 4
Top
6 楼waterfirer(水清)回复于 2006-08-27 13:58:28 得分 10
select top 3 A_id,title,num from A join (select A_id,max(date) md from B where istrueb='是' group by A_id) C on A.A_id=C.A_id order by A.num,C.mdTop
7 楼yourantian()回复于 2006-08-27 14:54:46 得分 0
回:i9988(冒牌j9988 V0.3)
order by 4 排序不管用啊,不是根据排名的次序来排的
Top
8 楼waterfirer(水清)回复于 2006-08-27 15:53:29 得分 0
yourantian() :试下我的方法
另外,A表的num可以通过B表统计出来Top
9 楼zjcxc(邹建)回复于 2006-08-27 16:17:57 得分 20
create table A(A_id int,title varchar(10),num int)
insert A
select 1 ,'吃饭',80 union all
select 2 ,'睡觉',75 union all
select 3 ,'看书',80 union all
select 4 ,'上网',92 union all
select 5 ,'游戏',70
--select * from A
create table B(B_id int,A_id int,date datetime,istrue char(2))
insert B
select 1,2,'2006-8-27 11:42:08','是' union all
select 1,1,'2006-8-25 11:42:08','是' union all
select 1,3,'2006-8-24 11:42:08','是' union all
select 1,3,'2006-8-23 11:42:08','是' union all
select 2,2,'2006-8-27 11:37:24','否'
--select * from B
select top 3
A.*,
排名 = (
select count(*)
from A aa
where aa.num > a.num or
aa.num=a.num and isnull((select max(date) from b where a_id=aa.a_id and istrue = '是'),0)>=isnull(b.date,0)
),
B.date
from A
left join(
select A_id, date = max(date)
from B
where istrue = '是'
group by A_id
)b
on a.A_id = b.A_id
order by 排名
go
drop table A,B
-- 结果:
A_id title num 排名 date
----------- ---------- ----------- ----------- -----------------------
4 上网 92 1 NULL
1 吃饭 80 2 2006-08-25 11:42:08.000
3 看书 80 3 2006-08-24 11:42:08.000
(3 行受影响)Top
10 楼i9988(冒牌j9988 V0.4)回复于 2006-08-27 16:24:09 得分 0
哈哈
那根据什么
测试过没有?没测试别说话
Top
11 楼duanzhi1984(莫邪)回复于 2006-08-27 17:37:19 得分 0
”zjcxc(邹建)“
真的是高人啊,没有什么能难到的啊!!!奇人。Top
12 楼zsforever(虎虎)回复于 2006-08-27 18:42:12 得分 0
zjcxc(邹建) 老大的回答规范且有效率 , 顶Top
13 楼digitalbeijing_001()回复于 2006-08-27 20:35:41 得分 0
顶!
顶!
我自己业余时间写的网站,大家看看给点意见,谢谢啦!(没广告,没病毒)
都是Flash游戏,大家别想歪了哦,呵呵!
【偷看洗澡美女】很简单,把泡沫挪开,然后可以看到。。。。。
http://www.hunbei.com.cn/flash/flash_play.asp?id=3363
【透视眼镜】
http://www.hunbei.com.cn/flash/flash_play.asp?id=3317
【美女脱衣猜大小】
http://www.hunbei.com.cn/flash/flash_play.asp?id=3269
另外小游戏频道还有很多游戏,都挺好玩的。位置是:Flash频道->小游戏->美女Top
14 楼polestarxu(一点星光)回复于 2006-08-27 23:19:49 得分 0
upTop
15 楼dongddong(大脚板雪糕 )回复于 2006-08-28 03:55:49 得分 0
邹老大
这句呢
SELECT top 3 A.*,b1.* FROM A
LEFT JOIN B b1 ON A.a_id = b1.a_id
ORDER BY A.num DESC
没写排名进去
和你那个效果一样嘛
不过你那个不对吧,人家是先投先排哦
1 吃饭 80 2 2006-08-25 11:42:08.000
3 看书 80 3 2006-08-24 11:42:08.000
注意这个地方的时间是错的哟Top
16 楼dongddong(大脚板雪糕 )回复于 2006-08-28 03:57:32 得分 0
应该是这个效果吧
排名
4 上网 92 1 NULL
3 看书 80 2 2006-08-24 11:42:08.000
1 吃饭 80 3 2006-08-25 11:42:08.000
Top
17 楼wisdomone()回复于 2006-08-28 06:47:57 得分 0
upTop
18 楼sunbird69(太阳鸟)回复于 2006-08-28 07:47:18 得分 0
upTop
19 楼i_love_pc(杰子)(欢迎加入技术交流QQ群:23640432)回复于 2006-08-28 08:05:31 得分 0
学习!Top
20 楼li_net(忘了我是谁)回复于 2006-08-28 08:23:59 得分 0
studyTop
21 楼louzg()回复于 2006-08-28 09:25:10 得分 0
select ID,title,num from (
select b.A_id ID,a.title title,count(distinct b.A_id) num,max(b.date)
from A a,B b
where a.A_id = b.A_id and b.istrue = '是'
group by b.A_id
order by count(distinct b.A_id) desc,max(b.date) asc
) where rownum < '4';
Top
22 楼zjcxc(邹建)回复于 2006-08-28 09:33:53 得分 0
select top 3
A.*,
排名 = (
select count(*)
from A aa
where aa.num > a.num or
aa.num=a.num and isnull((select max(date) from b where a_id=aa.a_id and istrue = '是'),0)<=isnull(b.date,0) -- 先回先排的话, 改这里面为<=
),
B.date
from A
left join(
select A_id, date = max(date)
from B
where istrue = '是'
group by A_id
)b
on a.A_id = b.A_id
order by 排名
Top
23 楼mengyang(梦阳)回复于 2006-08-28 10:03:11 得分 0
TO zjcxc(邹建)
很有创意,学习中。Top
24 楼MicrosoftSQL(改行做冒牌 V0.1)回复于 2006-08-28 10:47:10 得分 0
再请邹大哥:
create table A
(
A_id int,
title varchar(10),
num int,
LastDT datetime
)
insert A(A_id,title,num,LastDT)
select 1,'吃饭',80,'2006-8-27 11:42:08' union all
select 2,'睡觉',75,'2006-8-26 09:55:09' union all
select 3,'看书',80,'2006-8-25 10:37:24' union all
select 4,'上网',92,'2006-8-28 11:39:25' union all
select 5,'游戏',70,'2006-8-27 11:40:18'
select * from A order by num desc,lastDT
-- 同上,加一字段最后投票日期在表A中
-- 如何得到如下结果:
/*
A_id title num LastDT 排名
----------- ---------- ----------- --------------------------
4 上网 92 2006-08-28 11:39:25.000 1
3 看书 80 2006-08-25 10:37:24.000 2
1 吃饭 80 2006-08-27 11:42:08.000 3
2 睡觉 75 2006-08-26 09:55:09.000 4
5 游戏 70 2006-08-27 11:40:18.000 5
*/
drop table ATop
25 楼suntt(两条腿的狗)回复于 2006-08-28 11:03:00 得分 0
简简单单
select top 3 a.A_id from a inner join b on a.A_id=b.A_id order by num desc,date descTop
26 楼Linux_9(($$$$))回复于 2006-08-28 11:41:52 得分 0
留着以后学习吧!!!
顶一个!!Top
27 楼YiZhiNet(九斤半)回复于 2006-08-28 11:45:01 得分 0
如果LastDT日期值相同的话还是会并列第 几 名的
select *,
排名=
(
select count(*) from A
where num > CC.num
or (num = CC.num and LastDT<=CC.LastDT)
)
from A CC
order by 排名Top
28 楼gjz_1209(龙行天下--好想辞职!)回复于 2006-08-28 11:52:28 得分 0
眼冒绿光,必有出众之处,哈哈。。。Top
29 楼zjcxc(邹建)回复于 2006-08-28 12:00:21 得分 0
select
A.*,
排名 = (
select count(*)
from A aa
where aa.num > a.num or
aa.num=a.num and LastDT<=a.LastDT
),
A.LastDT
from A
order by 排名Top
30 楼kiss_me(祥仔)回复于 2006-08-28 12:15:06 得分 0
顶!!!Top
31 楼zhaojian68326952(黎明前的黑暗)回复于 2006-08-28 15:19:39 得分 10
select a_id,count(*) as cnt,max(t.votedate) as maxdate from B where istrue='是'
group by a_id order by cnt desc,maxdate
我觉得上面各位想的太复杂了吧,不知道还是我理解的不对?
如果我理解的对的话,楼主试试这个,可以的,我试过了!Top
32 楼zhaojian68326952(黎明前的黑暗)回复于 2006-08-28 15:20:38 得分 0
select a_id,count(*) as cnt,max(date) as maxdate from B where istrue='是'
group by a_id order by cnt desc,maxdate
修正,不好意思!
Top
33 楼quanyi(长生天)回复于 2006-08-28 15:36:50 得分 0
markTop
34 楼specialsoldier(雪地撒野~噢姐姐,我要回家)回复于 2006-08-28 16:24:59 得分 20
create table test(B_id int,A_id int,date datetime,istrue char(2))
insert test
select 1,2,'2006-8-27 11:42:08','是' union all
select 1,1,'2006-8-25 11:42:08','是' union all
select 1,3,'2006-8-24 11:42:08','是' union all
select 1,3,'2006-8-23 11:42:08','是' union all
select 2,2,'2006-8-27 11:37:24','是'
select a.a_id,a.sum,count(b.a_id)+1 pri
from
(select a_id,sum(1)sum,max(date)date from test
where istrue='是'
group by a_id)a
left join
(select a_id,sum(1)sum,max(date)date from test
where istrue='是'
group by a_id)b
on a.sum<b.sum or(a.sum=b.sum and a.date>b.date)
group by a.a_id,a.sum
order by pri
结果:
a_id sum pri
3 2 1
2 2 2
1 1 3
感觉逻辑上a表没什么作用,所以只试验了对B表排名的操作,最后关联下A表就可以了Top
35 楼achongsky(灵魂)回复于 2006-08-28 16:51:02 得分 0
MarkTop
36 楼helloMoney(你好,金钱)回复于 2006-08-28 17:51:48 得分 0
头一次看到钻石!Top
37 楼sunkefei521()回复于 2006-10-24 08:16:46 得分 0
是不是人呀,竟然有四个钻石!Top
38 楼sunkefei521()回复于 2006-10-24 09:03:56 得分 0
世间竟有如此奇人!Top




