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

可能な回答の 1 つに参加者がいない場合でも、各質問に可能なすべての回答と各質問の合計参加者数を表示するクエリを作成しました。この結果は、部門ごとに表示されます。

私が今しなければならないことは、このクエリを変更して、既に取得した参加者の総数に加えて、各部門の従業員の総数を表示し、参加者の総数/従業員の総数である参加率を表示することです。

では、どうやってそれを行うのですか?

クエリ:

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
;

これら2つ(役に立たない)を削除し、ID命名規則に固執することで、少しクリーンアップできます。この方法では、とIDsに一意のインデックスは必要ありません。だから、それはこのように見えるかもしれませんQuestionsAnswersCompleteSurvey

ここに画像の説明を入力してください

于 2012-05-26T20:31:15.983 に答える