如何提出显示测验总数,参与者总数的统计数据?

时间:2011-11-16 10:14:19

标签: asp.net sql-server

我正在开发一个简单的Web应用程序,为用户提供测验。由于我是ASP.NET世界的新手,我正在关注如何在ASP.net网站上构建测验引擎视频系列。我的数据库设计类似于这些视频中使用的设计。 这是link of the first video: 一切正常,但我现在想开发一个查询,帮助我向管理员显示统计数据,显示:

  1. 系统中的测验总数

  2. 每个测验的参与者总数

  3. 每日,每周,每月和每年(如果可能)的参与者总数

  4. 为了澄清最后一点,我希望管理员看到以下内容:

    1. 上周参与者穿戴的总人数为:......
    2. 上周采取的测验总数为:.........
    3. 上个月的测验总数为:.........
    4. 我认为这样的事情对于了解系统的使用情况非常有用,并向管理人员展示系统在copmany中的效率。

      数据库的sechma:

      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     Q.QuizID, Q.Title, COUNT(*) AS Users
                                         FROM       dbo.UserQuiz AS UQ 
                                         INNER JOIN dbo.Quiz AS Q ON Q.QuizID = UQ.QuizID
                                         GROUP BY Q.QuizID, Q.Title
      

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

1 个答案:

答案 0 :(得分:1)

如果您想计算过去7天和过去30天,可以使用以下内容:

-- Count of Participants in the last 7 days 
SELECT COUNT(DISTINCT Q.UserName) 
FROM dbo.UserQuiz Q 
WHERE DateTimeComplete >= DATEADD(dd, -7, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)) 

-- Count quizzes taken in the last 7 days 
SELECT COUNT(Q.QuizID) 
FROM dbo.UserQuiz Q 
WHERE Q.DateTimeComplete >= DATEADD(dd, -7, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)) 

-- Count quizzes taken in the last 30 days 
SELECT COUNT(Q.QuizID) 
FROM dbo.UserQuiz Q 
WHERE Q.DateTimeComplete >= DATEADD(dd, -30, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)) 

请注意,如果您想计算日历周和日历月,那么您需要做更多工作来过滤正确的开始和结束日期过滤器。


编辑:修复了查询语法(在SSMS中未运行时发布的第一个答案)我也没注意到UserQuiz.UserQuizID可以为空。因此,这一专栏不是我所期望的那样。


编辑#2:根据OP的请求,也可以使用子选择在单个查询中检索这三个统计信息。由于过滤标准或计算每个统计数据(或两者)所涉及的聚合存在差异,因此必须进行子选择。

-- Combine three stats into one query using sub-selects...
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(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