English 中文(简体)
SQL SQL SQL Server 2005 RANK 函数
原标题:SQL Server 2005 RANK function

SQL Server 2005 出现问题:

假设我有一个表格 PLAYER ( string播放器, int 评分, bol active ) 和这个查询 :

SELECT PLAYER.player AS NAME,
   PLAYER.score AS SCORE,
   POSITION = CASE WHEN PLAYER.Active THEN RANK()OVER(ORDER BY score desc) else NULL end
from PLAYER

问题是当玩家不活动时, 所产生的位置不是连续的 。

例如:

JOHN,10000,1
PETER,5000,NULL (NOT ACTIVE)
CHARLES,2500,3 (SHOULD HAVE POSITION 2, NOT 3)

我为我可怕的英语感到抱歉,我希望我已经解释了我的观点

最佳回答
declare @Player table
(
  player varchar(20),
  score int,
  state int
)

insert into @Player values ( JOHN , 10000, 1)
insert into @Player values ( PETER , 5000, NULL)
insert into @Player values ( PAUL , 5000, 2)
insert into @Player values ( CHARLES , 2500, 1)

select player as Name,
       score,
       case
          when state = 1 then
            rank() over(partition by state order by score desc)
          else null
       end as position
from @Player     

结果:

Name                 score       position
-------------------- ----------- --------------------
PETER                5000        NULL
JOHN                 10000       1
CHARLES              2500        2
PAUL                 5000        NULL
问题回答

尝试此 :

POSITION = 
   RANK()OVER(ORDER BY CASE WHEN Active IS NULL THEN 1 ELSE 0 END ASC, score DESC) 

< 强度 > 编辑 :

...but the problem is that actually my "Active" column is not boolean, the real column is an integer called "state" and it must equal 1 to be active. How should the code be in that case?

然后,这应当奏效:

POSITION = 
   RANK()OVER(ORDER BY CASE WHEN State = 1 THEN 0 ELSE 1 END ASC, score DESC) 

尝试此 :

SELECT PLAYER.player AS NAME, 
PLAYER.score AS SCORE, 
POSITION = RANK()OVER(ORDER BY score desc) 
from PLAYER
WHERE ACTIVE IS NOT NULL
UNION
SELECT PLAYER.player AS NAME, 
PLAYER.score AS SCORE,NULL  
from PLAYER
WHERE ACTIVE IS NULL

与已经回答的询问相比,我不知道这样做会有多有效率,但是这容易理解。





相关问题
SQL Server database is not visible

I have installed ASP.NET application with database on our server. ASP.NET application created database using connection string below. The problem is that I do not see database in SQL Server Management ...

Most efficient way to store number 11.111 in SQL Server

What datatype is the most efficient to store a number such as 11.111. The numbers will have up 2 digits before the point and up to three after. Currently the column is a bigint , I am guessing that ...

表格和数据表

是否需要在提瓜表之后更新表格统计数据,还是自动更新?

Inconsistent Generate Change Script

I add a column of type tinyint and being set to not allow nulls in a table and generate the change scripts. The table has data in it at this time. The script has code that creates a temp table and ...

Performance of Sql subqueriesfunctions

I am currently working on a particularly complex use-case. Simplifying below :) First, a client record has a many-to-one relationship with a collection of services, that is, a single client may have ...

selecting one value out of an xml column

I have a particularly troublesome xml column to query data from. The schema is fixed by the Quebec Ministry of Revenue so "it is what it is" The important part of the query looks like this: with ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签