如何显示发送和非发送测验的总数?

时间:2012-03-12 05:00:24

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

我正在开发一个内联网Web应用程序,它是一个测验引擎。现在,我需要提出一个查询,显示发送和发送的总数。非发送测验。我现在使用它的当前查询仅显示发送测验的数量:

SELECT COUNT(QuizID) AS [Total Number of Sending Quizzes] 
  FROM dbo.Quiz 
 WHERE (IsSent=1)

那么我应该如何修改它以获得非发送测验的总数?

为了您的信息,在数据库中,我有一个名为Quiz Table的表,包括:QuizID,Title,Description,IsSent。

IsSent是一个标志/布尔值,它采用True(1)或False(0)来确定是否发送了测验。

修改

我想获得两列:一列名为“已发送总数”,第二列为“未发送总数”。 如何获取这些列?

3 个答案:

答案 0 :(得分:1)

可能是允许null为IsSent列,所以你也应该处理这个

SELECT 
        CASE WHEN Isnull(IsSent,0) = 0 THEN  'Total not Sent' 
        ELSE 'Total Sent'
        END 'Quiz Status'
        ,COUNT(QuizID) 'Count'
FROM    dbo.Quiz 
GROUP BY isnull(issent,0)

根据评论进行修改:

SELECT (  SELECT count(SeqID) 
          FROM   quiz 
          WHERE  issent = 1 ) AS 'Total Number of not Sent'  

      , ( SELECT count(SeqID) 
          FROM   quiz 
          WHERE  isnull(issent,0) = 0 ) AS 'Total Number of Sent' 

答案 1 :(得分:0)

SELECT COUNT(QuizID) AS [Total Number of Sending Quizzes] 
  FROM dbo.Quiz 
 GROUP BY IsSent

答案 2 :(得分:0)

您可以按IsSent列的值对结果进行分组,如下所示:

SELECT COUNT(QuizID),
        CASE WHEN IsSent = 1 THEN  [Total Number of Sending Quizzes] 
        ELSE [Total Number of Non-Sending Quizzes] 
        END [status]
FROM dbo.Quiz 
GROUP BY IsSent