SQL查询根据百分比分配更新排名

时间:2010-12-03 10:50:32

标签: sql sql-server-2008

员工总数:10

要分配的排名:O,E,G

每个等级分配的百分比:20%,40%,40%

(例如:

1. total emp * 20/100 
 select 10 * 20./100 = 2
 First 2 employees rank to be 'O' 
 2. balance emp count * 40./100 
 select 8 * 40./100 = 3
 Next 3 employees Rank to be 'E'
3.  Balance 5 Employees Rank to be 'G'

我们需要从员工分数降序中分配排名

Create Table Ranking(Rank nvarchar,percentage int)
insert into Ranking values('O',20) 
insert into Ranking values('E',40)
insert into Ranking values('G',40)

Create Table Emp(Empcode nvarchar(3),Score numeric,Ranking nvarchar)  
insert into Emp values('E1',97,null) 
insert into Emp values('E2',95,null) 
insert into Emp values('E3',87,null) 
insert into Emp Values('E4',85,null) 
insert into Emp Values('E5',78,null) 
insert into Emp Values('E6',75,null) 
insert into Emp Values('E7',68,null) 
insert into Emp Values('E8',65,null) 
insert into Emp Values('E9',59,null) 
insert into Emp Values('E10',58,null) 

排名应该根据排名表中可用的百分比进行全涂层

现在E1和E2员工排名“O”  E3,E4,E5员工排名“E”  E6,E7,E8,E9,E10属于等级'G'

2 个答案:

答案 0 :(得分:1)

使用NTILE功能应该可以满足您的需要。怎么回事?

SELECT Empcode
     , Score
     , Ranking = CASE WHEN Band BETWEEN 1 AND 2 THEN 'O'
                  WHEN Band BETWEEN 3 and 6 THEN 'E'
                  WHEN Band > 6 THEN 'G'
                  ELSE '' END
 FROM (SELECT *, Band = NTILE(10) over (order by score DESC)
      FROM EMP) qq

答案 1 :(得分:0)

如何一起运行三个更新查询以实现目标?此解决方案考虑到您将来会有更多员工,并且您将获得每个级别的百分比:

update #Emp set Ranking = 'O'
where Empcode in
(
    select top(((select count('x') from #Emp where Ranking is null) * (select (percentage) from #Ranking where Rank = 'O')) / 100) Empcode
    from #Emp
    where Ranking is null
    order by Score desc
);

update #Emp set Ranking = 'E'
where Empcode in
(
    select top(((select count('x') from #Emp where Ranking is null) * (select (percentage) from #Ranking where Rank = 'E')) / 100) Empcode
    from #Emp
    where Ranking is null
    order by Score desc
);

update #Emp set Ranking = 'G'
where Ranking is null;

根据您的要求,很难提出干净的更新查询。大多数时候我都试图避免使用游标;但为了适应未来不断增长的排名列表,我必须使用光标。为了使游标有效,我必须在表格的排名中添加一个列[id]:

Create Table #Ranking(id int, Rank nvarchar,Percentage int)
insert into #Ranking values(1, 'O',20) 
insert into #Ranking values(2, 'E',40)
insert into #Ranking values(3, 'G',40)

Create Table #Emp(Empcode nvarchar(3),Score numeric,Ranking nvarchar)  
insert into #Emp values('E1',97,null) 
insert into #Emp values('E2',95,null) 
insert into #Emp values('E3',87,null) 
insert into #Emp Values('E4',85,null) 
insert into #Emp Values('E5',78,null) 
insert into #Emp Values('E6',75,null) 
insert into #Emp Values('E7',68,null) 
insert into #Emp Values('E8',65,null) 
insert into #Emp Values('E9',59,null) 
insert into #Emp Values('E10',58,null)

以下是使用游标重写的更新查询:

declare Ranking_Cursor cursor for
    select Rank, percentage
    from #Ranking
    order by id asc

declare @Rank as nvarchar
declare @Percentage as int

open Ranking_Cursor
fetch next from Ranking_Cursor into @Rank, @Percentage
while @@fetch_status = 0
begin
    update #Emp set Ranking = @Rank
    where Empcode in
    (
        select top(((select count('x') from #Emp where Ranking is null) * @Percentage) / 100) Empcode
        from #Emp
        where Ranking is null
        order by Score desc
    )

    fetch next from Ranking_Cursor into @Rank, @Percentage
end
close Ranking_Cursor
deallocate Ranking_Cursor

--catch-all query for the bottom rank
update #Emp set Ranking = @Rank
where Ranking is null;

不漂亮,但如果您计划在表格排名中添加更多记录,它应该可以使用。我希望其他人可以提出更清晰的更新查询。