2

I have the following database design:

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

I wrote a query that shows each question with all possible answers and the total number of participants in each question even if there is one of the possible answers without any participant. This results will be shown in each department.

What I have to do now is to modify this query to show the total number of employees in each department besides the total number of participants that I already got, then show the participation percentage which is the total number of participants/ total number of employees.

So how to do that?

The query:

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
4

1 回答 1

3

这就是我认为你现在拥有的

在此处输入图像描述

所以,试试这个

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

您可以通过删除这两个(无用的)并坚持 ID 命名约定来进行一些清理IDs——这样您就不需要QuestionsAnswersCompleteSurvey. 所以,它可能看起来像这样

在此处输入图像描述

于 2012-05-26T20:31:15.983 回答