字段排名

guodong66 2009-11-02 10:37:32
表中2个字段
--------------------------------------------------
----id----------------fenshu------------
----1 22
----2 23
----3 1
----4 1
----5 21
----6 2
----7 2
----8 22

类似这样的一个表结构,现在要查询出这个样子

----分数 ----------名次 ----id
23 1 2
22 2 1
22 2 8
21 4
2 5
2 5

也就是按名次查询出分数,这个能不能用case when 实现? 或者其他办法也行。
...全文
126 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
华夏小卒 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 guodong66 的回复:]
引用 21 楼 wufeng4552 的回复:
引用 19 楼 fredrickhu 的回复:
引用 18 楼 wufeng4552 的回复:
引用 16 楼 fredrickhu 的回复:
引用 15 楼 wufeng4552 的回复:
引用 12 楼 fredrickhu 的回复:
晕 被水哥忽悠了

沒忽悠你啊

我的没写反

看樓主的要求阿


我觉得7楼的满足要求 好象排名不连续 哈哈

count(distinct fenshu)
我被妳忽悠了


彻底倒了 count(distinct fenshu) 这个结果可以满足另一种要求,j2ee区怎么解决个问题就那么麻烦呢???
以后来ms区混了。
[/Quote]
guodong66 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 wufeng4552 的回复:]
引用 19 楼 fredrickhu 的回复:
引用 18 楼 wufeng4552 的回复:
引用 16 楼 fredrickhu 的回复:
引用 15 楼 wufeng4552 的回复:
引用 12 楼 fredrickhu 的回复:
晕 被水哥忽悠了

沒忽悠你啊

我的没写反

看樓主的要求阿


我觉得7楼的满足要求 好象排名不连续 哈哈

count(distinct fenshu)
我被妳忽悠了
[/Quote]

彻底倒了 count(distinct fenshu) 这个结果可以满足另一种要求,j2ee区怎么解决个问题就那么麻烦呢???
以后来ms区混了。
贾桂权 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jia_guijun 的回复:]
SQL codeselect 分数,名次=rank()over(orderby 分数),idfrom tb
[/Quote]

忘记加DESC


select 分数,名次=rank() over(order by 分数 desc),id from tb
水族杰纶 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 fredrickhu 的回复:]
引用 18 楼 wufeng4552 的回复:
引用 16 楼 fredrickhu 的回复:
引用 15 楼 wufeng4552 的回复:
引用 12 楼 fredrickhu 的回复:
晕 被水哥忽悠了

沒忽悠你啊

我的没写反

看樓主的要求阿


我觉得7楼的满足要求 好象排名不连续 哈哈
[/Quote]
count(distinct fenshu)
我被妳忽悠了
guodong66 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fredrickhu 的回复:]
SQL codeselect 分数,名次=(selectcount(分数)+1from tbwhere t.分数<分数),idfrom tb t
[/Quote]

貌似这个正解,呵呵,ms-sql区好多牛人在活动撒,j2ee区都是星星一下的。呵呵

这个用case when 能实现么?
select id , fen
(
case fen
when t.fen>=fen then 1;
else 0;
end;
)
from marks as t

当然我写的这个肯定是错的,我的意思是类似这个样子的语句能实现么? 这是我最初的想法
--小F-- 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 wufeng4552 的回复:]
引用 16 楼 fredrickhu 的回复:
引用 15 楼 wufeng4552 的回复:
引用 12 楼 fredrickhu 的回复:
晕 被水哥忽悠了

沒忽悠你啊

我的没写反

看樓主的要求阿
[/Quote]

我觉得7楼的满足要求 好象排名不连续 哈哈
水族杰纶 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 fredrickhu 的回复:]
引用 15 楼 wufeng4552 的回复:
引用 12 楼 fredrickhu 的回复:
晕 被水哥忽悠了

沒忽悠你啊

我的没写反
[/Quote]
看樓主的要求阿
纸纸纸纸飞飞 2009-11-02
  • 打赏
  • 举报
回复

很不错。
--小F-- 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wufeng4552 的回复:]
引用 12 楼 fredrickhu 的回复:
晕 被水哥忽悠了

沒忽悠你啊
[/Quote]
我的没写反
水族杰纶 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 fredrickhu 的回复:]
晕 被水哥忽悠了
[/Quote]
沒忽悠你啊
linste 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 linste 的回复:]
select fenshu  as 分数, 名次=(select count(*) from 表1 where 分数>=表.分数),id from 表
[/Quote]
select fenshu as 分数, 名次=(select count(*) from 表1 where 分数<表.分数)+1,id from 表
水族杰纶 2009-11-02
  • 打赏
  • 举报
回复
修正


--> Title : Generating test data [tb]
--> Author : wufeng4552
--> Date : 2009-11-02 10:40:59
if object_id('[tb]') is not null drop table [tb]
go
create table [tb] (id int,fenshu int)
insert into [tb]
select 1,22 union all
select 2,23 union all
select 3,1 union all
select 4,1 union all
select 5,21 union all
select 6,2 union all
select 7,2 union all
select 8,22
select
fenshu,
名次=(select count(distinct fenshu) from tb where fenshu>=t.fenshu),
id
from tb t
order by (select count(distinct fenshu) from tb where fenshu>=t.fenshu)
/*
fenshu 名次 id
----------- ----------- -----------
23 1 2
22 2 1
22 2 8
21 3 5
2 4 6
2 4 7
1 5 3
1 5 4

(8 個資料列受到影響)

(8 個資料列受到影響)
*/


--小F-- 2009-11-02
  • 打赏
  • 举报
回复
晕 被水哥忽悠了
--小F-- 2009-11-02
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-11-02 10:41:46
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[fenshu] int)
insert [tb]
select 1,22 union all
select 2,23 union all
select 3,1 union all
select 4,1 union all
select 5,21 union all
select 6,2 union all
select 7,2 union all
select 8,22
--------------开始查询--------------------------
select fenshu as 分数,名次=(select count(fenshu)+1 from tb where t.fenshu>fenshu),id from tb t order by 2
----------------结果----------------------------
/* 分数 名次 id
----------- ----------- -----------
1 1 3
1 1 4
2 3 6
2 3 7
21 5 5
22 6 1
22 6 8
23 8 2

(8 行受影响)
*/
linste 2009-11-02
  • 打赏
  • 举报
回复
select fenshu as 分数, 名次=(select count(*) from 表1 where 分数>=表.分数),id from 表
--小F-- 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 fredrickhu 的回复:]
引用 6 楼 wufeng4552 的回复:
引用 2 楼 fredrickhu 的回复:
SQL codeselect 分数,名次=(selectcount(分数)+1from tbwhere t.分数 <分数),idfrom tb t

反了吧

没反
[/Quote]
恩 反了
--小F-- 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wufeng4552 的回复:]
引用 2 楼 fredrickhu 的回复:
SQL codeselect 分数,名次=(selectcount(分数)+1from tbwhere t.分数 <分数),idfrom tb t

反了吧
[/Quote]
没反
--小F-- 2009-11-02
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-11-02 10:41:46
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[fenshu] int)
insert [tb]
select 1,22 union all
select 2,23 union all
select 3,1 union all
select 4,1 union all
select 5,21 union all
select 6,2 union all
select 7,2 union all
select 8,22
--------------开始查询--------------------------
select fenshu as 分数,名次=(select count(fenshu)+1 from tb where t.fenshu<fenshu),id from tb t order by 2
----------------结果----------------------------
/* 分数 名次 id
----------- ----------- -----------
23 1 2
22 2 1
22 2 8
21 4 5
2 5 6
2 5 7
1 7 3
1 7 4

(8 行受影响)

*/
水族杰纶 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fredrickhu 的回复:]
SQL codeselect 分数,名次=(selectcount(分数)+1from tbwhere t.分数<分数),idfrom tb t
[/Quote]
反了吧
水族杰纶 2009-11-02
  • 打赏
  • 举报
回复
--> Title  : Generating test data [tb]
--> Author : wufeng4552
--> Date : 2009-11-02 10:40:59
if object_id('[tb]') is not null drop table [tb]
go
create table [tb] (id int,fenshu int)
insert into [tb]
select 1,22 union all
select 2,23 union all
select 3,1 union all
select 4,1 union all
select 5,21 union all
select 6,2 union all
select 7,2 union all
select 8,22
select
fenshu,
名次=(select count(*) from tb where fenshu>=t.fenshu),
id
from tb t
order by (select count(*) from tb where fenshu>=t.fenshu)
/*
fenshu 名次 id
----------- ----------- -----------
23 1 2
22 3 1
22 3 8
21 4 5
2 6 6
2 6 7
1 8 3
1 8 4

(8 個資料列受到影響)
*/
加载更多回复(4)

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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