Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7588603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:57:49+00:00 2026-05-30T19:57:49+00:00

I am developing a web application for my company. This application provides the users

  • 0

I am developing a web application for my company. This application provides the users with quizzes. Now, I need to develop a powerful and meaningful dashboard to the management. The dashboard must show show % participation = (sum of all quizzes taken by each employee) / (total number of employees * total number of quizzes)

The question is: I have the following database design:

Employee Table: Username, Name, Job, DivisionID

Division Table: DivisionID, DivisionName

Quiz Table: QuizID, Title, Description

UserQuiz Table: UserQuizID, Score, DateTimeComplete, QuizID, Username

NOTE: The first attribute in each table is the primary key.

The SQL Query that I am using (but I am not sure about it) to show the percentage completion is:

DECLARE @LastDayOfPrevMonth DATETIME, @FirstDayOfThreeMonthsBefore DATETIME

SET @FirstDayOfThreeMonthsBefore = DATEADD(MONTH, -2, DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0))
SET @LastDayOfPrevMonth = GETDATE()

;WITH MonthCTE AS
(
SELECT DATENAME(MONTH, DATEADD(MONTH, -2, CURRENT_TIMESTAMP)) + '-' + DATENAME(YEAR, DATEADD(MONTH, -2, CURRENT_TIMESTAMP)) AS MonthYear
UNION ALL 
SELECT DATENAME(MONTH, DATEADD(MONTH, -1, CURRENT_TIMESTAMP)) + '-' + DATENAME(YEAR, DATEADD(MONTH, -1, CURRENT_TIMESTAMP))
UNION ALL
SELECT DATENAME(MONTH, DATEADD(MONTH, 0, CURRENT_TIMESTAMP)) + '-' + DATENAME(YEAR, DATEADD(MONTH, 0, CURRENT_TIMESTAMP))
)

    SELECT  Divisions.DivisionName
,       [Percentage Participation] = CAST([Total Number of Quizzes Taken]
        * 100.00 / [Total Number of Quizzes] AS DECIMAL(18, 2))
,       [Total Number of Quizzes Taken]
,       [Total Number of Quizzes]
,     [Total Number of Employees]
,       MonthYear [Month]
,     LEFT([MonthYear],3) + RIGHT([MonthYear], LEN([MonthYear]) - CHARINDEX('-',[MonthYear]) + 1) FirstThreeLettersOfMonth

FROM    dbo.Divisions Divisions CROSS JOIN 
    (SELECT ISNULL(NULLIF(COUNT(*),0),1) [Total Number of Quizzes]FROM [dbo].[Quiz] ) Quiz
    OUTER APPLY (SELECT COUNT(*) AS [Total Number of Employees] 
               FROM [dbo].[employee]
               WHERE employee.DivisionCode = Divisions.SapCode
               ) Employee 
    OUTER APPLY (
                 SELECT    ISNULL([Total Number of Quizzes Taken],0) [Total Number of Quizzes Taken],
                           MonthCTE.MonthYear FROM
                 (SELECT   COUNT(DISTINCT UserQuiz.QuizID) AS [Total Number of Quizzes Taken],
                            DATENAME(MONTH, UserQuiz.DateTimeComplete) + '-' + DATENAME(YEAR, UserQuiz.DateTimeComplete) MonthYear
                  FROM      UserQuiz UserQuiz
                            INNER JOIN employee employee 
                            ON UserQuiz.Username = employee.Username
                  WHERE     employee.DivisionCode = Divisions.SapCode 
                  AND       UserQuiz.DateTimeComplete BETWEEN @FirstDayOfThreeMonthsBefore AND @LastDayOfPrevMonth
                  GROUP BY DATENAME(MONTH, UserQuiz.DateTimeComplete), DATENAME(YEAR, UserQuiz.DateTimeComplete)
                  )Quiz
                  RIGHT JOIN MonthCTE ON Quiz.MonthYear = MonthCTE.MonthYear
                ) QuizMonthOutput

What I want now is just showing the Percent Completion for the LAST MONTH only.

I think my problem now is just with finding the total number of employees in each division to add it to this part of the above query:

SELECT  Divisions.DivisionShortcut
,       [Percent Completion] = CAST([Sum of all Quizzes Taken by each Employee]
        * 100.00 / ([Total Number of Employees]*[Total Number of Quizzes]) AS DECIMAL(18, 2))

So how to do that?

Sample of desired output:

The calculation should be: The percent completion should be calculated as mentioned above which is equal to (sum of all quizzes taken by each employee)/(total number of employees * total number of quizzes). For example, in Division A, if there are two employees Emp1 and Emp2. Each month, there are four quizzes. Emp1 took Quiz#1 and Quiz#2 and Emp2 took Quiz#4. The Percent Completion should be = ((Emp1 * 2 Quizzes) + (Emp2 * 1 Quiz)) / (total number of employees * total number of quizzes)

Percent Completion = (2 + 1) / (2*4) = 2 / 8 = 0.25

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:57:50+00:00Added an answer on May 30, 2026 at 7:57 pm

    Try this:

    DROP TABLE #Employee
    DROP TABLE #Division
    DROP TABLE #Quiz
    DROP TABLE #UserQuiz
    
    CREATE TABLE #Employee(
      Username  CHAR(10), 
      Name  VARCHAR(20),
      Job       VARCHAR(20),
      DivisionID    INT
    )
    INSERT INTO #Employee(Username, Name, Job, DivisionID) VALUES
    ('Me', 'Me', 'job1', 1),
    ('Myself', 'Myself', 'job2', 1),
    ('Andy', 'Andy', 'job3', 1),
    ('Ai', 'Ai', 'job4', 2)
    
    CREATE TABLE #Division(
      DivisionID    INT, 
      DivisionName  VARCHAR(20)
    )
    INSERT INTO #Division(DivisionID, DivisionName) VALUES
    (1, 'Div1'),
    (2, 'Div2')
    
    CREATE TABLE #Quiz(
      QuizID    INT, 
      Title VARCHAR(20), 
      Description   VARCHAR(20)
    )
    INSERT INTO #Quiz(QuizID, Title, Description) VALUES
    (1, 'Quiz1', 'Quiz1'),
    (2, 'Quiz2', 'Quiz2'),
    (3, 'Quiz3', 'Quiz3'),
    (4, 'Quiz4', 'Quiz4'),
    (5, 'Quiz5', 'Quiz5'),
    (6, 'Quiz6', 'Quiz6'),
    (7, 'Quiz7', 'Quiz7'),
    (8, 'Quiz8', 'Quiz8')
    
    CREATE TABLE #UserQuiz(
      UserQuizID    INT, 
      Score INT, 
      DateTimeComplete  DATETIME, 
      QuizID    INT, 
      Username  CHAR(10), 
    )
    INSERT INTO #UserQuiz(UserQuizID, Score, DateTimeComplete, QuizID, Username) VALUES
    (1, 10, '20000101', 1, 'Me'),
    (2, 0,  '20000101', 1, 'Myself'),
    (3, 10, '20120210', 5, 'Me'),
    (4, 10, '20120210', 6, 'Myself'),
    (5, 10, '20120210', 7, 'Andy'),
    (6, 10, '20120101', 5, 'Ai')
    
    DECLARE @LastDayOfPrevMonth DATETIME, @FirstDayOfPrevMonth DATETIME
    
    SET @FirstDayOfPrevMonth = DATEADD(dd, -DAY(DATEADD(mm, 1, GetDate()) - 1), DATEADD(mm, -1, GetDate()))
    SET @LastDayOfPrevMonth = DATEADD(dd, -DAY(DATEADD(m, 1, GetDate())), DATEADD(m, 0, GetDate()))
    
    ;WITH LastMontQuizes AS
    (SELECT distinct QuizID
    FROM #UserQuiz
    WHERE DateTimeComplete BETWEEN @FirstDayOfPrevMonth AND @LastDayOfPrevMonth
    ),
    NumberOfQuizes AS
    (SELECT COUNT(*) as NumberOfQuizes
    FROM LastMontQuizes
    ),
    NrOfQuizesPerDivision AS
    (SELECT COUNT(*) as NrOfQuizesPerDivision, #Division.DivisionID
    FROM #Division
      INNER JOIN #Employee ON
        #Division.DivisionID = #Employee.DivisionID
      INNER JOIN #UserQuiz ON
        #Employee.Username = #UserQuiz.Username
      INNER JOIN LastMontQuizes ON
        #UserQuiz.QuizID = LastMontQuizes.QuizID
    GROUP BY #Division.DivisionID
    ),
    NrOfEmployeesPerDivision AS
    (SELECT COUNT(*) as NrOfEmployeesPerDivision, #Division.DivisionID
    FROM #Division
      INNER JOIN #Employee ON
        #Division.DivisionID = #Employee.DivisionID
    GROUP BY #Division.DivisionID
    )
    SELECT #Division.DivisionName,
      NrOfQuizesPerDivision.DivisionID, 
      NrOfQuizesPerDivision.NrOfQuizesPerDivision * 100.0 / (NrOfEmployeesPerDivision.NrOfEmployeesPerDivision + NumberOfQuizes.NumberOfQuizes) AS Percentage,
      NrOfQuizesPerDivision.NrOfQuizesPerDivision,
      NrOfEmployeesPerDivision.NrOfEmployeesPerDivision,
      NumberOfQuizes.NumberOfQuizes
    FROM NrOfQuizesPerDivision
      INNER JOIN NrOfEmployeesPerDivision ON
        NrOfQuizesPerDivision.DivisionID = NrOfEmployeesPerDivision.DivisionID
      INNER JOIN #Division ON
        NrOfQuizesPerDivision.DivisionID = #Division.DivisionID
      CROSS JOIN NumberOfQuizes
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a web application for a comapny. This application provides the users
I am developing web application where users have collection of tags. I need to
I am developing an ASP.NET intranet web application that provides short quizzes to the
I'm developing a new web-based financial application for our company that provides online real-time
My company is currently developing a project management web application in PHP, and I
I'm developing a web application for a company which I work for. My team
The company I work for is thinking of developing a LAMP SaaS Web application
I'm developing a web application and I need to mix Forms & Windows authentication
I'm working at a small company and we are developing a web application. With
My company is developing a web application that builds in ant. I've been tasked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.