开发者

I need help with subtracting the results from two queries

开发者 https://www.devze.com 2023-03-19 03:27 出处:网络
DECLARE @TotalQuestions int; DECLARE @CorrectQuestions int; DECLARE @IncorrectQuestions int; SELECT ( SET CorrectQuestion = SELECT COUNT( WiningComment)
    DECLARE @TotalQuestions int;
DECLARE @CorrectQuestions int;
DECLARE @IncorrectQuestions int;

SELECT (
  SET CorrectQuestion = SELECT COUNT( WiningComment)
    FROM Threads
    WHERE WiningComment IN (SELECT CommentsID
    FROM Comments
    WHERE  UsersID=@UserID)
) 开发者_如何学JAVAas 'WinningAnswers',
(
    SET TotalQuestions =  SELECT COUNT(CommentsID)
    FROM  Comments
    WHERE  UsersID=@UserID
) as 'TotalAnswers'
(
  SELECT  (TotalQuestions-CorrectQuestions ) //I am not sure about this part!!
) as 'IncorrectQuestions' 

I am not sure about the last part, I want to subtract the results of one subquery from the results of another subquery


Try this:

   DECLARE @TotalQuestions int;
DECLARE @CorrectQuestions int;
DECLARE @IncorrectQuestions int;

SELECT @CorrectQuestions = COUNT( WiningComment) 
    FROM Threads
    WHERE WiningComment IN (SELECT CommentsID
    FROM Comments
    WHERE  UsersID=@UserID)


    SELECT @TotalQuestions =  COUNT(CommentsID)
    FROM  Comments
    WHERE  UsersID=@UserID


  SELECT  @IncorrectQuestions = (@TotalQuestions-@CorrectQuestions ) 

Select @CorrectQuestions as 'WinningAnswers',
@TotalQuestions as 'TotalAnswers',
@IncorrectQuestions as 'IncorrectQuestions'
0

精彩评论

暂无评论...
验证码 换一张
取 消