0

我有一个调查,我需要为其设计一个数据库来存储其结果并使用一些查询检索结果。

我目前的数据库设计如下:

Respondents Table: RespondentID, Name, OrgCode
Department Table: OrgCode, DepartmentName
Category Table: CategoryID, CategoryName
SubCategory Table: SubCategoryID, SubCategoryName, CategoryID
Questions Table: QuestionID, QuestionDesc, CategoryID, SubCategoryID
SubQuestions Table: SubQuestionID, SubQuestionDesc, QuestionID
Answers Table: AnswerID, AnswerDesc, QuestionID, SubQuestionID
CompleteSurvey Table: ID, RespondentID, CategoryID, AnswerID

本次调查的一个例子:

Category I
    SubCategory A
    Question 1: what do you think about the following service
        SubQuestion 1: Service #1 
            (Strongly Agree, Agree, Disagree, Strongly Disagree)
        SubQuestion 2: Service #2 
            (Strongly Agree, Agree, Disagree, Strongly Disagree)

让我们假设有 3 名员工回答了这个问题

现在,我想编写一个查询,显示在每个问题或子问题中说“非常同意”、“同意”、“不同意”和“非常同意”的受访者数量。这种数据库设计是否可行?如果是的话,你能帮我解决这个问题吗?

另外,您认为还有其他设计比上述设计简单得多吗?如果是的话,你能推荐我其中一个吗?

数据库设计:

/****** Object:  Table [dbo].[Departments]    Script Date: 05/20/2012 07:26:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Departments](
    [OrgCode] [float] NOT NULL,
    [DepartmentName] [nvarchar](max) NOT NULL,
 CONSTRAINT [PK_Departments] PRIMARY KEY CLUSTERED 
(
    [OrgCode] 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
/****** Object:  Table [dbo].[Category]    Script Date: 05/20/2012 07:26:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Category](
    [CategoryID] [int] IDENTITY(1,1) NOT NULL,
    [CategoryName] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Category] PRIMARY KEY CLUSTERED 
(
    [CategoryID] 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
/****** Object:  Table [dbo].[SubCategory]    Script Date: 05/20/2012 07:26:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SubCategory](
    [SubCategoryID] [int] IDENTITY(1,1) NOT NULL,
    [SubCategoryName] [nvarchar](50) NOT NULL,
    [CategoryID] [int] NOT NULL,
 CONSTRAINT [PK_SubCategory] PRIMARY KEY CLUSTERED 
(
    [SubCategoryID] 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
/****** Object:  Table [dbo].[Respondents]    Script Date: 05/20/2012 07:26:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Respondents](
    [RespondentID] [nvarchar](50) NOT NULL,
    [Name] [varchar](50) NULL,
    [OrgCode] [float] NOT NULL,
 CONSTRAINT [PK_Respondents] PRIMARY KEY CLUSTERED 
(
    [RespondentID] 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
/****** Object:  Table [dbo].[Questions]    Script Date: 05/20/2012 07:26:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Questions](
    [QuestionID] [int] IDENTITY(1,1) NOT NULL,
    [QuestionDesc] [nvarchar](max) NOT NULL,
    [CategoryID] [int] NULL,
    [SubCategoryID] [int] NULL,
 CONSTRAINT [PK_Questions] 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
/****** Object:  Table [dbo].[CompleteSurvey]    Script Date: 05/20/2012 07:26:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[CompleteSurvey](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [RespondentID] [nvarchar](50) NOT NULL,
    [CategoryID] [int] NOT NULL,
    [AnswerID] [int] NOT NULL,
 CONSTRAINT [PK_CompleteSurvey] PRIMARY KEY CLUSTERED 
(
    [ID] 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
/****** Object:  Table [dbo].[SubQuestions]    Script Date: 05/20/2012 07:26:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SubQuestions](
    [SubQuestionID] [int] IDENTITY(1,1) NOT NULL,
    [SubQuestionDesc] [nvarchar](max) NOT NULL,
    [QuestionID] [int] NULL,
 CONSTRAINT [PK_SubQuestions] PRIMARY KEY CLUSTERED 
(
    [SubQuestionID] 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
/****** Object:  Table [dbo].[Answers]    Script Date: 05/20/2012 07:26:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Answers](
    [AnswerID] [int] IDENTITY(1,1) NOT NULL,
    [AnswerDesc] [nvarchar](max) NOT NULL,
    [QuestionID] [int] NULL,
    [SubQuestionID] [int] NULL,
 CONSTRAINT [PK_Answers] PRIMARY KEY CLUSTERED 
(
    [AnswerID] 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
/****** Object:  ForeignKey [FK_SubCategory_Category]    Script Date: 05/20/2012 07:26:11 ******/
ALTER TABLE [dbo].[SubCategory]  WITH CHECK ADD  CONSTRAINT [FK_SubCategory_Category] FOREIGN KEY([CategoryID])
REFERENCES [dbo].[Category] ([CategoryID])
GO
ALTER TABLE [dbo].[SubCategory] CHECK CONSTRAINT [FK_SubCategory_Category]
GO
/****** Object:  ForeignKey [FK_Respondents_Departments]    Script Date: 05/20/2012 07:26:11 ******/
ALTER TABLE [dbo].[Respondents]  WITH CHECK ADD  CONSTRAINT [FK_Respondents_Departments] FOREIGN KEY([OrgCode])
REFERENCES [dbo].[Departments] ([OrgCode])
GO
ALTER TABLE [dbo].[Respondents] CHECK CONSTRAINT [FK_Respondents_Departments]
GO
/****** Object:  ForeignKey [FK_Questions_Category]    Script Date: 05/20/2012 07:26:11 ******/
ALTER TABLE [dbo].[Questions]  WITH CHECK ADD  CONSTRAINT [FK_Questions_Category] FOREIGN KEY([CategoryID])
REFERENCES [dbo].[Category] ([CategoryID])
GO
ALTER TABLE [dbo].[Questions] CHECK CONSTRAINT [FK_Questions_Category]
GO
/****** Object:  ForeignKey [FK_Questions_SubCategory]    Script Date: 05/20/2012 07:26:11 ******/
ALTER TABLE [dbo].[Questions]  WITH CHECK ADD  CONSTRAINT [FK_Questions_SubCategory] FOREIGN KEY([SubCategoryID])
REFERENCES [dbo].[SubCategory] ([SubCategoryID])
GO
ALTER TABLE [dbo].[Questions] CHECK CONSTRAINT [FK_Questions_SubCategory]
GO
/****** Object:  ForeignKey [FK_CompleteSurvey_Respondents]    Script Date: 05/20/2012 07:26:11 ******/
ALTER TABLE [dbo].[CompleteSurvey]  WITH CHECK ADD  CONSTRAINT [FK_CompleteSurvey_Respondents] FOREIGN KEY([RespondentID])
REFERENCES [dbo].[Respondents] ([RespondentID])
GO
ALTER TABLE [dbo].[CompleteSurvey] CHECK CONSTRAINT [FK_CompleteSurvey_Respondents]
GO
/****** Object:  ForeignKey [FK_SubQuestions_Questions]    Script Date: 05/20/2012 07:26:11 ******/
ALTER TABLE [dbo].[SubQuestions]  WITH CHECK ADD  CONSTRAINT [FK_SubQuestions_Questions] FOREIGN KEY([QuestionID])
REFERENCES [dbo].[Questions] ([QuestionID])
GO
ALTER TABLE [dbo].[SubQuestions] CHECK CONSTRAINT [FK_SubQuestions_Questions]
GO
/****** Object:  ForeignKey [FK_Answers_Questions]    Script Date: 05/20/2012 07:26:11 ******/
ALTER TABLE [dbo].[Answers]  WITH CHECK ADD  CONSTRAINT [FK_Answers_Questions] FOREIGN KEY([QuestionID])
REFERENCES [dbo].[Questions] ([QuestionID])
GO
ALTER TABLE [dbo].[Answers] CHECK CONSTRAINT [FK_Answers_Questions]
GO
/****** Object:  ForeignKey [FK_Answers_SubQuestions]    Script Date: 05/20/2012 07:26:11 ******/
ALTER TABLE [dbo].[Answers]  WITH CHECK ADD  CONSTRAINT [FK_Answers_SubQuestions] FOREIGN KEY([SubQuestionID])
REFERENCES [dbo].[SubQuestions] ([SubQuestionID])
GO
ALTER TABLE [dbo].[Answers] CHECK CONSTRAINT [FK_Answers_SubQuestions]
GO
4

1 回答 1

1

人们通常有两种方式来执行您想要执行的查询类型。问题将非常简单 - 只需使用 COUNT 聚合函数,但您可能想要报告从未选择过的答案。聚合函数和外连接不能很好地结合在一起。

获得您正在寻找的答案的第一种方法是使用Correlated Sub-Queries

第二种方法是使用公用表表达式(或 CTE)。

相关子查询更容易理解,但效率可能低于 CTE。CTE 需要更长的时间来学习,但它们可以解决普通旧 SQL 无法解决的问题。

于 2012-05-20T11:50:37.087 回答