2013-07-21 5 views
0

"Worker"라는 제목의 평가 사람들을위한 기술 등급을 부여하는 프로그램이 있습니다.이 프로그램은 할당 된 파일 번호와 함께 처리해야합니다. 각 노동자의 일부입니다 I는 다음과 같이 할 수있는 데이터 세트 얻을기술 능력의 백분율 및 백분율

SELECT distinct 
o.VP, 
o.AVP, 
o.Director, 
o.Supervisor, 
o.Worker, 
bs.File_NBR, 
s.Skill 
bs.score 
FROM [New_EEs].[dbo].[SBC_Best_Scores] bs 
inner join new_ees.dbo.SBC_Skills s 
on bs.Skill_NBR=s.SKILL_NBR 
inner join gw_PPP.dbo.Org_Hierarchy oon 
bs.File_NBR=o.File_NBR; 

:.

VP AVP Director Supervisor Worker File_NBR Skill Rating 
Gerald Kris Doris NULL Mack 107812 B2 4 
Gerald Kris Doris NULL Mack 107812 D1 3 
Gerald Kris Doris NULL Mack 107812 D2 3 
Gerald Kris Doris NULL Mack 107812 D3 3 
Gerald Kris Doris NULL Mack 107812 E1 4 
Gerald Kris Mike NULL Brady 109080 A1 5 
Gerald Kris Mike NULL Brady 109080 B1 4 
Gerald Kris Mike NULL Brady 109080 B2 3 
Gerald Kris Mike NULL Brady 109080 B3 4 
Gerald Kris Mike NULL Brady 109080 C1 4 
Gerald Kris Mike NULL Brady 109080 C2 4 
Gerald Kris Mike NULL Brady 109080 C3 0 
Kim Harry NULL Grant Tom 108457 B1 4 
Kim Harry NULL Grant Tom 108457 B2 4 
Kim Harry NULL Grant Tom 108457 C1 4 
Kim Harry NULL Grant Tom 108457 C2: 5 
Kim Harry NULL Grant Tom 108457 C5 5 
Kim Harry NULL Grant Tom 108457 D1 4 
Kim Harry NULL Grant Tom 108457 D2 5 
Kim Harry NULL Grant Tom 108457 D3 4 
Kim Harry NULL Grant Jean 106934 C5 4 
Kim Harry NULL Grant Jean 106934 D1 5 
Kim Harry NULL Grant Jean 106934 D3 5 
Kim Harry NULL Grant Raphe 108901 B2 5 
Kim Harry NULL Grant Raphe 108901 C2 5 
Kim Harry NULL Grant Raphe 108901 C3 4 
Kim Harry NULL Grant Raphe 108901 C5 5 
Kim Harry NULL Grant Raphe 108901 D2 5 
Kim Harry NULL Grant Raphe 108901 E1 5 
Kim Harry NULL Grant Tyika 107923 B1 5 
Kim Harry NULL Grant Tyika 107923 B2 5 
Kim Harry NULL Grant Tyika 107923 D2 4 
Kim Harry NULL Grant Tyika 107923 D3 4 

을 평가 수준이 내가해야 할 것은을 생성 할 일은 1 (5)을 통해입니다 각 sk에 대해 근로자에게주는 각 등급의 수와 백분율을 보여주는 표 Vp, AVP, 관리자 및 감독별로 그룹화되어 있습니다. 최종적으로 AVP와 궁극적으로 감독 아래에있는 모든 워커들에 의한 모든 작품들.

Name Role Skill Count of % of Count of  % of 
           Rating 1 Rating 1 Rating 2 Rating 2 
Gerald VP A1 100 29% 130 33% 
Gerald VP B1 95 28% 95 24% 
Gerald VP B2 120 35% 70 18% 
Gerald VP B3 30 9% 100 25% 
Kim VP A1    
Kim VP B1    
Kim VP B2  and so on  
Kim VP B3    
Kris AVP A1    
Kris AVP B1    
Kris AVP B2    
Kris AVP B3    
Harry AVP A1    
Harry AVP B1    
Harry AVP B2    
Harry AVP B3    
Doris Director A1    
Doris Director B1    
Doris Director B2    
Doris Director B3    
Mike Director A1    
Mike Director B1    
Mike Director B2    
Mike Director B3    
Grant Supervisor A1    
Grant Supervisor B1    
Grant Supervisor B2    
Grant Supervisor B3    

도움이 될 것입니다. 감사!

답변

1

서로 다른 열에 서로 다른 역할이 있으므로 소형 쿼리를 얻으려면 동적 SQL 또는 복잡한 피벗이 필요합니다. 그러므로 복잡성이 네 가지 역할에 가치가 있다고 생각하지 않기 때문에 복사 및 붙여 넣기를 선택했습니다.

예를 들어 쿼리 T의 이름을 지정했습니다.

with roles as (
    select VP as Name, 'VP' as Role, Skill, Rating from t where VP is not null 
    union all 
    select AVP as Name, 'AVP' as Role, Skill, Rating from t where AVP is not null 
    union all 
    select Director as Name, 'Director' as Role, Skill, Rating from t where Director is not null 
    union all 
    select Supervisor as Name, 'Supervisor' as Role, Skill, Rating from t where Supervisor is not null 
), counts as (
    select Name, Role, Skill 
     ,count(case when rating = 1 then 1 else NULL end) as [Count of Rating 1] 
     ,count(case when rating = 2 then 1 else NULL end) as [Count of Rating 2] 
     ,count(case when rating = 3 then 1 else NULL end) as [Count of Rating 3] 
     ,count(case when rating = 4 then 1 else NULL end) as [Count of Rating 4] 
     ,count(case when rating = 5 then 1 else NULL end) as [Count of Rating 5] 
     ,count(*) as TotalCount 
    from roles 
    group by Name, Role, skill 
) 
select Name, Role, Skill 
,[Count of Rating 1] 
,CONVERT(varchar(10), convert(int,100.0 * [Count of Rating 1]/NULLIF(TotalCount, 0))) + '%' as [% of Rating 1] 
,[Count of Rating 2] 
,CONVERT(varchar(10), convert(int,100.0 * [Count of Rating 2]/NULLIF(TotalCount, 0))) + '%' as [% of Rating 2] 
,[Count of Rating 3] 
,CONVERT(varchar(10), convert(int,100.0 * [Count of Rating 3]/NULLIF(TotalCount, 0))) + '%' as [% of Rating 3] 
,[Count of Rating 4] 
,CONVERT(varchar(10), convert(int,100.0 * [Count of Rating 4]/NULLIF(TotalCount, 0))) + '%' as [% of Rating 4] 
,[Count of Rating 5] 
,CONVERT(varchar(10), convert(int,100.0 * [Count of Rating 5]/NULLIF(TotalCount, 0))) + '%' as [% of Rating 5] 
from counts 
order by Name, skill 

여기서 내가 한 것은 모든 역할을 함께 결합하여 역할 이름을 하드 코딩하는 것입니다. roles은 VP를 가진 모든 사람이 VP를 가진 행을 가져 오도록 AVP를 가진 모든 사람이 해당 AVP를 가진 행을 가져 오도록 테이블을 재구성합니다 .... counts은 각 이름, 역할 및 스킬에 대한 모든 작업자를 계산합니다. 마지막 선택은 백분율을 계산합니다.

실제 행동을 보여주는 피들입니다. http://sqlfiddle.com/#!3/fe09d/15