如何将多行与两个不同表中的其他行进行比较并检查它们是否相等?

时间:2017-08-02 06:51:56

标签: sql-server dynamicquery

我有2个名为successMsg = getJdbcTemplate().batchUpdate("{call "+ packageName +"."+ procedureName +sb.toString(),new BatchPreparedStatementSetter(){ @Override public void setValues(PreparedStatement ps, int i) throws SQLException { String values = insertList.get(i); String []valuesArray=values.split(","); int parameterCount=ApplicationConstant.ONE; for(int valueIndex=0;valueIndex<valuesArray.length;valueIndex++) { ps.setString(parameterCount++, String.valueOf(valuesArray[valueIndex])!=null?String.valueOf(valuesArray[valueIndex]).trim().replaceAll("@@@", ","):""); //added trim() in order to avoid blank space entry in DB. } if(financialMonth!=null&&!financialMonth.equals("")) { ps.setString(parameterCount++,"01-"+financialMonth); } ps.setInt(parameterCount++,loginBean.getUserId()); ps.setDate(parameterCount, null); } @Override public int getBatchSize() { return insertList.size(); } }); OptionText的表格第一个表格包含SubmittedAns QuestionIdOptionId。此处IsRight用于针对该问题的选项是正确还是错误。这里IsRight有多个QuestionId甚至也可以有一个OptionId。第二个表表示用户提交的内容。他们可以根据自己的假设选择一个选项或多个选项。现在我需要制作自动脚本,以证明提交的答案是对还是错。

注意:如果一个问题有多个正确的选项,那么用户必须选择所有正确的选项,如果缺少一个,那么结果将为false。但是,如果他选择了所有正确的答案,那么答案就是真的。

我已经尝试了这个script。它只能计算所选的数字,但不能证明答案是正确还是错误。所以我需要帮助。

我可以假设我需要一个WHILE循环来检查每个元素。但是怎么样?所以我需要帮助。这是我的代码。

CREATE TABLE OptionText(
    [OptionTextId] [bigint] IDENTITY(1,1) NOT NULL,
    [QuestionId] [bigint] NOT NULL,
    [IsRightAnswer] [bit] NOT NULL)

    Insert into OptionText (QuestionId, IsRightAnswer) VALUES (5, 1)    
    Insert into OptionText (QuestionId, IsRightAnswer) VALUES (5, 0)   
    Insert into OptionText (QuestionId, IsRightAnswer) VALUES (5, 0)

    Insert into OptionText (QuestionId, IsRightAnswer) VALUES (17, 0)    
    Insert into OptionText (QuestionId, IsRightAnswer) VALUES (17, 1)   
    Insert into OptionText (QuestionId, IsRightAnswer) VALUES (17, 1)

CREATE TABLE SubmittedAns(
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [QuestionId] [bigint] NOT NULL,
    [OptionTextId] [bigint] NOT NULL)


    Insert into SubmittedAns (QuestionId, OptionTextId) VALUES (5, 1)    
    Insert into SubmittedAns (QuestionId, OptionTextId) VALUES (5, 2)

    Insert into SubmittedAns (QuestionId, OptionTextId) VALUES (2, 1)
    Insert into SubmittedAns (QuestionId, OptionTextId) VALUES (2, 1)


    select * from OptionText
    select * from SubmittedAns
    if (select count(OptionTextId) from SubmittedAns where QuestionId =5) = (select count(ot.OptionTextId) from OptionText as ot  where ot.IsRightAnswer = 1)
     select 1  as "isRight"
    else
     select 0  as "isRight"

2 个答案:

答案 0 :(得分:1)

请参阅第一个&amp;关键材料的最后一行:

SELECT CASE COUNT(*) WHEN 0 THEN 'Pass' ELSE 'Fail' END AS Boolean
FROM (
    SELECT *
    FROM #OptionText
    WHERE QuestionId = 5
      AND IsRightAnswer = 1
) AS OT
FULL OUTER JOIN #SubmittedAns AS SA ON OT.QuestionId = SA.QuestionId AND OT.OptionTextId = SA.OptionTextId
WHERE SA.QuestionId = 5
  AND OT.OptionTextId IS NULL -- This means some answers failed to be matched with your original questions/options, either because IsRightAnswer is zero, or because it doesn't exist in your questions/answers.

答案 1 :(得分:1)

我的理解方式得到了解决方案。所以我使用了一个函数将它应用于所有问题并创建了这段代码。它正在发挥作用。

CREATE FUNCTION [dbo].[Set_IsCorrect_Answer_Deva]
(
    @QuestionId BIGINT
)
RETURNS  BIT
AS
BEGIN

DECLARE @IsRightAns BIT
DECLARE @count int

set @IsRightAns = 0
Declare @supplied_count int
select @supplied_count = Count(*) from SuppliedAnswersTemp where QuestionId=@QuestionId

 IF(@supplied_count>0)
    Begin
        IF(@supplied_count=(select Count(*) from OptionText where QuestionId=@QuestionId and IsRightAnswer=1))
            Begin
                    select @count=Count(*) from OptionText ot join  SuppliedAnswersTemp sa on ot.QuestionId = sa.QuestionId
                     where ot.QuestionId= @QuestionId and ot.IsRightAnswer =1 and ot.Id=sa.OptionId
            END
END

IF(@count>0)
    Set @IsRightAns=1

RETURN   @IsRightAns
END