如何在部门级别每月显示%参与率并比较每个部门?

时间:2012-01-15 08:09:37

标签: sql sql-server-2008-r2

我正在为comapny开发一个Web应用程序。此应用程序为用户提供测验。现在,我需要为管理层开发一个功能强大且有意义的仪表板。仪表板必须显示许多统计信息。他们希望我以这样的方式设计此仪表板:show%participation =完成的测验数除以部门级月度测验的总数,并比较每个部门。

问题是:我有以下数据库设计:

员工表:用户名,姓名,职位,分部ID

分部表:DivisionID,DivisionName

测验表:QuizID,标题,描述

UserQuiz表:UserQuizID,得分,DateTimeComplete,QuizID,用户名

注意:每个表中的第一个属性是主键。

我试图用来显示过去三个月结果的SQL查询是:

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)

此外,我尝试了很多查询,每次都有不同于我想要的东西。坦率地说,我对如何达到上述要求一无所知。

编辑: 这是包含一些数据的模式:

    /****** Object:  Table [dbo].[Divisions]    Script Date: 01/15/2012 12:29:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Divisions](
    [SapCode] [float] NOT NULL,
    [DivisionShortcut] [varchar](10) NOT NULL,
    [DivisionName] [varchar](max) NOT NULL,
 CONSTRAINT [PK_Divisions] PRIMARY KEY CLUSTERED 
(
    [SapCode] 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
INSERT [dbo].[Divisions] ([SapCode], [DivisionShortcut], [DivisionName]) VALUES (30003143, N'PMOD', N'AB')
INSERT [dbo].[Divisions] ([SapCode], [DivisionShortcut], [DivisionName]) VALUES (30003144, N'ESD', N'BC')
INSERT [dbo].[Divisions] ([SapCode], [DivisionShortcut], [DivisionName]) VALUES (30003153, N'PESD', N'CD')
INSERT [dbo].[Divisions] ([SapCode], [DivisionShortcut], [DivisionName]) VALUES (30003158, N'SSD', N'DE')
INSERT [dbo].[Divisions] ([SapCode], [DivisionShortcut], [DivisionName]) VALUES (30012601, N'PEOD', N'EF')
INSERT [dbo].[Divisions] ([SapCode], [DivisionShortcut], [DivisionName]) VALUES (30021812, N'PEMD', N'FG')
INSERT [dbo].[Divisions] ([SapCode], [DivisionShortcut], [DivisionName]) VALUES (30021876, N'BAG', N'GH
')
INSERT [dbo].[Divisions] ([SapCode], [DivisionShortcut], [DivisionName]) VALUES (30023176, N'EPM', N'HI')
/****** Object:  Table [dbo].[Quiz]    Script Date: 01/15/2012 12:29:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Quiz](
    [QuizID] [int] IDENTITY(1,1) NOT NULL,
    [Title] [varchar](max) NOT NULL,
    [IsSent] [bit] 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], [IsSent], [Description]) VALUES (11, N'Safety Quiz 1', 0, N'General Safety Quiz')
INSERT [dbo].[Quiz] ([QuizID], [Title], [IsSent], [Description]) VALUES (12, N'Safety Quiz 2', 0, N'General Safety Quiz')
INSERT [dbo].[Quiz] ([QuizID], [Title], [IsSent], [Description]) VALUES (13, N'Safety Quiz 3', 0, N'TEST')
INSERT [dbo].[Quiz] ([QuizID], [Title], [IsSent], [Description]) VALUES (14, N'Safety Quiz 4', 0, N'TEST')
SET IDENTITY_INSERT [dbo].[Quiz] OFF
/****** Object:  Table [dbo].[employee]    Script Date: 01/15/2012 12:29:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[employee](
    [Name] [nvarchar](max) NOT NULL,
    [Username] [nvarchar](255) NOT NULL,
    [JobTitle] [nvarchar](max) NOT NULL,
    [BadgeNo] [float] NOT NULL,
    [EmpOrgType] [float] NOT NULL,
    [DivisionCode] [float] NOT NULL,
 CONSTRAINT [PK_employee] PRIMARY KEY CLUSTERED 
(
    [Username] 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
INSERT [dbo].[employee] ([Name], [Username], [JobTitle], [BadgeNo], [EmpOrgType], [DivisionCode]) VALUES (N'John', N'AGUILEBS', N'Engineering Technician', 9545246, 2, 30012601)
INSERT [dbo].[employee] ([Name], [Username], [JobTitle], [BadgeNo], [EmpOrgType], [DivisionCode]) VALUES (N'TED', N'ALKHATHI', N'Technical Clk Engrg', 8016951, 2, 30012601)
INSERT [dbo].[employee] ([Name], [Username], [JobTitle], [BadgeNo], [EmpOrgType], [DivisionCode]) VALUES (N'Bel', N'ALMARHMS', N'Business Sys Analyst Iv', 289589, 1, 30012601)
INSERT [dbo].[employee] ([Name], [Username], [JobTitle], [BadgeNo], [EmpOrgType], [DivisionCode]) VALUES (N'Tony', N'GRIGFW0A', N'Business Sys Analyst I', 9395990, 2, 30012601)
INSERT [dbo].[employee] ([Name], [Username], [JobTitle], [BadgeNo], [EmpOrgType], [DivisionCode]) VALUES (N'Maria', N'KHWAILAM', N'Asst Engineer Ii', 431177, 2, 30012601)
INSERT [dbo].[employee] ([Name], [Username], [JobTitle], [BadgeNo], [EmpOrgType], [DivisionCode]) VALUES (N'Johny', N'SALEMS0M', N'Business Sys Analyst Iii', 431163, 2, 30003143)
INSERT [dbo].[employee] ([Name], [Username], [JobTitle], [BadgeNo], [EmpOrgType], [DivisionCode]) VALUES (N'Teddy', N'TOWAAH0A', N'Business Sys Analyst Iv', 8819001, 2, 30003143)
INSERT [dbo].[employee] ([Name], [Username], [JobTitle], [BadgeNo], [EmpOrgType], [DivisionCode]) VALUES (N'Arnold', N'VILLAV0A', N'Asst Engineer I', 329398, 1, 30023176)
/****** Object:  Table [dbo].[UserQuiz]    Script Date: 01/15/2012 12:29:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserQuiz](
    [UserQuizID] [int] IDENTITY(1,1) NOT NULL,
    [QuizID] [int] NOT NULL,
    [DateTimeComplete] [smalldatetime] NOT NULL,
    [Score] [float] NOT NULL,
    [Username] [nvarchar](255) NOT NULL,
 CONSTRAINT [PK_UserQuiz] PRIMARY KEY CLUSTERED 
(
    [UserQuizID] 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 IDENTITY_INSERT [dbo].[UserQuiz] ON
INSERT [dbo].[UserQuiz] ([UserQuizID], [QuizID], [DateTimeComplete], [Score], [Username]) VALUES (21, 11, CAST(0x9FCD0345 AS SmallDateTime), 0, N'ALMARHMS')
INSERT [dbo].[UserQuiz] ([UserQuizID], [QuizID], [DateTimeComplete], [Score], [Username]) VALUES (81, 11, CAST(0x9FD50288 AS SmallDateTime), 0, N'ALMARHMS')
INSERT [dbo].[UserQuiz] ([UserQuizID], [QuizID], [DateTimeComplete], [Score], [Username]) VALUES (82, 11, CAST(0x9FC80000 AS SmallDateTime), 100, N'TOWAAH0A')
INSERT [dbo].[UserQuiz] ([UserQuizID], [QuizID], [DateTimeComplete], [Score], [Username]) VALUES (83, 12, CAST(0x9FCE0000 AS SmallDateTime), 100, N'ALMARHMS')
INSERT [dbo].[UserQuiz] ([UserQuizID], [QuizID], [DateTimeComplete], [Score], [Username]) VALUES (84, 11, CAST(0x9E790000 AS SmallDateTime), 50, N'VILLAV0A')
INSERT [dbo].[UserQuiz] ([UserQuizID], [QuizID], [DateTimeComplete], [Score], [Username]) VALUES (85, 12, CAST(0x9FC20000 AS SmallDateTime), 100, N'ALMARHMS')
SET IDENTITY_INSERT [dbo].[UserQuiz] OFF
/****** Object:  Default [DF_Quiz_IsSent]    Script Date: 01/15/2012 12:29:48 ******/
ALTER TABLE [dbo].[Quiz] ADD  CONSTRAINT [DF_Quiz_IsSent]  DEFAULT ((0)) FOR [IsSent]
GO
/****** Object:  ForeignKey [FK_employee_Divisions]    Script Date: 01/15/2012 12:29:48 ******/
ALTER TABLE [dbo].[employee]  WITH CHECK ADD  CONSTRAINT [FK_employee_Divisions] FOREIGN KEY([DivisionCode])
REFERENCES [dbo].[Divisions] ([SapCode])
GO
ALTER TABLE [dbo].[employee] CHECK CONSTRAINT [FK_employee_Divisions]
GO
/****** Object:  ForeignKey [FK_UserQuiz_employee]    Script Date: 01/15/2012 12:29:48 ******/
ALTER TABLE [dbo].[UserQuiz]  WITH CHECK ADD  CONSTRAINT [FK_UserQuiz_employee] FOREIGN KEY([Username])
REFERENCES [dbo].[employee] ([Username])
GO
ALTER TABLE [dbo].[UserQuiz] CHECK CONSTRAINT [FK_UserQuiz_employee]
GO
/****** Object:  ForeignKey [FK_UserQuiz_Quiz]    Script Date: 01/15/2012 12:29:48 ******/
ALTER TABLE [dbo].[UserQuiz]  WITH CHECK ADD  CONSTRAINT [FK_UserQuiz_Quiz] FOREIGN KEY([QuizID])
REFERENCES [dbo].[Quiz] ([QuizID])
GO
ALTER TABLE [dbo].[UserQuiz] CHECK CONSTRAINT [FK_UserQuiz_Quiz]
GO

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT 
    COUNT(DISTINCT dbo.employee.UserName) UserCount,
    (SELECT COUNT(*) FROM dbo.Quiz) AS [Total Number of Quizzes],  
    (SELECT COUNT(*) FROM dbo.Quiz)*COUNT(DISTINCT dbo.employee.UserName) AS [Total Number of Quizzes for All users],  
    dbo.Divisions.DivisionName,
    DATENAME(Month, dbo.UserQuiz.DateTimeComplete) AS MONTH,
    COUNT(DISTINCT dbo.UserQuiz.QuizId)*COUNT(DISTINCT dbo.employee.UserName) AS [Completed This Month],
    COUNT(DISTINCT dbo.UserQuiz.QuizId)*100/(SELECT COUNT(*) FROM dbo.Quiz) AS [Percent Completion]
FROM dbo.employee
JOIN  dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode
LEFT JOIN dbo.UserQuiz ON dbo.UserQuiz.Username = dbo.employee.Username 
WHERE dbo.UserQuiz.DateTimeComplete IS NOT NULL
GROUP BY 
    dbo.Divisions.DivisionName, 
    DATENAME(Month, dbo.UserQuiz.DateTimeComplete)

只是上个月每个部门的完成百分比?

SELECT 
    dbo.Divisions.DivisionName,
    DATENAME(Month, dbo.UserQuiz.DateTimeComplete) AS MONTH,
    COUNT(DISTINCT dbo.UserQuiz.QuizId)*COUNT(DISTINCT dbo.employee.UserName)*100/((SELECT COUNT(*) FROM dbo.Quiz)*COUNT(DISTINCT dbo.employee.UserName)) AS [Percent Completion]
FROM dbo.employee
JOIN  dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode
LEFT JOIN dbo.UserQuiz ON dbo.UserQuiz.Username = dbo.employee.Username 
WHERE 
    dbo.UserQuiz.DateTimeComplete IS NOT NULL
AND DATEPART(MONTH, dbo.UserQuiz.DateTimeComplete) = 12
GROUP BY 
    dbo.Divisions.DivisionName, 
    DATENAME(Month, dbo.UserQuiz.DateTimeComplete)