在SQL Server中将Null显示为0

时间:2017-05-04 01:49:39

标签: sql sql-server

enter image description here尝试在我的报告中将NULL显示为zero

  • ISNULL(course, ' ') - 这样可以将空单元显示为空白
  • ISNULL(count(course), '') - 这不起作用

我尝试ISNothing使用SQL Server 12将计数显示为零,但没有。

有什么建议吗?

由于

3 个答案:

答案 0 :(得分:2)

你的代码是......

SELECT college,
       department,
       COUNT( department ),
       MONTH( [date] ),
       CASE
           WHEN DATEPART( MONTH, [date] ) = 1 THEN
               ISNULL( COUNT( department ),
                       ' ' )
           END AS 'January',
       CASE
           WHEN DATEPART( MONTH, [date] ) = 2 THEN
               ISNULL( COUNT( department ),
                       ' ' )
           END AS 'February'
FROM rpt_school
GROUP BY college,
         department,
         MONTH( [date] );

如果您的目的是获取每个学院的学院,部门,每个学院内每个部门的记录数量以及每个部门JanuaryFebruary的记录数量在每个大学里面,我建议以下......

SELECT college,
       department,
       COUNT( department ) AS departmentCount,
       COUNT( CASE
                  WHEN DATEPART( MONTH, [date] ) = 1 THEN
                      1
              END
            ) AS 'January',
       COUNT( CASE
                  WHEN DATEPART( MONTH, [date] ) = 2 THEN
                      1
              END
            ) AS 'February'
FROM rpt_school
GROUP BY college,
         department;

如果您有任何问题或意见,请随时发表评论。

附录

要获取每个collegedepartmentcollege的列表,以及每个学院内每个部门与date相关联的month的列表{{1}请尝试以下内容......

SELECT collegeDepartments.college AS college,
       collegeDepartments.department AS department,
       COUNT( CASE
                  WHEN monthNumber = 1 THEN
                      COALESCE( countOfMonthNumber, 0 )
                  ELSE
                      0
              END
            ) AS 'January',
       COUNT( CASE
                  WHEN monthNumber = 2 THEN
                      COALESCE( countOfMonthNumber, 0 )
                  ELSE
                      0
              END
            ) AS 'February',
       COUNT( CASE
                  WHEN monthNumber = 3 THEN
                      COALESCE( countOfMonthNumber, 0 )
                  ELSE
                      0
              END
            ) AS 'March',
       COUNT( CASE
                  WHEN monthNumber = 4 THEN
                      COALESCE( countOfMonthNumber, 0 )
                  ELSE
                      0
              END
            ) AS 'April',
       COUNT( CASE
                  WHEN monthNumber = 5 THEN
                      COALESCE( countOfMonthNumber, 0 )
                  ELSE
                      0
              END
            ) AS 'May',
       COUNT( CASE
                  WHEN monthNumber = 6 THEN
                      COALESCE( countOfMonthNumber, 0 )
                  ELSE
                      0
              END
            ) AS 'June',
       COUNT( CASE
                  WHEN monthNumber = 7 THEN
                      COALESCE( countOfMonthNumber, 0 )
                  ELSE
                      0
              END
            ) AS 'July',
       COUNT( CASE
                  WHEN monthNumber = 8 THEN
                      COALESCE( countOfMonthNumber, 0 )
                  ELSE
                      0
              END
            ) AS 'August',
       COUNT( CASE
                  WHEN monthNumber = 9 THEN
                      COALESCE( countOfMonthNumber, 0 )
                  ELSE
                      0
              END
            ) AS 'September',
       COUNT( CASE
                  WHEN monthNumber = 10 THEN
                      COALESCE( countOfMonthNumber, 0 )
                  ELSE
                      0
              END
            ) AS 'October',
       COUNT( CASE
                  WHEN monthNumber = 11 THEN
                      COALESCE( countOfMonthNumber, 0 )
                  ELSE
                      0
              END
            ) AS 'November',
       COUNT( CASE
                  WHEN monthNumber = 12 THEN
                      COALESCE( countOfMonthNumber, 0 )
                  ELSE
                      0
              END
            ) AS 'December'
FROM ( SELECT college AS college,
              department AS department,
              monthNumber AS monthNumber
       FROM ( SELECT college AS college,
              department AS department
              FROM rpt_school
              GROUP BY college,
                       department
            ) AS collegeDepartments,
            ( SELECT 1 AS monthNumber
              UNION SELECT 2
              UNION SELECT 3
              UNION SELECT 4
              UNION SELECT 5
              UNION SELECT 6
              UNION SELECT 7
              UNION SELECT 8
              UNION SELECT 9
              UNION SELECT 10
              UNION SELECT 11
              UNION SELECT 12
            ) AS monthNumbers
     ) AS collegeDepartmentsMonthNumbers
LEFT JOIN ( SELECT college AS college,
                   department AS department,
                   DATEPART( MONTH, rpt_school.[date] ) AS monthNumber,
                   COUNT( * ) AS countOfMonthNumber
            FROM rpt_school
            GROUP BY college,
                     department,
                     monthNumber
          ) AS countOfMonthPartFinder ON collegeDepartmentsMonthNumbers.college = countOfMonthPartFinder.college
                                AND collegeDepartmentsMonthNumbers.department = countOfMonthPartFinder.department
                                AND collegeDepartmentsMonthNumbers.monthNumber = countOfMonthPartFinder.monthNumber
ORDER BY college,
         department,
         collegeDepartmentsMonthNumbers.monthNumber;

此语句以以下子查询开始...

SELECT college AS college,
       department AS department
FROM rpt_school
GROUP BY college,
         department

此子查询编译来自college的{​​{1}}和department的唯一组合列表。现在而不是后来执行此分组会消除很多连接,并且应该使整个语句更有效。

然后对此子查询的结果和以下子查询的结果执行rpt_school ...

CROSS JOIN

此子查询创建每个月号的列表。执行的SELECT 1 AS monthNumber UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12 具有将每月编号的列表附加到CROSS JOINcollege的每个唯一组合的效果。然后,通过选择数据集的每个字段,将此department ed数据集的字段返回到主语句,如下所示...

CROSS JOIN

请注意,两个表之间的SELECT college AS college, department AS department, monthNumber AS monthNumber FROM ( SELECT college AS college, department AS department FROM rpt_school GROUP BY college, department ) AS collegeDepartments, ( SELECT 1 AS monthNumber UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12 ) AS monthNumbers 语法为CROSS JOIN

tblTable1, tblTable2 ed数据集随后CROSS JOIN添加到子查询中,该子查询查找属于每个LEFT JOIN的{​​{1}}的{​​{1}}个dates,按照以下内容......

month

然后选择我们现在最终加入的数据集中的departmentcollege以及一系列JOIN ( SELECT college AS college, department AS department, DATEPART( MONTH, rpt_school.[date] ) AS monthNumber, COUNT( * ) AS countOfMonthNumber FROM rpt_school GROUP BY college, department, monthNumber ) AS countOfMonthPartFinder ON collegeDepartmentsMonthNumbers.college = countOfMonthPartFinder.college AND collegeDepartmentsMonthNumbers.department = countOfMonthPartFinder.department AND collegeDepartmentsMonthNumbers.monthNumber = countOfMonthPartFinder.monthNumber 语句,这些语句将返回当前月份记录的计数或适当的零。

答案 1 :(得分:0)

也可以这样做:

select case when course  is null then '0' else course end

它甚至接受文字。

答案 2 :(得分:0)

在样本数据中,我们有两个空值,其计数显示为AS 2

    Declare @Course TABLE (Course VARCHAR(50))
    Insert INTO @Course
    SELECT 'ABC'    Union all
    SELECT NUll  Union all
    SELECT 'ABC'     Union all
    SELECT NUll  Union all
    SELECT 'dfg'    
SELECT Course,COUNT(ISNULL( NULLIF(Course, NULL) ,'0'))AS CourseCount
FROM @Course
Group by Course

输出

Rno |Course |CourseCount
------------------------
1   |NULL   |2
2   |ABC    |2
3   |dfg    |1