2012-05-26 111 views
2

我有以下的數據庫設計:如何顯示此數據庫設計中的員工總數?

Employee Table: EmployeeID, Name, OrgCode 
Department Table: OrgCode, DepartName 
CompleteSurvey Table: ID, RespondantID, QuestionsAnswersID 
Questions Table: QuestionID, Question 
Answers Table: AnswerID, Answer 
QuestionsAnswers Table: ID, QuestionID, AnswerID 

我寫了一個查詢,顯示了所有可能的答案和參與者的每一個問題總數每個問題即使沒有任何參與者可能的答案之一。這個結果將顯示在每個部門。

我現在要做的就是修改這個查詢來顯示除了我已經有的參與者總數之外的每個部門的僱員總數,然後顯示出參與者總數的百分比/總數在職員工人數。

那麼該怎麼做?

查詢:

SELECT  
    TOP (100) PERCENT 
    COUNT(DISTINCT CompleteSurvey.RespondantID) AS [Total Number of Participants], 
    dbo.Answers.Answer, dbo.Questions.Question, 
    dbo.Departments.DepartmentName 
FROM 
    dbo.Questions 
INNER JOIN 
    dbo.QuestionsAnswers ON dbo.Questions.QuestionID = dbo.QuestionsAnswers.QuestionID 
INNER JOIN 
    dbo.Answers ON dbo.QuestionsAnswers.AnswerID = dbo.Answers.AnswerID 
CROSS JOIN 
    dbo.Departments 
LEFT OUTER JOIN 
    (SELECT  
     dbo.Employees.OrgCode, CompleteSurvey_1.QuestionsAnswersID, 
     CompleteSurvey_1.RespondantID 
    FROM   
     dbo.CompleteSurvey AS CompleteSurvey_1 
    INNER JOIN 
     dbo.Employees ON dbo.Employees.EmployeeID = CompleteSurvey_1.RespondantID) AS CompleteSurvey ON dbo.QuestionsAnswers.ID = CompleteSurvey.QuestionsAnswersID AND dbo.Departments.OrgCode = CompleteSurvey.OrgCode 
GROUP BY 
    dbo.Answers.Answer, dbo.Questions.Question, dbo.Departments.DepartmentName 
ORDER BY 
    dbo.Questions.Question, dbo.Answers.Answer, dbo.Departments.DepartmentName 

回答

3

這是我想你現在

enter image description here

所以,試試這個

;with 
q_00 as (-- all possible QA combinations 
    select 
      x.ID  as QA_ID 
     , q.Question 
     , a.Answer 
    from QuestionsAnswers as x 
    join Questions   as q on q.QuestionID = x.QuestionID 
    join Answers    as a on a.AnswerID = x.AnswerID 
), 
q_01 as (-- QA chosen by some employees 
    select 
      s.QuestionAnswersID as QA_ID 
     , e.EmployeeID 
     , d.DepartmentName 
    from CompleteSurvey as s 
    join Employee  as e on e.EmployeeID = s.RespondantID 
    join Department  as d on d.OrgCode = e.OrgCode 
), 
q_02 as (-- participants for each QA for each department 
    select 
      Question 
     , Answer 
     , DepartmentName 
     , count (distinct EmployeeID) as Participants 
    from  q_00 as a 
    left join q_01 as b on b.QA_ID = a.QA_ID 
    group by Question, Answer, DepartmentName 
), 
q_03 as (-- number of people in a department 
    select 
      DepartmentName 
     , count(1) as PeopleInDepartment 
    from Department as d 
    join Employee as e on e.OrgCode = d.OrgCode 
    group by DepartmentName 
) 
select 
     Question 
    , Answer 
    , a.DepartmentName 
    , Participants 
    , PeopleInDepartment 
    , cast(
      cast(Participants  as decimal(9,2)) 
     /cast(PeopleInDepartment as decimal(9,2)) 
      * 100.0 
     ) as ParticipationPercent 
from q_02 as a 
join q_03 as b on b.DepartmentName = a.DepartmentName 
order by Question, Answer, a.DepartmentName 
; 

你可以清理一個通過刪除這兩個(無用的)IDs並堅持使用ID命名約定 - 這種方式在QuestionsAnswersCompleteSurvey上不需要唯一索引。所以,它可能看起來像這樣

enter image description here