2010-12-03 84 views
0

僱員總數更新排名,40%SQL查詢基於所述百分比分配

(例如:

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在排名表中可用的百分比allcoated,E4,E5員工在等級「E」下降 E6 ,E7,E8,E9,E10的級別'G'

+0

不知道你在問什麼。另外,您可以編輯帖子,並使用代碼格式按鈕格式化代碼。謝謝。 – 2010-12-03 10:54:34

+0

由於第二次計算佔剩餘80%的40%,因此這不會將員工分爲20%,40%和40%。純粹的20/40/40拆分顯然會將員工分成2/4/4組。 – 2010-12-03 11:25:36

+0

你的意思是我直接將前兩名員工的排名更新爲'O' – Girija 2010-12-03 12:03:41

回答

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 
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; 

不漂亮,但它如果您打算將更多記錄添加到表格中,應該可以工作。我希望別人能拿出更清晰的更新查詢。