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 6972473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:58:20+00:00 2026-05-27T16:58:20+00:00

I am developing a training management web-based system which will show the management the

  • 0

I am developing a training management web-based system which will show the management the training record for each division. My database design is like following:

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 for this task is:

SELECT COUNT(DISTINCT dbo.UserQuiz.QuizID) AS [Total Number of Quizzes], 
  dbo.Divisions.DivisionName, 
  DATENAME(Month, dbo.UserQuiz.DateTimeComplete) AS Month 
FROM dbo.UserQuiz 
INNER JOIN dbo.Quiz 
  ON dbo.UserQuiz.QuizID = dbo.Quiz.QuizID 
INNER JOIN  dbo.employee 
  ON dbo.UserQuiz.Username = dbo.employee.Username 
RIGHT OUTER JOIN dbo.Divisions 
  ON dbo.employee.DivisionCode = dbo.Divisions.SapCode 
GROUP BY dbo.Divisions.DivisionName,
  DATENAME(Month, dbo.UserQuiz.DateTimeComplete)

This query will show me the total number of taken quizzes by each division based on month. What I want now is showing these results for the last three months. Also, I want to show all the divisions even if there is a division which does not take any quiz. This means I want to show the division with zero number of taken quizzes.

  • 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-27T16:58:21+00:00Added an answer on May 27, 2026 at 4:58 pm

    For showing ALL Divisions, that will need to be first in the list, then LEFT JOINed to the count results. You’ll need to COALESCE() the value so if null, will return the zero instead. As for the last 3 months, you’ll just need to add a WHERE clause for the date range.

    Edit… per comment. I’ve taken it one step further and for each division, I’ve created it as a Cross-Tab to show the division with 3 columns showing the counts of each respective month in a single row… So, the resulting column names would be the same, but would reflect the actual month data content… Actually adjusted the WHERE clause going back only 2 months… 2 months back plus current equals 3 months total.

     select 
              d.DivisionName,
              SUM( case when PreQuery.ByMonth = DATENAME(Month, DateAdd( month, -2, GetDate())
                        then PreQuery.DistinctQuizes else 0 end ) as TwoMonthsAgoCount,
              SUM( case when PreQuery.ByMonth = DATENAME(Month, DateAdd( month, -1, GetDate())
                        then PreQuery.DistinctQuizes else 0 end ) as OneMonthAgoCount,
              SUM( case when PreQuery.ByMonth = DATENAME(Month, GetDate())
                        then PreQuery.DistinctQuizes else 0 end ) as CurrentMonthCount
           from
              Divisions d
                 left join
                 ( select count( distinct UQ.QuizID ) DistinctQuizes,
                         DATENAME(Month, UQ.DateTimeComplete) ByMonth,
                          d2.DivisionName
                      from 
                         UserQuiz UQ
                            JOIN Quiz Q on UQ.QuizID = Q.QuizID
                            JOIN Employee E on UQ.UserName = E.UserName
                               JOIN Divisions D2 on E.DivisionCode = D2.SapCode
                      where
                        UQ.DateTimeComplete between 
                            DateAdd( month, -2, GetDate())  and GetDate() 
                      group by 
                         d2.DivisionName,
                         DATENAME(Month, UQ.DateTimeComplete)
                 ) PreQuery
                 ON d.DivisionName = PreQuery.DivisionName
       GROUP BY
          d.DivisionName
    

    By using “GetDate()”, this returns whatever the current date is on the computer. This would be the ENDING date. The first date would be basic date arithmetic… Use the DateAdd function, add an interval based on months of -2 (negative to go BACKWARDS), based on whatever the current date is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When developing a new web based application which version of html should you aim
I am developing a system in which a server will serve a video to
When developing whether its Web or Desktop at which point should a developer switch
I developing a plugin which reads from a .properties file for the database connection,
I am developing an intranet web application which should provide its users with a
Developing websites are time-consuming. To improve productivity, I would code a prototype to show
Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux.
Developing search utility to search a file entire computer system, works fine on windows
Developing a desktop application based on Java + Swing I faced the problem of
developing an application in Access. The access file will be located on a db

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.