如何显示此测验引擎的每个月和每年的参与者总数和测验?

时间:2011-11-19 11:47:43

标签: c# asp.net sql-server sql-server-2008-r2

我正在开发一个简单的Web应用程序,为用户提供测验。由于我是ASP.NET世界的新手,我正在关注如何在ASP.net网站上构建测验引擎视频系列。我的数据库设计类似于这些视频中使用的设计。 这是link of the first video:  http://www.asp.net/general/videos/lesson-11-building-a-quiz-engine-1 [^]

我的一切正常,但我现在想开发一个查询,帮助我向管理员显示统计信息,显示:

  1. 每个月的参与者总数和参加测验
  2. 每年参与者总数和参加测验
  3. 系统中的测验总数
  4. 为了澄清最后一点,我希望管理员看到以下内容:

    参加者数量#Taken Quizzes

    11月15日6  10月10日9日

    另一个例子:

    年度参加者数量#Taken Quizzes

    2011 150 89

    我认为这样的事情对于了解系统的使用情况非常有用,并向管理人员展示系统在copmany中的效率。

    数据库的架构:

    CREATE TABLE [dbo].[Quiz](
            [QuizID] [int] IDENTITY(1,1) NOT NULL,
            [Title] [varchar](max) NOT NULL,
            [Description] [varchar](max) NULL,
         CONSTRAINT [PK_Quiz] PRIMARY KEY CLUSTERED 
        (
            [QuizID] ASC
        )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
        ) ON [PRIMARY]
        GO
        SET ANSI_PADDING OFF
        GO
        SET IDENTITY_INSERT [dbo].[Quiz] ON
        INSERT [dbo].[Quiz] ([QuizID], [Title], [Description]) VALUES (6, N'Safety', N'General Safety Test')
        INSERT [dbo].[Quiz] ([QuizID], [Title], [Description]) VALUES (7, N'my title', N'my description')
        INSERT [dbo].[Quiz] ([QuizID], [Title], [Description]) VALUES (9, N'General Safety Quiz2', N'Testing')
        INSERT [dbo].[Quiz] ([QuizID], [Title], [Description]) VALUES (10, N'General Safety Quiz3', N'Testing #2')
        SET IDENTITY_INSERT [dbo].[Quiz] OFF
    
    
        /****** Object:  Table [dbo].[Question]    Script Date: 11/17/2011 00:44:38 ******/
    
        CREATE TABLE [dbo].[Question](
            [QuestionID] [int] IDENTITY(1,1) NOT NULL,
            [Question] [varchar](max) NOT NULL,
            [Answer1] [varchar](max) NOT NULL,
            [Answer2] [varchar](max) NOT NULL,
            [Answer3] [varchar](max) NOT NULL,
            [Answer4] [varchar](max) NOT NULL,
            [CorrectAnswer] [tinyint] NOT NULL,
            [AnswerExplanation] [varchar](max) NULL,
            [QuestionOrder] [tinyint] NOT NULL,
            [QuizID] [int] NOT NULL,
         CONSTRAINT [PK_Question] PRIMARY KEY CLUSTERED 
        (
            [QuestionID] ASC
        )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
        ) ON [PRIMARY]
        GO
        SET ANSI_PADDING OFF
        GO
        SET IDENTITY_INSERT [dbo].[Question] ON
        INSERT [dbo].[Question] ([QuestionID], [Question], [Answer1], [Answer2], [Answer3], [Answer4], [CorrectAnswer], [AnswerExplanation], [QuestionOrder], [QuizID]) VALUES (4, N'What is your name?', N'Mohammed ', N'Ali', N'Hassan', N'Husain', 1, N'My Name', 1, 6)
        INSERT [dbo].[Question] ([QuestionID], [Question], [Answer1], [Answer2], [Answer3], [Answer4], [CorrectAnswer], [AnswerExplanation], [QuestionOrder], [QuizID]) VALUES (7, N'What is the definition of Safety?', N'Being Safe', N'Being in danger', N'Be careful', N'be careless', 1, N'Nothing', 1, 9)
        INSERT [dbo].[Question] ([QuestionID], [Question], [Answer1], [Answer2], [Answer3], [Answer4], [CorrectAnswer], [AnswerExplanation], [QuestionOrder], [QuizID]) VALUES (8, N'What is the definition of Safety? ', N'Being Safe', N'Being Careless', N'Being Careful', N'Being in Dangerous', 1, N'Nothing to say', 1, 10)
        SET IDENTITY_INSERT [dbo].[Question] OFF
        /****** Object:  Table [dbo].[UserQuiz]    Script Date: 11/17/2011 00:44:38 ******/
        CREATE TABLE [dbo].[UserQuiz](
            [UserQuizID] [int] NULL,
            [QuizID] [int] NOT NULL,
            [DateTimeComplete] [smalldatetime] NOT NULL,
            [Score] [tinyint] NOT NULL,
            [Username] [nvarchar](256) NOT NULL
        ) ON [PRIMARY]
        GO
        INSERT [dbo].[UserQuiz] ([UserQuizID], [QuizID], [DateTimeComplete], [Score], [Username]) VALUES (NULL, 6, CAST(0x9F8F02D8 AS SmallDateTime), 100, N'SMP\ALMARHMS')
        INSERT [dbo].[UserQuiz] ([UserQuizID], [QuizID], [DateTimeComplete], [Score], [Username]) VALUES (NULL, 6, CAST(0x9F8F02E3 AS SmallDateTime), 50, N'SMP\ALMARHMS')
        INSERT [dbo].[UserQuiz] ([UserQuizID], [QuizID], [DateTimeComplete], [Score], [Username]) VALUES (NULL, 6, CAST(0x9F8F0333 AS SmallDateTime), 50, N'SMP\ALMARHMS')
        INSERT [dbo].[UserQuiz] ([UserQuizID], [QuizID], [DateTimeComplete], [Score], [Username]) VALUES (NULL, 7, CAST(0x9F8F0335 AS SmallDateTime), 100, N'SMP\ALMARHMS')
    

    在这个伟大社区的其中一个人的帮助下,我想出了一个查询,显示每个测验中的参与者数量。这是查询:

    SELECT
    (SELECT COUNT(DISTINCT Q.UserName)  
     FROM dbo.UserQuiz Q  
     WHERE DateTimeComplete >= DATEADD(dd, -7, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)))
     as ParticipantsLast7Days,
    (SELECT COUNT(DISTINCT Q.UserName)  
     FROM dbo.UserQuiz Q  
     WHERE DateTimeComplete >= DATEADD(dd, -30, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)))
     as ParticipantsLast30Days,
    (SELECT COUNT(Q.QuizID)  
     FROM dbo.UserQuiz Q  
     WHERE Q.DateTimeComplete >= DATEADD(dd, -7, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)))
     as QuizzesLast7Days,
    (SELECT COUNT(Q.QuizID)  
     FROM dbo.UserQuiz Q  
     WHERE Q.DateTimeComplete >= DATEADD(dd, -30, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)))
     as QuizzesLast30Days
    

    现在我需要对其进行修改或提出新的查询,以便给出以上两点。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

参加者数量#Taken Quizzes

select datename(month, datetimecomplete)+' '+ cast(count(distinct username) as varchar) +' '+ cast(count(distinct quizid) as varchar) Quizzes from userquiz group by datename(month, datetimecomplete)

年度参与者数量#Taken Quizzes

select datename(year, datetimecomplete)+' '+ cast(count(distinct username) as varchar) +' '+ cast(count(distinct quizid) as varchar) Quizzes from userquiz group by datename(year, datetimecomplete)