表结构:id name value
a1 a 4.0
a2 b 2.0
a3 c 3.0按照value排序后的结果:id name value
a2 b 2.0
a3 c 3.0
a1 a 4.0最终希望返回的结果:id name value 排名
a2 b 2.0 1
a3 c 3.0 2
a1 a 4.0 3即,插入一列,该列的内容是当前该记录在结果集的排名(也可以是0,1,2)。
请问这样的sql如何写?谢谢。
a1 a 4.0
a2 b 2.0
a3 c 3.0按照value排序后的结果:id name value
a2 b 2.0
a3 c 3.0
a1 a 4.0最终希望返回的结果:id name value 排名
a2 b 2.0 1
a3 c 3.0 2
a1 a 4.0 3即,插入一列,该列的内容是当前该记录在结果集的排名(也可以是0,1,2)。
请问这样的sql如何写?谢谢。
解决方案 »
- 急,sqlserver进不去了?[
- 删除出此状况何故(有图)
- (select @ClickFee=sum(ClickFee) from a where AdCampaignId=Id) as ClickFee,这有错误,怎么改一下。
- 这样的字段应该怎么update?
- 求一SQL语句!中等难度!
- sql server 2000 横向纵向数据查询
- 关于2个事务同时执行的一个问题,麻烦帮看看。
- 请教大家jsp连接sql数据库问题(急!)
- 对您很简单,对我很难,SQL server的存储过程中select一个表后如何得到记录数
- 请问:我想每天定自动时更新数据库的某个字段值(ASP+SQL SERVER2000),我该怎么做??
- 关于视图的使用
- 循环Update问题
应该语法是这样的
2005/2008才行的,2000用临时表可以解决
select *,排名=(select count(1)+1 from tb a where a.value<value)
from tb
order by value desc
(SELECT COUNT(*) FROM TB WHERE [value]<=T.[value ] ) AS 排名
FROM TB T
ORDER BY (SELECT COUNT(*) FROM TB WHERE [value]<=T.[value ] )
排名=(select COUNT(*) from tb where T.value<=value),
*
from tb t
order by value
/***********************************************--> 测试数据:[tb]
--> 测试时间:2009-08-10 16:58:51
--> 我的淘宝:<<戒色坊>> http://shop36766744.taobao.com/***********************************************/if object_id('[tb]') is not null drop table [tb]
create table [tb]([id] varchar(2),[name] varchar(1),[value] numeric(2,1))
insert [tb]
select 'a1','a',4.0 union all
select 'a2','b',2.0 union all
select 'a3','c',3.0select *,flag=(select count(id)+1 from tb where t.value>value) from [tb]t
order by cast(value as int)/*
id name value flag
---- ---- --------------------------------------- -----------
a2 b 2.0 1
a3 c 3.0 2
a1 a 4.0 3(3 行受影响)
*/drop table TB
排名=(select COUNT(*) from tb where T.value>=value),
*
from tb t
order by value
drop table [tb]
go
create table [tb]([id] varchar(2),[name] varchar(1),[value] numeric(2,1))
insert [tb]
select 'a1','a',4.0 union all
select 'a2','b',2.0 union all
select 'a3','c',3.0select
排名=(select COUNT(*) from tb where T.value>=value),
*
from tb t
order by value
/*
排名 id name value
----------- ---- ---- ---------------------------------------
1 a2 b 2.0
2 a3 c 3.0
3 a1 a 4.0
*/
declare @t table(id varchar(10), [name] varchar(10), [value] dec(10,2))
insert @t select 'a1' ,'a', 4.0
insert @t select 'a2' , 'b' , 2.0
insert @t select 'a3', 'c' , 3.0
select *,(select count(*) from @t where [value]<=t.[value])as 排名 from @t t
order by [value]
/*id name value 排名
---------- ---------- --------------------------------------- -----------
a2 b 2.00 1
a3 c 3.00 2
a1 a 4.00 3(3 個資料列受到影響)*/
create table [tb]([id] varchar(2),[name] varchar(1),[value] numeric(2,1))
insert [tb]
select 'a1','a',4.0 union all
select 'a2','b',2.0 union all
select 'a3','c',3.0SELECT * ,
(SELECT COUNT(*) FROM TB WHERE [value]<=T.[value ] ) AS 排名
FROM TB T
ORDER BY (SELECT COUNT(*) FROM TB WHERE [value]<=T.[value ] )(所影响的行数为 3 行)id name value 排名
---- ---- ----- -----------
a2 b 2.0 1
a3 c 3.0 2
a1 a 4.0 3(所影响的行数为 3 行)
create table tt(id varchar(10),name varchar(10),value decimal(14,2))
insert tt
select 'a1', 'a', 4.0
union all select 'a2', 'b', 2.0
union all select 'a3', 'c', 3.0select *,排序=row_number() over(order by value) from tt
select *,排序=rank() over(order by value) from tt /*
id name value 排序
a2 b 2.00 1
a3 c 3.00 2
a1 a 4.00 3
*/