我无法让我的查询正常工作。你能帮忙吗?

时间:2013-08-02 13:36:26

标签: sql sql-server sql-server-2008

我几周来一直在努力解决这个问题,进行各种数据库更改以帮助使查询正常工作。

这是我的持续调查任务。

最后一项任务是显示任何个人确定的结果。

查询是列出每个多项选择题,然后是用户的选择和正确的答案,具体取决于问题是否是复选框问题,而不是答案作为正确答案。

然后,下一行是计算回答的总问题数,用户得出的答案总数和正确答案的百分比。

以下是总体布局:

Question                       Your Choice                    Correct Answer

Total Questions:_________    You got _X out of _X number    %Correct:____x%

由于复杂性,我已将sql查询分解为3.每个查询的第一个查询列表。第二个列出用户选择,第三个列出正确的答案。

全部按SurveyId和用户名

过滤

到目前为止,这很好。

我最困难的领域是计算总问题,有多少用户从总问题和百分比中获得。

以下是我的查询。我应该在今天下午给出这个调查的演示。

我希望你们慷慨的SQL天才可以帮助我。

SELECT Q.QUESTION, 
       Sum(CASE 
             WHEN A.CHOICEID IS NOT NULL 
                  AND C.ISCORRECT = 'True' THEN 1 
             ELSE 0 
           END - CASE 
                   WHEN Q.ANSWERTYPE = 'M' 
                        AND A.CHOICEID IS NOT NULL 
                        AND C.ISCORRECT = 'False' THEN 1 
                   ELSE 0 
                 END) / ( CASE Q.ANSWERTYPE 
                            WHEN 'S' THEN 1 
                            WHEN 'M' THEN Sum(CASE 
                                                WHEN C.ISCORRECT = 'True' THEN 1 
                                                ELSE 0 
                                              END) 
                          END ) AS score 
FROM   SURVEYQUESTIONS AS Q 
       INNER JOIN SURVEY AS S 
               ON Q.SURVEYID = S.SURVEYID 
       INNER JOIN SURVEYCHOICES AS C 
               ON Q.QUESTIONID = C.QUESTIONID 
       LEFT JOIN SURVEYANSWERS AS A 
              ON A.QUESTIONID = C.QUESTIONID 
       LEFT JOIN SURVEYANSWERS AS sa 
              ON C.CHOICEID = A.CHOICEID 
                 AND A.USERNAME = @UserName 
WHERE  S.SURVEYID = @SurveyId 
GROUP  BY Q.QUESTION, 
          Q.ANSWERTYPE; 

如果需要,我很乐意发布表结构。

提前多多感谢

2 个答案:

答案 0 :(得分:0)

我试图告诉你如何做到这一点,虽然我不完全确定列名和结果列:

with SingleAnswers (QuestionID,SurveyID,Username,TotalSingle)
as(
Select 
    SQ.QuestionID,S.SurveyID, Username,count(*)TotalSingle 
from 
    SurveyAnswers SA 
join  
    surveychoices SC on SA.choiceId = SC.choiceId
join
    surveyquestions SQ on SQ.QuestionID = SC.QuestionID
JOIN 
    Survey S ON SQ.SurveyID =  S.SurveyID
where 
    SA.ChoiceID is Not Null and username = @UserName and SC.iscorrect='True' and SQ.AnswerType='S'
group by
    SQ.QuestionID,S.SurveyID, Username)

,MultipleAnswers (QuestionID,SurveyID,Username,TotalMultiple)
as(
Select 
    SQ.QuestionID,S.SurveyID, Username,count(*)TotalMultiple
from 
    SurveyAnswers SA 
join  
    surveychoices SC on SA.choiceId = SC.choiceId
join
    surveyquestions SQ on SQ.QuestionID = SC.QuestionID
JOIN 
    Survey S ON SQ.SurveyID =  S.SurveyID
where 
    SA.ChoiceID is Not Null and username = @UserName and SC.iscorrect='True' and SQ.AnswerType='M'
group by
    SQ.QuestionID,S.SurveyID, Username)

select 
    count(Q.QuestionID) TotalQuestions,'You got '+Convert(Varchar,sum(TotalSingle) +sum(TotalMultiple))+'out of  '+Convert(Varchar,count(Q.QuestionID))+' number' FinalOutCome,(count(Q.QuestionID)/sum(TotalSingle)+sum(TotalMultiple)) Correct_Percent
from 
    surveyquestions AS Q
JOIN 
    Survey AS S ON Q.SurveyID = S.SurveyID
LEFT JOIN
    SingleAnswers SA ON SA.QuestionID = Q.QuestionID AND SA.SurveyID =  S.SurveyID
LEFT JOIN
    MultipleAnswers MA ON MA.QuestionID = Q.QuestionID AND SA.SurveyID =  S.SurveyID

答案 1 :(得分:0)

您的SQLFiddle示例只有一个调查答案,这不是调查10的一部分。我已经为新的SQL Fiddle添加了一个正确的答案。我认为最后的查询可以让你得到你想要的东西,而且非常简单。

下面的SQL小提琴详情:

SQL Fiddle

MS SQL Server 2008架构设置

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SurveyQuestions]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[SurveyQuestions](
    [QuestionID] [int] IDENTITY(1,1) NOT NULL,
    [SurveyID] [int] NULL,
    [Question] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [AnswerType] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [QuestionOrder] [int] NULL,
 CONSTRAINT [PK_SurveyQuestions_1] PRIMARY KEY CLUSTERED 
(
    [QuestionID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)
)
END
GO
SET IDENTITY_INSERT [dbo].[SurveyQuestions] ON
INSERT [dbo].[SurveyQuestions] ([QuestionID], [SurveyID], [Question], [AnswerType], [QuestionOrder]) VALUES (8, 10, N'Archaeological studies of the Indus Valley cities of Harappa and Mohenjo-Daro show evidence of.', N'S', 1)
INSERT [dbo].[SurveyQuestions] ([QuestionID], [SurveyID], [Question], [AnswerType], [QuestionOrder]) VALUES (9, 10, N'Between 1540 and 1580, what happened to the native population of Central Mexico?', N'M', 2)
INSERT [dbo].[SurveyQuestions] ([QuestionID], [SurveyID], [Question], [AnswerType], [QuestionOrder]) VALUES (11, 9, N'What is Avalon?', N'S', 1)
INSERT [dbo].[SurveyQuestions] ([QuestionID], [SurveyID], [Question], [AnswerType], [QuestionOrder]) VALUES (13, 8, N'What is your quest?', N'S', 2)
INSERT [dbo].[SurveyQuestions] ([QuestionID], [SurveyID], [Question], [AnswerType], [QuestionOrder]) VALUES (14, 11, N'How do you like this video', N'S', 1)
INSERT [dbo].[SurveyQuestions] ([QuestionID], [SurveyID], [Question], [AnswerType], [QuestionOrder]) VALUES (15, 11, N'Your favorite movie', N'M', 2)
INSERT [dbo].[SurveyQuestions] ([QuestionID], [SurveyID], [Question], [AnswerType], [QuestionOrder]) VALUES (16, 10, N'George Boole is a:', N'M', 3)
SET IDENTITY_INSERT [dbo].[SurveyQuestions] OFF

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Survey]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Survey](
    [SurveyID] [int] IDENTITY(1,1) NOT NULL,
    [Title] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [Description] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 CONSTRAINT [PK_Survey] PRIMARY KEY CLUSTERED 
(
    [SurveyID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)
)
END
GO
SET IDENTITY_INSERT [dbo].[Survey] ON
INSERT [dbo].[Survey] ([SurveyID], [Title], [Description]) VALUES (8, N'The Bridge of Death', N'Test your knowledge of this classic scene from a classic movie.')
INSERT [dbo].[Survey] ([SurveyID], [Title], [Description]) VALUES (9, N'What''s Next at Microsoft?', N'Reading your blogs?  Test your knowledge about what''s baking in Microsoft''s oven.')
INSERT [dbo].[Survey] ([SurveyID], [Title], [Description]) VALUES (10, N'Movie History', N'Test your knowledge of movies')
SET IDENTITY_INSERT [dbo].[Survey] OFF


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SurveyChoices]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[SurveyChoices](
    [ChoiceID] [int] IDENTITY(1,1) NOT NULL,
    [QuestionID] [int] NOT NULL,
    [Choice] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [IsCorrect] [bit] NULL,
 CONSTRAINT [PK_SurveyChoices] PRIMARY KEY CLUSTERED 
(
    [ChoiceID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)
)
END
GO
SET IDENTITY_INSERT [dbo].[SurveyChoices] ON
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (56, 8, N'dynastic rule', 1)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (57, 8, N'monotheism', 0)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (58, 8, N'social equality', 0)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (59, 8, N'urban planning', 0)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (60, 9, N'It decreased by approximately 6 million people.', 0)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (61, 9, N'It increased to a high of 26 million people.', 0)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (62, 9, N'It increased by approximatley 2 million people per year.', 1)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (63, 9, N'It decreased to 8 million people.', 1)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (64, 11, N'Build UI with XAML', 0)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (65, 11, N'Next Generation Web Services', 1)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (66, 11, N'Next Version of Office', 0)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (67, 11, N'Codename for Xbox 360', 0)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (89, 13, N'To seek the Holy Grail', 1)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (90, 13, N'To seek the Holy Hand Grenade', 0)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (91, 13, N'To see the Knight that says Ni!', 0)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (92, 13, N'To sing the Ballad of Brave Sir Robin', 0)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (93, 14, N'Bad', 0)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (94, 14, N'Ok', 0)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (95, 14, N'Good', 0)
INSERT [dbo].[SurveyChoices] ([ChoiceID], [QuestionID], [Choice], [IsCorrect]) VALUES (97, 14, N'Excellent', 1)
SET IDENTITY_INSERT [dbo].[SurveyChoices] OFF

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SurveyAnswers]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[SurveyAnswers](
    [AnswerID] [int] IDENTITY(1,1) NOT NULL,
    [QuestionID] [int] NOT NULL,
    [ChoiceID] [int] NULL,
    [ChoiceText] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [UserName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 CONSTRAINT [PK_SurveyAnswers] PRIMARY KEY CLUSTERED 
(
    [AnswerID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)
)
END
GO
SET IDENTITY_INSERT [dbo].[SurveyAnswers] ON
INSERT [dbo].[SurveyAnswers] ([AnswerID], [QuestionID], [ChoiceID], [ChoiceText], [UserName]) VALUES (51, 14, 95, NULL, N'John.Doe')
INSERT [dbo].[SurveyAnswers] ([AnswerID], [QuestionID], [ChoiceID], [ChoiceText], [UserName]) VALUES (52, 9, 62, NULL, N'John.Doe')
SET IDENTITY_INSERT [dbo].[SurveyAnswers] OFF

查询1

SELECT SUM(CASE WHEN SC.IsCorrect = 1 THEN 1 ELSE 0 END) AS TotalQuestions,
       COUNT(SA.AnswerID) AS TotalCorrect,
       CAST(COUNT(SA.AnswerID) AS Float) / 
            SUM(CASE WHEN SC.IsCorrect = 1 THEN 1 ELSE 0 END) * 100 As Percentage
FROM
SurveyQuestions SQ
INNER JOIN SurveyChoices SC
    ON SQ.QuestionId = SC.QuestionID
LEFT JOIN SurveyAnswers SA
  ON SA.QuestionId = SC.QuestionID 
    AND SA.ChoiceID = SC.ChoiceID
    AND SA.UserName = 'John.Doe'
WHERE SC.IsCorrect = 1 
  AND SQ.SurveyID = 10

<强> Results

| TOTALQUESTIONS | TOTALCORRECT |      PERCENTAGE |
---------------------------------------------------
|              3 |            1 | 33.333333333333 |